RegisterStartupScript cannot work.

Discussion in 'ASP .Net Web Controls' started by RickDee, Oct 29, 2004.

  1. RickDee

    RickDee Guest

    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
    RickDee, Oct 29, 2004
    #1
    1. Advertising

  2. 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.)
    Steven Cheng[MSFT], Oct 29, 2004
    #2
    1. Advertising

  3. RickDee

    RickDee Guest

    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]" <> wrote in message
    news:eek:0v$...
    > 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.)
    >
    >
    >
    >
    RickDee, Oct 31, 2004
    #3
  4. RickDee

    RickDee Guest

    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" <> wrote in message
    news:...
    > 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]" <> wrote in message
    > news:eek:0v$...
    > > 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.)
    > >
    > >
    > >
    > >

    >
    >
    RickDee, Nov 1, 2004
    #4
  5. 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.)
    Steven Cheng[MSFT], Nov 1, 2004
    #5
  6. RickDee

    RickDee Guest

    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]" <> wrote in message
    news:...
    > 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.)
    >
    RickDee, Nov 1, 2004
    #6
  7. 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.)
    Steven Cheng[MSFT], Nov 1, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian W
    Replies:
    1
    Views:
    2,919
    Yan-Hong Huang[MSFT]
    Jul 4, 2003
  2. bruce barker
    Replies:
    1
    Views:
    494
    Dave Stienessen
    Jul 31, 2003
  3. Mark
    Replies:
    1
    Views:
    552
    Natty Gur
    Oct 16, 2003
  4. AmitKu
    Replies:
    3
    Views:
    915
  5. BillE
    Replies:
    10
    Views:
    1,254
    BillE
    Aug 29, 2007
Loading...

Share This Page