locked
btn.attribute.add("javascript"); does'nt pop the window in 1st click RRS feed

  • Question

  • User2068616976 posted

    Hey Experts,

               I hope all of you are best of your health. i am working on dating site where i need to use to a line of code to pop the window

              I m just wondering why does it happened when i click very 1st time it does'nt open but when i click it again severl times its opens in 1st click.

              here is the line of code that i m using

            

              imgbtn1.Attributes.Add("onClick", "popNewWindow('UploadPhoto.aspx?ID=" + txtpic1.Text + "');");

               what should i do to make it open at very 1st click all the times?

               Please help me out

               Thanks

           

            

    Wednesday, November 24, 2010 11:28 PM

Answers

  • User713056278 posted

    Hi,

    I m just wondering why does it happened when i click very 1st time it does'nt open but when i click it again severl times its opens in 1st click.

              here is the line of code that i m using

            

              imgbtn1.Attributes.Add("onClick", "popNewWindow('UploadPhoto.aspx?ID=" + txtpic1.Text + "');");

              

     

    According to your code and description ,if the code is contained in page_load event, when page loads first time the textbox's text is empty,thus the value of id(Url parameter)could not get the text of textbox when you click the button to open a page(in the first time).

    If the code is contained in button click event,when the page loads,the button's attribute is not added yet(client side).Therefore when you click the button first time ,it can not open the page.

    what should i do to make it open at very 1st click all the times?

    There are several ways can achieve the goal.Please check the samples below,hope it can help you.

    1.Use OnClientClick of button to call JavaScript function.

    Code in page(.aspx):

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script language="javascript" type="text/javascript">
            function popNewWindow() {
                var id = document.getElementById("txtpic1").value;
                window.open("UploadPhoto.aspx?ID=" + id);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ImageButton ID="imgbtn1" runat="server" OnClientClick="popNewWindow();" />
            <asp:TextBox ID="txtpic1" runat="server"></asp:TextBox>
        </div>
        </form>
    </body>
    </html>


    2.Use Page.ClientScript.RegisterStartupScript method to call the JavaScript function in code behind.

     Code in page(.aspx):

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script language="javascript" type="text/javascript">
            function popNewWindow(id) {
                window.open("UploadPhoto.aspx?ID=" + id);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ImageButton ID="imgbtn1" runat="server" onclick="imgbtn1_Click" />
            <asp:TextBox ID="txtpic1" runat="server"></asp:TextBox>
        </div>
        </form>
    </body>
    </html>


    Code in page(.cs):

     protected void imgbtn1_Click(object sender, ImageClickEventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Ming", "popNewWindow('"+this.txtpic1.Text+"')", true);
        }


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 30, 2010 3:52 AM

All replies

  • User1224194097 posted

    Set OnClientClick for the ImageButton

    imgBtn1.OnClientClick = "popNewWindow('UploadPhoto.aspx?ID=" + txtpic1.Text + "');";
    Thursday, November 25, 2010 1:22 AM
  • User2068616976 posted

    thanks for your replay

    but it still takes the two click to open the pop up window Frown

    Thursday, November 25, 2010 2:00 AM
  • User1224194097 posted

    Where is that button located?

    Add that OnClientClick in either Markup or Page_Load event. 

    If that button is in gridview or so, Set OnClientClick in Row DataBound event and button should open a popup for subsequent clicks.

    Thursday, November 25, 2010 2:19 AM
  • User2068616976 posted

    its the simple image button and on the click event  i m using this line of code.

    Thursday, November 25, 2010 2:24 AM
  • User-548979073 posted

    Use the following line of code in Page_Load instead of imgbtn1_Click.

    imgbtn1.Attributes.Add("onClick", "popNewWindow('UploadPhoto.aspx?ID=" + txtpic1.Text + "');");

    Thursday, November 25, 2010 7:23 AM
  • User2068616976 posted

    Thanks For ur replay .

    it works in Mozila Perfectly but in I.E, it Does'nt even open just came to know this right now?

    what should i do now Frown ?

    Thursday, November 25, 2010 7:54 AM
  • User713056278 posted

    Hi,

    I m just wondering why does it happened when i click very 1st time it does'nt open but when i click it again severl times its opens in 1st click.

              here is the line of code that i m using

            

              imgbtn1.Attributes.Add("onClick", "popNewWindow('UploadPhoto.aspx?ID=" + txtpic1.Text + "');");

              

     

    According to your code and description ,if the code is contained in page_load event, when page loads first time the textbox's text is empty,thus the value of id(Url parameter)could not get the text of textbox when you click the button to open a page(in the first time).

    If the code is contained in button click event,when the page loads,the button's attribute is not added yet(client side).Therefore when you click the button first time ,it can not open the page.

    what should i do to make it open at very 1st click all the times?

    There are several ways can achieve the goal.Please check the samples below,hope it can help you.

    1.Use OnClientClick of button to call JavaScript function.

    Code in page(.aspx):

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script language="javascript" type="text/javascript">
            function popNewWindow() {
                var id = document.getElementById("txtpic1").value;
                window.open("UploadPhoto.aspx?ID=" + id);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ImageButton ID="imgbtn1" runat="server" OnClientClick="popNewWindow();" />
            <asp:TextBox ID="txtpic1" runat="server"></asp:TextBox>
        </div>
        </form>
    </body>
    </html>


    2.Use Page.ClientScript.RegisterStartupScript method to call the JavaScript function in code behind.

     Code in page(.aspx):

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script language="javascript" type="text/javascript">
            function popNewWindow(id) {
                window.open("UploadPhoto.aspx?ID=" + id);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ImageButton ID="imgbtn1" runat="server" onclick="imgbtn1_Click" />
            <asp:TextBox ID="txtpic1" runat="server"></asp:TextBox>
        </div>
        </form>
    </body>
    </html>


    Code in page(.cs):

     protected void imgbtn1_Click(object sender, ImageClickEventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Ming", "popNewWindow('"+this.txtpic1.Text+"')", true);
        }


     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 30, 2010 3:52 AM