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.
Related
I have my code-behind class and there I have an EventListener, I need to refresh the whole page when my EventListener catches an ´Event´.
How do I do this? I have a JavaScript function in my client-side window.location.reload(true).
The problem is that the javascript never gets executed.
Code-Behind:
private void WebResponse_Msg(object sender, EventArgs e){
ClientScript.RegisterClientScriptBlock(typeof(Page), Guid.NewGuid().ToString(), "refresh();", true);
}
JS:
<script type="text/javascript">
function refresh() {
window.location.reload(true);
}
</script>
Server side code can not trigger javascript (client side code) whenever you want (like when an event is triggered).
However there are several workarounds that I mention 2 of them:
1.Using SignalR
2.Logging that event when triggered in a Session, and checking the session value by ajax periodically.
Using insert functionality of an aspx DetailsView. Would like to show a javascript modal popup window while the new record is processed and added to the database. I can hook the button click in DetailsView_ItemCommand. It's not working, so I started trying to figure out whats going on by simply displaying a javascript Alert() popup. But can't get that to even work. Here's the relevant DetailsView_ItemCommand:
protected void DetailsViewInsertFPL_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "waitMessage", "alert('Please wait while your request is processed');", true);
return;
}
}
After the record is inserted, there is a redirect to another aspx page.
Can anyone steer me down the right path? I'll be looking at some of the aspx page and DetailsView properties next to see if something there isn't set correct.
You cant just use
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "waitMessage", "alert('Please wait while your request is processed');", true);
then preceeded by a
response.redirect("toanotherpage.aspx");
ScriptManager.RegisterStartupScript will render your script after all the elements in the page (right before the form's end tag) hence not executing it when you use response redirect on the same code block.
To achieve what you want you could do one of the following solutions,
Create a Javascript and place you the redirect or for this instance
window.location there after the Alert message you want
Create a pop-up modal using other methods like bootstrap, on the modal declare a button with a code for response.redirect.
Alex Kudryashev's suggestion to enclose the DetailView within an asp:UpdatePanel and use the asp:UpdateProgress to show the "please wait while your request is processed" gave me the solution I needed in this case.
Infrequent user here, so not sure how to give the points to Alex as his suggestion was a comment rather than an answer. Feel free to let me know how to handle the votes in that case.
Thanks!!!! I've been banging my head against the wall for a couple of days to get this one working.
I have the following code to register a javascript function on Page_Load (also tried it on Page_Init). The javascript switches two panels from hidden to shown based on a parameter on load of page:
protected void Page_Load(object sender, EventArgs e)
{
String switchAction = "<script language='javascript'>switchactionpanel(" + (int)((Global.upAction)Enum.Parse(typeof(Global.upAction), Global.ProfileAction.ToString())) + ")</script>";
Page.RegisterClientScriptBlock("switchaction", switchAction);
}
But when the page loads I am receiving an error: $ is not defined.
I looked in Firebug and the jQuery files are being loaded however, the first file that is being loaded in the .Net tab is the page itself. I know the jquery is correct as the same code works on a different page. Where should my RegisterClientScriptBlock be put in the page lifecycle to work correctly when the page loads? Or am I going about this all wrong?
You just need to ensure that the inserted script gets inserted after the JQuery reference.
Use RegisterStartupScript instead -- that inserts the script tag before </form> closing tag.
Not sure if this is relevant but I always use:
<script type="text/javascript"...
Sorry this should have been a comment rather than an answer.
I think it has to do with the register function your using. Try using RegisterStartupScript instead of RegisterClientScriptBlock.
Page.ClientScript.RegisterStartupScript(this.GetType(), "switchaction", "<script language='javascript'>switchactionpanel(" + (int)((Global.upAction)Enum.Parse(typeof(Global.upAction), Global.ProfileAction.ToString())) + ")</script>", false);
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.
In my asp page, I take a screenshot of the client's desktop with an applet and ask them to send it to the server with a single click of a LinkButton. I use runApplet() function to call my applet to capture the screen and assign the strings value to a hidden value. (picture is stored as base64 string) Until here, everything works perfect! However, SendLinkButton_Click doesnt seem to be executing!
This is my link button.
<asp:LinkButton ID="SendLinkButton"
OnClientClick="runApplet(); return false;"
OnClick="SendLinkButton_Click"
Visible="false"
CssClass="portal-arrow portal-button"
runat="server">Send</asp:LinkButton>
This is my Javascript function
function runApplet() {
var msg = document.capture.capture();
var hiddenControl = '<%= inpHide.ClientID %>';
document.getElementById(hiddenControl).value = msg;
}
and this is what's inside of SendLinkButton_Click
protected void SendLinkButton_Click(object sender, EventArgs e)
{
Server.Transfer("Preview.aspx", true);
}
when I put the javascript function to a LinkButton's OnClientClick, and execute this "SendLinkButton_Click" with another LinkButton. It works perfect! But I want them to work with just one click!
Please help!
Your client click is returning false so no postback will be made to the server after this point.
Try changing:
OnClientClick="runApplet(); return false;"
To
OnClientClick="runApplet();"
Remove return false from the OnClientClick attribute. If you return false from it, the postback will not execute.