RegisterStartupScript cannot work.

R

RickDee

Hi.
I am using Visual Web Development Beta 2 from Microsoft. I am trying to pop
up a message with text "Are you sure?" with two buttons ( OK and Cancel )
when user click a delete button on my web site. After looking up and down. I
found that this site provide the help file which I think I need.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwebuipageclassregisterclientscriptblocktopic.asp

But then when I try it. it doen not work at all. No pop up dialog box at
all.

Can somebody help me? How can I pop up a simple dialog box which just show a
text message with two buttons ( Ok and Cancel ) and where I can retrieve
what the user click ?

I am surprise why Microsoft has provided so many great things in software,
but did not provide simple solution for user to just pop up a dialog box (
like in VB and C# ) in Visual Web Development.

Please help. somebody. I just can't tell my boss that my project is delayed
because Microsoft did not provide a dialog box.

Thanks
Regards
 
S

Steven Cheng[MSFT]

Hi RickDee,

Thanks for your posting. Regarding on the problem you mentioned, I think we
can just register a simple javascript code for the ASP.NET server button
control. The javascript is mainly based on the
"confirm" function which will popup a confirm dialog (with a yes and no
button) , click yes return true and click no return false. Then, we can
choose whether to allow the click event continue or cancel it.

Here is the reference on the "confirm" javascript function.
#Javascript Confirm Form Submission
http://www.shiningstar.net/articles/articles/javascript/confirmsubmit.asp?ID
=AW

Also, here is a simple demo page on how to apply it on an asp.net server
button control:

=========aspx page==============
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>

<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:Button id="btnPost" runat="server" Text="Post Back
"></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>

============code behind=========
public class confirmdialog : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnPost;

private void Page_Load(object sender, System.EventArgs e)
{
btnPost.Attributes["onclick"] =
"if(!confirm('Are you sure to post back the page?')){return false;}" +
btnPost.Attributes["onclick"];
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.btnPost.Click += new System.EventHandler(this.btnPost_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnPost_Click(object sender, System.EventArgs e)
{
Response.Write("Button is clicked at: " +
DateTime.Now.ToLongTimeString());
}
}
==============================

BTW, since I'm major on the C# code and the above code sample is also in c#
, if you need a VB.NET version , please feel free to let me know. Hope
helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
R

RickDee

Steven Cheng,

I am so glad that you reply to my posting. Thanks a lot for your help and
time.

I have give your code a try, it seems to work except a small problem. When I
right click the page that I am developing and select "View in Browser" and
then click the button. The very first time that I click, the pop up message
DID NOT pop up. Second times onwards, everything is fine. Any idea why is
that so? What else did I not setup correctly?

Regards
 
R

RickDee

Steven Cheng,

Ok. I got it working now, BUT... when I click the OK button at the pop up
message box, it will write the the page "HelloHello". Please refer to the
latest code below and the gif file that I attached.

******** Begin of code *********
public partial class test1_aspx
{
protected override void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
void InitializeComponent()
{
this.Button1.Click += new EventHandler(this.Button1_Click);
this.Load += new EventHandler(this.Page_Load);
}
void Page_Load(Object sender, EventArgs e)
{
string message = "if (confirm('Are you sure to delete?')) {} else
{return false}";
Button1.Attributes.Add("onclick", message);
}
void Button1_Click(object sender, EventArgs e)
{
Response.Write("Hello");
}
}
******** End of code *********
Please help me....
Thanks
Regards

************************ End of Mail
****************************************

RickDee said:
Steven Cheng,

I am so glad that you reply to my posting. Thanks a lot for your help and
time.

I have give your code a try, it seems to work except a small problem. When I
right click the page that I am developing and select "View in Browser" and
then click the button. The very first time that I click, the pop up message
DID NOT pop up. Second times onwards, everything is fine. Any idea why is
that so? What else did I not setup correctly?

Regards


Steven Cheng said:
Hi RickDee,

Thanks for your posting. Regarding on the problem you mentioned, I think we
can just register a simple javascript code for the ASP.NET server button
control. The javascript is mainly based on the
"confirm" function which will popup a confirm dialog (with a yes and no
button) , click yes return true and click no return false. Then, we can
choose whether to allow the click event continue or cancel it.

Here is the reference on the "confirm" javascript function.
#Javascript Confirm Form Submission
http://www.shiningstar.net/articles/articles/javascript/confirmsubmit.asp?ID
=AW

Also, here is a simple demo page on how to apply it on an asp.net server
button control:

=========aspx page==============
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>

<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:Button id="btnPost" runat="server" Text="Post Back
"></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>

============code behind=========
public class confirmdialog : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnPost;

private void Page_Load(object sender, System.EventArgs e)
{
btnPost.Attributes["onclick"] =
"if(!confirm('Are you sure to post back the page?')){return false;}" +
btnPost.Attributes["onclick"];
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.btnPost.Click += new System.EventHandler(this.btnPost_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnPost_Click(object sender, System.EventArgs e)
{
Response.Write("Button is clicked at: " +
DateTime.Now.ToLongTimeString());
}
}
==============================

BTW, since I'm major on the C# code and the above code sample is also in c#
, if you need a VB.NET version , please feel free to let me know. Hope
helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Rick,

Thanks for your reply. As for the further issue you mentioned, I've also
done some tests but seems haven't reproduct it on my side. Is there any
change in your page? Also, I've modified the code behind's Page_Load event
as following:

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
btnPost.Attributes["onclick"] = "if(!confirm('Are you sure to post back
the page?')){return false;}";
}

You may try the above method and test again.

In addition, you can also try put a normal html <input type=button..>
(html input button) and test the script code, just like :

<INPUT id="htmlButton" type="button" value="Delete"
onclick="if(!confirm('Are you sure to delete?')){return false;}" >

Put it directly in the aspx page template and run the page to see whether
this works.

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
R

RickDee

Steven Cheng,

I am so sorry. I found the problem already. With the previous mail that I
sent which has all the code inside, I found that at the event property of
the button, at the Click Event there, I actually called the Button1_Click
"again". That is why "Hello" came out twice.

I am so sorry.

Thanks
Regards.

*********************
 
S

Steven Cheng[MSFT]

Thanks for your followup Rick,

Glad that everything works ok now. Also, if you meet any other problems
later, please also feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top