Setting Click Event for LinkButton

2.4k views Asked by At

I have a LinkButton within a Datagrid. I am having trouble setting a Click event for it. I will add the OnClick="Remove_Click" attribute in the HTML. But when I go to write the actual event, VB isn't finding the LinkButton. Therefore nothing happens.

Here is the code for it.

<asp:DataGrid ID="StandardsDataGrid" runat="server" ShowHeader="false" 
    ShowFooter="false"
    AutoGenerateColumns="false" CellPadding="2" CellSpacing="0" 
    ItemStyle-VerticalAlign="middle"
    DataKeyField="Id" Width="100%" BorderColor="#000">
    <Columns>
        <asp:TemplateColumn>
            <ItemTemplate>

            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:LinkButton ID="Remove" OnClick="Removed_Click" runat="server"
                    Text="<img src='../images/btnDelete.gif' border='0'>" />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>

Any ideas on why VB isn't recognizing it? Or is there a different way I should go about performing a click event?

1

There are 1 answers

0
Luke On BEST ANSWER

Check this out, took me excactly one second to find it!

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemtemplate.aspx

You have to use

<asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" />

and handle the Command Event of the grid

this is a common usage:

<asp:DataGrid id="DataGrid1" 
   runat="server" CssClass="grid" 
   AutoGenerateColumns="False">
   <Columns>
      <asp:EditCommandColumn 
         EditText="Edit" CancelText="Cancel" 
         UpdateText="Update" />
      <asp:BoundColumn 
         DataField="OrderID" ReadOnly="True" 
         HeaderText="Order ID" />
      <asp:BoundColumn 
         DataField="ShipName" HeaderText="Ship to" 
         ReadOnly="True" />
      <asp:BoundColumn 
         DataField="ShipCountry" HeaderText="Country" 
         ReadOnly="True" />
      <asp:TemplateColumn HeaderText="Ship Method">
         <ItemTemplate>
            <%#Container.DataItem("ShipVia")%>
         </ItemTemplate>
         <EditItemTemplate>
            <asp:DropDownList runat="server" ID="Dropdownlist1"/>
         </EditItemTemplate>
      </asp:TemplateColumn>
   </Columns>
</asp:DataGrid>

And here how you can handle it

Private Sub DataGrid1_UpdateCommand(ByVal source As Object, _
   ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
   Handles DataGrid1.UpdateCommand

End Sub