I was using a msgbox to get a user response but after some search found out that doesn't work on a webserver for getting and responding to client interaction.
So I found this post and tried the method NC describes there.
http://forums.asp.net/t/1175290.aspx
Now if I use an input tag my code behind wont find/recognize the id so I tried using a textbox that I hide onload of the page. I get the clientside messagebox but my actions that are needed when clicked on ok or cancel are not being done? Anyone has any
more insight on this?
Here are the code snippets:
In Page_Load:
If IsPostBack AndAlso Request("__EVENTTARGET") = "confirmValueVraagVerwijder" Then
Me.Response.Write("confirmValueVraagVerwijder.Text: " + confirmValueVraagVerwijder.Text + "<br>")
If confirmValueVraagVerwijder.Text = "true" Then
' User answered OK
Dim objController As KlantendienstController = New KlantendienstController
objController.DeleteVraagCategorieV(lstVragen.SelectedValue)
objController.DeleteVraag(lstVragen.SelectedValue)
lstVragen.Items.Remove(lstVragen.SelectedItem)
lblMessage.Text = "De bewerking werd successvol doorgevoerd."
'Page.ClientScript.GetPostBackEventReference(confirmValueVraagVerwijder, String.Empty)
Else
' User answered Cancel
lblMessage.Text = "De vraag werd niet verwijderd."
End If
Then in my button onclick:
Dim scriptString As String = "<script language=JavaScript>" + Environment.NewLine ' Store the confirm's return in the hidden control... scriptString += "document.getElementById('" + confirmValueVraagVerwijder.ClientID + "').value = " + Environment.NewLine scriptString += " confirm('Are you sure?');" + Environment.NewLine ' Do a new PostBack... scriptString += Page.ClientScript.GetPostBackEventReference(confirmValueVraagVerwijder, String.Empty) + ";" + Environment.NewLine scriptString += "</" scriptString += "script>"
Not to make this too simple, but why don't you just add the following:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Button1.Attributes.Add("onclick", "return confirm('Are you sure?');")
End Sub
If they click "yes", the server-side button click code executes. If they click "no", it does not. Simple as that.
When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.
private void Page_Load(object sender, System.EventArgs e)
{
// Insure that the __doPostBack() JavaScript is added to the page...
this.GetPostBackEventReference(this, string.Empty);
if ( eventTarget = "UserConfirmationPostBack" )
{
if ( eventArgument == "true" )
// User said to go ahead and do it...
else
// User said NOT to do it...
}
}
}
Also, do NOT use Response.Write to render your JavaScript.
Thanks alot for the responses. I indeed need server side processing PRIOR to the confirmmessage and AFTER the confirmmessage after wich my controls on the page need to be updated So I will need the method of NC. Thanks alot for the update. I will be testing
it monday and post then if I got it to work. Thanks for the help.
Don't really get it yet. Here is a little testpage I made but I am not getting any confirmmessage yet:
In my Page_Load:
Page.ClientScript.GetPostBackEventReference(Me, String.Empty)
If (Page.IsPostBack) Then
Dim eventTarget As String
If (Page.Request("__EVENTTARGET") = Nothing) Then
eventTarget = String.Empty
Else
eventTarget = Page.Request("__EVENTTARGET")
End If
Dim eventArgument As String
If (Page.Request("__EVENTARGUMENT") = Nothing) Then
eventArgument = String.Empty
Else
eventArgument = Page.Request("__EVENTARGUMENT")
End If
If (eventTarget = "UserConfirmationPostBack") Then
If (eventArgument = "True") Then
'User said to go ahead and do it...
Label3.Text = "You clicked YES - added after confirm during page load"
Else
Label3.Text = "You clicked NO - added after confirm during page load"
End If
End If
End If
In my code behind button event:
Dim isConfirmNeeded As Boolean = True
Dim confirmMessage As String = "This is the message, yes or no"Dim scriptKey As String = "test"If (isConfirmNeeded) Then
Dim javaScript As System.Text.StringBuilder = New System.Text.StringBuilder()
javaScript.Append("\n<script type=text/javascript>\n")
javaScript.Append("<!--\n")
javaScript.Append("var userConfirmation = window.confirm('" + confirmMessage + "');\n")
javaScript.Append("__doPostBack('UserConfirmationPostBack', userConfirmation);\n")
javaScript.Append("// -->\n")
javaScript.Append("</script>\n")
Page.ClientScript.RegisterStartupScript(Me.GetType, scriptKey, javaScript.ToString())
End If
1. Are you using AJAX by chance?
2. Is the JavaScript getting rendered (do a View Source with the page displayed in your browser and look towards the bottom).
There is not much else that I have to go on here with the code that you have posted as it works for me.
Senthrax
Member
51 Points
92 Posts
client side msgbox with asp.net and ajax help
Jan 31, 2008 09:01 AM|LINK
I was using a msgbox to get a user response but after some search found out that doesn't work on a webserver for getting and responding to client interaction.
So I found this post and tried the method NC describes there.
http://forums.asp.net/t/1175290.aspx
Now if I use an input tag my code behind wont find/recognize the id so I tried using a textbox that I hide onload of the page. I get the clientside messagebox but my actions that are needed when clicked on ok or cancel are not being done? Anyone has any more insight on this?
Here are the code snippets:
In Page_Load:
If IsPostBack AndAlso Request("__EVENTTARGET") = "confirmValueVraagVerwijder" Then Me.Response.Write("confirmValueVraagVerwijder.Text: " + confirmValueVraagVerwijder.Text + "<br>") If confirmValueVraagVerwijder.Text = "true" Then ' User answered OK Dim objController As KlantendienstController = New KlantendienstController objController.DeleteVraagCategorieV(lstVragen.SelectedValue) objController.DeleteVraag(lstVragen.SelectedValue) lstVragen.Items.Remove(lstVragen.SelectedItem) lblMessage.Text = "De bewerking werd successvol doorgevoerd." 'Page.ClientScript.GetPostBackEventReference(confirmValueVraagVerwijder, String.Empty) Else ' User answered Cancel lblMessage.Text = "De vraag werd niet verwijderd." End IfThen in my button onclick:
Thanks in advance for the assistance.
vik20000in
All-Star
25882 Points
3993 Posts
MVP
Re: client side msgbox with asp.net help
Jan 31, 2008 11:17 AM|LINK
have a look at the following article which talks about the same topic
http://msdn2.microsoft.com/en-us/library/bb428868.aspx
www.vikramlakhotia.com
Please mark the answer if it helped you
DisturbedBud...
Star
11523 Points
1911 Posts
Re: client side msgbox with asp.net help
Jan 31, 2008 01:29 PM|LINK
Not to make this too simple, but why don't you just add the following:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Button1.Attributes.Add("onclick", "return confirm('Are you sure?');") End SubIf they click "yes", the server-side button click code executes. If they click "no", it does not. Simple as that.
My latest ASP.NET AJAX blog entries.
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: client side msgbox with asp.net help
Jan 31, 2008 03:02 PM|LINK
There were so many responses on the link that you provided, that I decided to re-post the solution.
As DisturbedBuddha posted the easiest way, as long as no server-side processing is required PRIOR to the confirmation message is:
private void Page_Load(object sender, System.EventArgs e)
{
Button1.Attributes.Add("onclick", "return window.confirm('Are you sure?');");
}
If however you need to do server-side processing PRIOR to the confirmation message:
private void Button1_Click(object sender, System.EventArgs e)
{
bool isConfirmNeeded = false;
string confirmMessage = string.Empty;
// All server side execution goes here and set isConfirmNeeded to true,
// and create the confirmMessage text, if user confirmation is needed.
if ( isConfirmNeeded )
{
System.Text.StringBuilder javaScript = new System.Text.StringBuilder();
javaScript.Append("\n<script type=text/javascript>\n");
javaScript.Append("<!--\n");
javaScript.Append("var userConfirmation = window.confirm('" + confirmMessage + "');\n");
javaScript.Append("__doPostBack('UserConfirmationPostBack', userConfirmation);\n");
javaScript.Append("// -->\n");
javaScript.Append("</script>\n");
RegisterStartupScript(scriptKey, javaScript.ToString());
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Insure that the __doPostBack() JavaScript is added to the page...
this.GetPostBackEventReference(this, string.Empty);
if ( this.IsPostBack )
{
string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
if ( eventTarget = "UserConfirmationPostBack" )
{
if ( eventArgument == "true" )
// User said to go ahead and do it...
else
// User said NOT to do it...
}
}
}
Also, do NOT use Response.Write to render your JavaScript.
NC...
JoshStodola
Star
13736 Points
3177 Posts
Re: client side msgbox with asp.net help
Jan 31, 2008 04:54 PM|LINK
If you have client-side validation that needs to execute before submitting the form, you will need to do it this way...
Button1.Attributes.Add("onclick", "if(!confirm('Are you sure?')) return false;");Best regards...
Senthrax
Member
51 Points
92 Posts
Re: client side msgbox with asp.net help
Feb 01, 2008 07:33 AM|LINK
Thanks alot for the responses. I indeed need server side processing PRIOR to the confirmmessage and AFTER the confirmmessage after wich my controls on the page need to be updated So I will need the method of NC. Thanks alot for the update. I will be testing it monday and post then if I got it to work. Thanks for the help.
Senthrax
Member
51 Points
92 Posts
Re: client side msgbox with asp.net help
Feb 06, 2008 09:35 AM|LINK
Haven't got to testing it yet, other priority atm so will do next week. the scriptKey is just a name you can choose for the script right?
How do I create another postback so my info on my page gets updated? after the user has said YES I do more processing and need to update the page.
Thanks in advance.
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: client side msgbox with asp.net help
Feb 06, 2008 11:46 AM|LINK
Yes scriptKey is just a name you choose for the script. It insures that the script is not added more than once.
The __doPostBack call, created in the server-side button click event handler, generates the PostBack which you catch in the Page_Load event handler.
I do not see why you would need yet another PostBack. You would just do your update here:
...
if ( eventArgument == "true" )
{
// User said to go ahead and do it...
// Do any processing for a "Yes" response
}
else
{
// User said NOT to do it...
// Do any processing for a "No" response
}
...
NC...
Senthrax
Member
51 Points
92 Posts
Re: client side msgbox with asp.net help
Feb 07, 2008 10:50 AM|LINK
Don't really get it yet. Here is a little testpage I made but I am not getting any confirmmessage yet:
In my Page_Load:
Page.ClientScript.GetPostBackEventReference(Me, String.Empty) If (Page.IsPostBack) Then Dim eventTarget As String If (Page.Request("__EVENTTARGET") = Nothing) Then eventTarget = String.Empty Else eventTarget = Page.Request("__EVENTTARGET") End If Dim eventArgument As String If (Page.Request("__EVENTARGUMENT") = Nothing) Then eventArgument = String.Empty Else eventArgument = Page.Request("__EVENTARGUMENT") End If If (eventTarget = "UserConfirmationPostBack") Then If (eventArgument = "True") Then 'User said to go ahead and do it... Label3.Text = "You clicked YES - added after confirm during page load" Else Label3.Text = "You clicked NO - added after confirm during page load" End If End If End IfIn my code behind button event:
Thanks for you help.
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: client side msgbox with asp.net help
Feb 07, 2008 12:40 PM|LINK
1. Are you using AJAX by chance?
2. Is the JavaScript getting rendered (do a View Source with the page displayed in your browser and look towards the bottom).
There is not much else that I have to go on here with the code that you have posted as it works for me.
NC...