I checked Calling a parent window function from an iframe but it didn't work for me. I want to call parent page javascript function from Code behind of a page inside an iframe.
Page Inside Iframe Code
Protected Sub btnContinue_Click(sender As Object, e As System.EventArgs) Handles btnContinue.Click
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "StartTimer", "parent.initializeClock();", True)
End Sub
It is not working. How should I call this from code behind. It must be called from code behind.
Related
I have master page with custom button control. I have one javascript function in master page. When i try to call this javascript function with in custom control code behind its not invoking. pls check below code.
Javascript code in master page
function loader()
{
//Note: ctl00_loader is master page control.
document.getElementById('ctl00_loader')="X";
}
Mycustom control is
<Shri:ConfirmBtn ID="UsrBtn" runat="server">
Code-behind code is
Protected Sub UsrBtn_ConfirmClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles UsrBtn.ConfirmClick
ClientScript.RegisterStartupScript(GetType(String), "MyCode1", "<Script
Language=Javascript>loader();</script>")
End sub
code behind line is working fine in page loading time. but when i place it custom control click (UsrBtn_ConfirmClick) inside its not loading. how to solve this one? any idea please.
Use jQuery:
In the header of your html page:
<script src="https://code.jquery.com/jquery-3.3.1.js" type="text/javascript"></script>
Then capture the button click
$("#<%=UsrBtn.ClientID%>").bind("ConfirmClick", function () {
// Do whatever you need here ....
});
To debug code embedded in the page add the keyword debugger; like so:
$("#<%=UsrBtn.ClientID%>").bind("ConfirmClick", function () {
// Do whatever you need here ....
debugger;
});
This will force the IDE to stop on that line then you can use the F10 key to step through the code like you would in the code behind.
I'm using Telerik UI for asp.net. Specifically I'm using RadTabStrip with partial page postbacks to allow the user to tab through different sets of data. When the user clicks a tab, some code executes and loads data just for that tab.
I've figured out how to execute codebehind: I set the OnTabClick property of the RadTabStrip, and then in codebehind I check what tab was clicked.
E.g.
protected void tab_Click(object sender, RadTabStripEventArgs e)
{
if (e.Tab.Text == "Info")
{
populateInfoTab();
}
}
private void populateInfotab()
{
// Do some stuff
}
However, I can't figure out how to execute client side javascript after a specific tab is clicked. What I tried:
Set OnClientTabSelected property, and then add some javascript:
function tab_ClientClick(sender, args)
{
var tab = args.get_tab();
if(tab.get_text() == "Info")
{
alert("Tab Clicked");
}
}
The problem is that I need to set the InnerHtml of some div in the clicked pageview after it is clicked. The div does not exist on page load (that specific RadPageView is hidden) so I cannot set it then. Once the user clicks into the tab, and after the page view loads, I need to be able to update the div's InnerHtml through JavaScript.
How would I go about doing this?
First option - if you do not set the RenderSelectedPageOnly property to true, all page views will be rendered on the initial load and you will be able to use JS to find/modify elements in them.
Second option - just set the content from the server as soon as you load the UC, this will usually make things simpler.
Third option - use client-side events (offered by the native PageRequestManager class or the RadAjaxManager, depending on how you setup your AJAX interactions) to execute when the response is received. The difficulty here is to determine which is the postback you need. Looking for the desired element and only executing logic if it exists is the simplest flag you can opt for.
Fourth option - register a script from the server code that will call your function, something like:
populateInfoTab();
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "someKey", "myDesiredFunction()", true);
where you may want to use the Sys.Application.Load to ensure it is executed later than IScriptControl initialization.
I'm using ASP.net; I have a popup browser window that contains an databound gridview with textboxes. It has an "Add to Order" button which takes the values entered and updates the database, then closes the popup and refreshes the parent. This currently works perfectly using window.opener.document.forms[0].submit();self.close(); in a RegisterScriptBlock
I now need to update the database on gridview page chage so that textbox values are not lost. I put window.opener.document.forms[0].submit(); into the PageIndexChanging event of the datagrid, but it does not refresh the parent window. Refreshing the parent window with the order lines helps the user see what they have already ordered. My update database method runs fine, just not the parent browser refresh. I also tried "window.opener.location.href = window.opener.location.href" to no avail.
Thank you in advance!
In parent aspx page write one JavaScript function
function fnReload() {
alert('hi')
window.location.href = window.location.href;
}
Protected Sub grdDisplay_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdDisplay.PageIndexChanging
Dim strScript As New StringBuilder()
Call addItems()
grdDisplay.PageIndex = e.NewPageIndex
Call search()
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "reload", "window.opener.fnReload();", true);
End Sub
Also insert on alert in fnReload() function to check that this function called or not.
Here's the situation: I have a variable number of dynamically created update panels on my page, so I thought I would write one method which handles all of the loading for each one.
My Updatepanel creation looks something like this:
Dim newUpdp As New UpdatePanel
newUpdatep.ChildrenAsTriggers = False
newUpdatep.UpdateMode = UpdatePanelUpdateMode.Conditional
newUpdatep.ID = Guid.NewGuid.ToString
AddHandler newUpdatep.Load, AddressOf updatep_load_method
updatep_Holder.ContentTemplateContainer.Controls.Add(newUpdp)
updatep_Holder.Update()
This creates the update panel and binds its load event to the method "updatep_load_method". This method is called as soon as the Updatepanel is inserted into the holder. The method code is as follows:
Private Sub updp_load_method(sender As UpdatePanel, e As System.EventArgs)
Dim div As New HtmlGenericControl("div")
div.InnerText = Date.Time.Now.ToString
sender.ContentTemplateContainer.Controls.Add(div)
sender.Update()
End Sub
A little later on I want to update the panel, and refresh the time. So I use the javascript __doPostBack method. According to Dave Ward, the __doPostBack method follows the full page postback lifecycle, so I figured the load event of my Update panel would be fired and that "updatep_load_method" would be called by that particular update panel...
Although the partial postback occurs, and other update panel's load events are called, my bound ones aren't. So what's happening here?
You need to recreate dynamic controls during Page_Init and have to use the same Id (so you cannot do a Guid.NewGuid.ToString() while recreating the control).
Otherwise, ASP.Net will not be able to locate your control and populate it with data from ViewState.
Here's a good article that explains more about dynamic controls.
Try implementing the RaisePostBackEvent method in your code:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
//call the RaisePostBack event
base.RaisePostBackEvent(source, eventArgument);
//add whatever code you need to execute on postback here
}
The following lines of code are in a user control in a SharePoint website.
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "jquery144", "<script type=\"text/javascript\" src=\"/_layouts/Unicre.Web.RUOnline.Controlos/Scripts/jquery-1.4.4.min.js\"></script>", false);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "javascriptgeral", "<script type=\"text/javascript\" src=\"/_layouts/Unicre.Web.RUOnline.Controlos/Scripts/javascript.js\"></script>", false);
Why doesn't it work? I also can't find the respective script tags in the HTML generated in the response.
The problem has to do with an update panel. After the partial postback, the Javascript stops working. Shouldn't it work with the above code?
(I also tried the RegisterClientScriptInclude method but with the same result.)
This isn't going to work on a partial postback. You need to register the scripts on Page_Load. In the user control, attach to the Load event and call ScriptManager.RegisterClientScriptBlock() from that handler.
After the partial postback, the Javascript stops working
An UpdatePanel partial postback is DOM update of the UpdatePanel <div> contents. This means the previous contents are lost, so the state of inline script contained within that <div> loses its state.
See here for more information:
UpdatePanel does its work on the client through the innerHTML DOM property. A delta is retrieved from the server, finds itself in the existing DOM, disposes of the contents, and then assigns the new content via innerHTML. ... But inline script doesn't work this way. Setting the innerHTML of a DOM element to HTML which contains a script block does not cause that script to execute.
Likewise, calling ScriptManager.RegisterClientScriptBlock() on an UpdatePanel update is not going to act like a page load. You could add the <script> elements directly to the UpdatePanel contents, but it won't execute.
Update
You can call javascript code after a partial postback by adding an endRequest handler to the PageRequestManager:
<script>
function load() {
//register the handler
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
//this handler will execute after a partial postback
function EndRequestHandler(){
//...arbitrary code...
}
window.onload = load;
</script>