Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > RegisterStartupScript cannot work.

Reply
Thread Tools

RegisterStartupScript cannot work.

 
 
RickDee
Guest
Posts: n/a
 
      10-29-2004
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/de...us/cpref/html/
frlrfsystemwebuipageclassregisterclientscriptblock topic.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


 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      10-29-2004
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/...msubmit.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.)




 
Reply With Quote
 
 
 
 
RickDee
Guest
Posts: n/a
 
      10-31-2004
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[MSFT]" <(E-Mail Removed)> wrote in message
news0v$(E-Mail Removed)...
> 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/...msubmit.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.)
>
>
>
>



 
Reply With Quote
 
RickDee
Guest
Posts: n/a
 
      11-01-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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[MSFT]" <(E-Mail Removed)> wrote in message
> news0v$(E-Mail Removed)...
> > 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/...msubmit.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.)
> >
> >
> >
> >

>
>





 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      11-01-2004
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.)

 
Reply With Quote
 
RickDee
Guest
Posts: n/a
 
      11-01-2004
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.

*********************

"Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.)
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      11-01-2004
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.)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RegisterStartupScript Lukman ASP .Net 4 06-24-2010 09:15 AM
Using Page.RegisterStartupScript in Class Earl Teigrob ASP .Net 2 02-03-2004 09:27 PM
RegisterStartupScript() and RegisterClientScriptBlock() Mark ASP .Net 1 10-16-2003 04:23 PM
Re: client side script with RegisterStartupScript issue bruce barker ASP .Net 1 07-31-2003 08:39 PM
Re: RegisterClientScriptBlock & RegisterStartupScript revisited Brian W ASP .Net 1 07-04-2003 01:43 AM



Advertisments