I need to display a modal popup from a aspx.cs page. I need to invoke the popup from server side because before the popup opens, I need to pass an ID into the popup via query string.
this is my code to display the popup.
protected void btnNote_Click(object sender, EventArgs e)
{
string queryStringParam = "some text"; // some server code here to get the string ready;
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "openNotePopup('"+ queryStringParam +"');", true);
}
And this is the javascript to get the parameter and launch the modal popup.
function openNotePopup(var param)
{
var noteResult = window.showModalDialog("AddEditNote.aspx?Note=" + param, "Add/Edit Notes", 'center:yes; dialogWidth:600px; dialogHeight:500px;');
document.getElementById("hidden_NoteText").value = noteResult;
}
When the popup is closed, I pass a string value as window.returnValue which is captured in the noteResult variable in client side.
Now I need to capture the popup close event in my server side. I can capture the event in client side but I need the event in server side so that I can pick up the value from the hidden field and process it.
How can I achieve this?
I found a thread that seems to tackle this very issue. Hopefully this is something similar to what you were looking for:
Javascript confirm message problem
I suggest you to write you own function on ShowDialog like this:
showNotePopup('NotePopup', title, closeNotePopup);
NotePopup - id of your popup;
showNotePopup should describe what you wanna see in your popup, how it will close;
closeNotePopup function you bind to popup closing and inside it you can make for example post-request and this way you'll catch on server when your popup is closing.
Related
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 have a button and textbox on my page aspx and on button click event I am displaying alert message.
Now when I run website and click on button it display message but after that when I press F5 (Refresh) it again display message. So my question how to remove message when I click F5.
Below is my code for button click:
protected void btnExport_Click(object sender, EventArgs e)
{
DisplayMessage("There is no data to Export.", this);
}
and
public void DisplayMessage(String strMessage, Control name)
{
string script = "<script language='javascript'>alert('" + strMessage + "');</script>";
ScriptManager.RegisterStartupScript(name, name.GetType(), "JSCR", script, false);
}
Change your script as follows
string script = "alert('hello');window.location.href='Default.aspx'";
where i assumed Default.aspx is your page
You are instigating a client side event from server side which usually does not make sense.
The behaviour you are noticing is correct, when you clicked that button a request is posted to the server which calls your button click handler which then registers the client side script when the response is returned. When you press F5 to refresh your browsers is posting that exact same request again to the server, so the button handler is fired again.
I advise you read a little about how Post And Gets
work as well as how postbacks work in ASP.
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.
I am new in development, I want to show the alert message on button click after that I want to redirect the on another page. my button code is like below:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Write("<script type='text/javascript'> alert('Please wait for approval!');</script>");
Response.Redirect("~/profile/scrapbook.aspx?uid=" + Request.QueryString["uid"], false);
}
in this case alert is not working, If I remove the Response.Redirect then it will works properly.
Please suggest me how to call he alert message after that redirect on another page.
You are mixing server side and client side code.
The Response.Redirect("~/profile/scrapbook.aspx?uid= is server side and the Alert won't wait the action of the user to execute it...
What you will need is to do you redirect in Javascript or to use something else than an Alert for the message.
Solution 1
You display a Alert message in Javascript (client side) when the user press okay you do a redirect in Javascript.
Solution 2
You display a message in the HTML with a button in ASP with an event that will do a call to the server and redirect your user to the page you desire.
Add your script like this:
const string scriptString = "<script type='text/javascript'> alert('Your friend request sent to the user! Please wait for approval!');</script>";
ClientScriptManager script = Page.ClientScript;
script.RegisterClientScriptBlock(GetType(), "randomName", scriptString);
Do not use Response.Write to render your JavaScript. Use RegisterClientScriptBlock or RegisterStartupScript instead.
Your example should be using RegisterStartupScript, since it's rendering executing script, and not function declarations.
I want to execute alert when user clicks LinkButton1.It is not working?
protected void LinkButton1_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "abc", "<script language=javascript>alert(hi)</script>");
}
You mistyped the function name. Change aler to alert. Also, you forgot the delimiters around the string that you are trying to alert.
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "abc", "alert('hi');", true);
use the below
<linkButton runat="server" id="lnk1" OnClientClick="alert('hi');" />
I hope your sample doesn't have a typo, but you are calling "aler" instead of "alert"!
Also, this doesn't define that your button would run this script when the user clicks the link button, it'll be executed when the page gets loaded.
For having such behavior, you need to use the "OnClientClick" control's property, and set there the name of the function - event handler - that would do the alert.
linkButton1.OnClientClick = "myEventHandler";
And define your script in some JavaScript file or by registering a client script block during the pre-render event of your container control or page.