| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |
|

06-27-10, 19:14
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 143
|
|
|
Popup message
|
|
Hi all,
I am using asp.net to design a web page and I need to popup a confirm message to the user if he wants to continue or cancel an event. I am not too sure how to do this. I have tried using this:
Me.Button2.Attributes("onclick") = "confirm('are you sure?');document.getElementById(""Button2"").invok eMember(""click"");"
It does not work because I am trying to programatically press the button to raise an event button2_Click() event. But the popup message does not appear instead it goes directly to that event handler method. Any idea? Thanks.
|
|

06-27-10, 22:40
|
|
Purveyor of Discontent
|
|
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
|
|
I don't know if the Attributes collection automagically adds keys that it doesn't know about. I've always done .Attributes.Add("key", "value"), you might want to check and see if that's the deal.
Also, asp.net server controls will have their id's drastically altered by the ASP.NET runtime to ensure uniqueness. This is required for viewstate and other mechanisms to be able to properly identify a control upon postback. Generally this means that the id's of all the server control's parent containers will be prepended to the control's id before emitting a response. There's also some added tomfoolery if the control is within a contentplaceholder on a masterpage.
Anyway, you can get the ACTUAL id that will be rendered down to the browser like so:
"getElementById(""" + Button2.ClientId + """)"
Once you get the event wired up properly, you'll probably run in to that next, so far warning.
|
|

06-28-10, 08:56
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 143
|
|
|
|
Thanks for the reply. Yes there is always that question of the appended string to the name of controls. I've tried the button2.clientID like this:
Response.Write(("<script> language='javascript'>document.getElementById(""" + Button2.ClientID + """).InvokeMember(""click"");</script>"))
but it raises null pointer exception. Any reason why? Thanks
|
|

06-28-10, 09:04
|
|
Purveyor of Discontent
|
|
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
|
|
Dunno, I'd take a look at it with the FireBug inspector and make sure the name is what you think it is. Then, of course, simplify the troubleshooting by putting a non-asp.net control on the page that would do something like an alert(document.getElementById("<%= Button2.ClientId %>")); just to narrow the issue down to exactly what part of the statement it doesn't care for.
Shouldn't take too long to suss it out.
|
|

06-28-10, 09:28
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 143
|
|
Thanks. I tried the alert thing you mentioned and it is still giving me null. I viewed the source code when the page is generated and the ID of the button2 is Button2. So am bit confused there.
|
|

06-28-10, 09:37
|
|
Purveyor of Discontent
|
|
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
|
|
Oh? If that's the case, then I don't think you're actually using an ASP.NET server control. Or, in the very least, the runtime doesn't know you wanted it to work with this control or it would have rewritten the id...
Did you make sure to slap a runat="server" on your control?
|
|

06-28-10, 10:43
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 143
|
|
<input id="Button3" type="button" value="button" runat="server" /><br />
<asp:Button ID="Button2" runat="server" Text="Button" />
Yes the runat is there. I used the asp control button and the html button to test but they are both causing the null error.
Code:
Private Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
academicTokensDDL.AutoPostBack = True
Me.Button2.Attributes("onclick") = "confirm('are you sure?');"
'Response.Write(("<script> language='javascript'>confirm('are you sure?');if (!confirm('are you sure?')) {document.getElementById(""" + Button2.ClientID + """).InvokeMember(""click"");}</script>"))
'Response.Write(("<script language='javascript'>document.getElementById('Button2').onClick();</script>"))
Response.Write(("<script language='javascript'>alert(document.getElementById('Button3'));</script>"))
'Me.Button2.Attributes("onclick") = "confirm('are you sure?');"
End Sub
|
|

06-28-10, 11:04
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 143
|
|
If there is an alternative to this because what I am trying to do is this:
if (some condition)
do something
else
popup confirm dialog
if (poupup =OK) do aaa
else do bbb
how do I achieve this? I was trying to click the button programatically to test if this functionality was working but it is being so long. any idea on this one? Thanks for your suggestions
|
|

06-28-10, 11:07
|
|
Purveyor of Discontent
|
|
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
|
|
Pare it down:
Response.Write(("<script language='javascript'>alert(document.getElementByI d(""" + Button2.ClientID + """));</script>"))
You have to pull .ClientId on ANY control that the runtime handles, even if it wasn't a server control. So your Button3 references have to be Button3.ClientId...
Also, did you give .Attributes.Add() a whirl as opposed to just passing the key to .Attributes()? I haven't had a chance to check yet, but again I'm not sure that it is smart enough to add the key if it doesn't exist.
AjaxControlToolkit has a lot of this stuff if you're looking for a more out-of-the-box approach.
|
|

06-28-10, 11:19
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 143
|
|
I tried to add the key as this:
Me.Button3.Attributes.Add("onClick", "return confirm('are you sure?')")
Still giving me null. Gosh can't believe it. If AjaxControlToolkit is more simple, I dont want to waste our time on this. Can you please direct me how I go about this please? And do you need Ajax enabled website to be able to use this toolkit or because I already have a website application under developement and I dont think its ajax enabled. I am using the express 2008 edition. Thanks very much for your effort and help so far. By the way I did download this but I dont really know how to make this work or go about it. any quick tutorial because I only need a simply popup. thanks again
AjaxControlToolkit - Release: 40412
|
|

06-28-10, 12:00
|
|
Purveyor of Discontent
|
|
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
|
|
You do need to "ajax-enable" the site, but that's just a matter of including some sections in your webconfig, all of which are pretty well documented.
|
|

06-28-10, 15:17
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 143
|
|
Thanks. I did download the toolkit and it seems great. Added the extender to the Button2. It works when clicked, ie, displays the popup. But according to the above procedure I outlined, how do i achieve it using the ajax extender? I mean I need to popup the confirm dialog within the else part of the code and if the user presses OK then the next statement is executed else it breaks. Thanks
|
|

06-28-10, 18:17
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 143
|
|
I found another way of doing it through javascript, that is, test whether OK is pressed :
Response.Write("<script language='javascript'>if (confirm('Please select a valid project')) {alert(document.getElementById('Button3'));}")
But the alert is always giving me null whatever control I have on my page. Any help?
|
|

06-28-10, 18:46
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 143
|
|
This is what I did. I put the code at the end of the aspx file before the closing of the </body> tag:
<script type="text/javascript">
function getMessage() {
if (confirm('Please select a valid project')) {
document.getElementById('TextBox4').value = 'hey';
}
else {
document.getElementById('TextBox4').value = 'ho';
}
}
</script>
and then it recognized the control. However I could not achieve this in vb.net though although I did put that code in the page_LoadComplete() function. It should have recognized the control variables?? Maybe somebody could clarify on this.
|
|

06-29-10, 08:55
|
|
Purveyor of Discontent
|
|
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
|
|
Look at RegisterClientScriptBlock, see if that helps
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|