Click here to Skip to main content
14,668,470 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a grid which has a select button to select row data.On select button click i want to open another page in another tab.
javascript function:=
function opentab() {
            var vrn = document.getElementById("<%=hdnvrno.ClientID%>").value;
            window.open("ABC.aspx?vrno=" + vrn + "", "_blank");
        }

ASPX:=
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>

<asp:HiddenField ID="hdnvrno" runat="server" Value="" />
<asp:GridView ID="GridView1" runat="server">
<columns>
<asp:TemplateField>
                                           <ItemTemplate>
                                               <asp:Button ID="btnSelect" runat="server" Text="s" Width="7px" CommandName="Select" CommandArgument='<%# Eval("VAL") %>' />
                                           </ItemTemplate>
                                       </asp:TemplateField>

CODE BEHIND :=
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          if (e.CommandName == "Select")
          {
             string  VAL= e.CommandArgument.ToString();

Button btnlink = (Button)e.CommandSource;
hdnvrno.Value = VAL;
btnlink.Attributes.Add("OnClick", "opentab();");
}
}
when i firstly click on select button the function js function is not called when i click it secondly it gets called.Whats the problem i m not able to understand.
Help me plzzz.
Posted
Updated 25-May-20 20:38pm
v3

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

When the code "btnlink.Attributes.Add("OnClick", "opentab();");" execute first time, it will register clickevent(javascript) with button click so it is not called first time. Then next time if you click button again, the registered event will be called so second time you get call for that function.

To overcome this problem, you need to register this event (i.e. btnlink.Attributes.Add("OnClick", "opentab();")) in gridview_load event. So when grid is loaded first time, the event will be registered for button click.

Ask me if you have any doubt.

Thanks,
Bh@gyesh
   
Comments
pwavell 28-May-14 5:15am
   
where and how to register this event?
Bh@gyesh 28-May-14 8:07am
   
Add RowDataBound event in grid and register buttonclick event in that.

Eg.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = (Button)e.Row.FindControl("btnSelect");
btn.Attributes.Add("OnClick", "opentab();");
}
}

and remove "btnlink.Attributes.Add("OnClick", "opentab();");" line from RowCommand event.
Bh@gyesh 31-May-14 1:02am
   
Is it working or not?
pwavell 31-May-14 2:43am
   
nope... still not working...
Bh@gyesh 2-Jun-14 0:12am
   
I created an application like your code and it is working..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100