0
votes

hi i have a gridview in my asp.net webpage that is bounded after a user selects 3 dropdown list.
dropdown list's auto postbacks are set true , now my problem is when i put a button , imagebutton or linkbutton in my gridview ItemTemplate the onclick event dosent fire ! here is my code

    <asp:GridView ID="GridView1" runat="server"   Width="90%" Height="100%" OnRowCommand="GridView1_RowCommand"  OnPageIndexChanging="GridView1_PageIndexChanging" style="margin:20px; vertical-align:top;"  PageSize="10" AllowPaging="True" 
        AllowSorting="True"  
        AutoGenerateColumns="False" DataKeyNames="WFStudentID" ShowHeader="true" BorderWidth="1px">

        <Columns>
          <asp:TemplateField  ShowHeader="true">
            <ItemTemplate >
              <tr>
                <td>
                  <asp:Label ID="Label1" ForeColor="Silver" Font-Size="Medium" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </td>
                <td>
                  <asp:Label ID="Label2" runat="server" ForeColor="Silver" Font-Size="Medium"  Text='<%# Bind("Family") %>'></asp:Label>
                </td>
                <td>
                  <asp:HyperLink ID="HyperLink1" ForeColor="Silver" Font-Size="Medium" Target="_blank" NavigateUrl='<%# Bind("WFStudentFilePath") %>' runat="server">دانلود</asp:HyperLink>
                </td>
                <td>
                  <asp:Label ID="Label7" runat="server" ForeColor="Silver" Font-Size="Medium"  Text='<%# Bind("WFStudentDate") %>'></asp:Label>
                </td>
                <td>
                  <asp:Button ID="Deny" Text="deny"  OnClick="Deny_Click1"       runat="server" />
                </td>
                <td>
                  <asp:ImageButton ID="accept" OnClick="accept_Click"  runat="server" />
                </td>
                <td>
                  <asp:TextBox ID="des" TextMode="MultiLine" Height="100px" Width="200px" runat="server"></asp:TextBox>
                </td>
              </tr>
              <br />
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
    </asp:GridView>  



protected void Deny_Click1(object sender, EventArgs e)
{
  Response.Redirect("home.aspx");
}
3
Have you checked out the answer here? stackoverflow.com/a/11455262/382214 - ewitkows
Just to double check... have you put a breakpoint at the very top of Deny_Click1. You're swallowing potencial exceptions so maybe the problem is that there is an exception but you didn't realize about it. - Claudio Redi
@ClaudioRedi i put Response.redirect("home.aspx") it did not fire!! - Mohammad Olfatmiri
@ewitkows thats not my problem.. - Mohammad Olfatmiri
Remove the Onclick event on your button and set the commandname and argument there too. - Ahmed ilyas

3 Answers

2
votes

It is probably firing, but it will throw an exception (which your catch block ignores). The exception is because of this line:

LinkButton Link = (LinkButton)sender;

The sender is a Button, not a LinkButton, so the cast is not valid and an exception will be thrown.

0
votes

Buttons inside GridView do not respond to direct events. They have to go through GridView's RowCommand event implementation. I've seen that you have implemented OnRowCommand="GridView1_RowCommand". So now you need make following change to image button:

<asp:ImageButton ID="accept"  runat="server" CommandName="Accept" />

Now look for "Accept" command name in your GridView1_RowCommand event handler.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "Accept")
  {
    // Your code goes here
  }

}

I hope this helps.