0

I have an interesting ASP/VB.NET problem. I have a gridview where each row has its own datalist in a template column. I want to add to each item in the datalist a linkbutton that will trigger an event based on data in the datalist. But I'm not sure how to do it. It's in a project so it has the designer file which does list the gridview but not the datalist inside the gridview. When I try to add it, the listing for the datalist is removed later on when I compile.

My question is now do I get the linkbutton in the datalist in the gridview to do something?

<asp:GridView ID="gvCmteNom" runat="server" AutoGenerateColumns="False" showheader="true" HeaderStyle-BackColor="Silver" Width="1600px">
<Columns>
<asp:TemplateField HeaderText="CURRENT SERVICE">
        <ItemTemplate>
            <asp:TextBox ID="txtID" runat="server" Text='<%# Bind("NOMINEE_ID") %>' Visible="False" Width="25px" />
            <asp:DataList ID="dlCurrentCmtes" runat="server" DataSourceID="dsCurrentCmte" RepeatLayout="Flow" DataKeyField="ID" RepeatDirection="Horizontal">
                <ItemTemplate>
                    <asp:HiddenField runat="server" ID="hdnUserID" Value='<%# Eval("ID") %>' />
                    <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>' />
                    <asp:LinkButton runat="server" ID="lbIncrementYear" CommandName="IncrementYear" CommandArgument='<%# Eval("ProductID") %>' Text="Add Year" />
                </ItemTemplate>
                <SeparatorTemplate><br /><br /></SeparatorTemplate>
            </asp:DataList>
            <asp:SqlDataSource ID="dsCurrentCmte" runat="server" 
                ConnectionString="" 
                ProviderName="System.Data.SqlClient" SelectCommand="spCmteList" SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:ControlParameter ControlID="txtID" Name="ID" PropertyName="Text" Type="String" DefaultValue="" />
                </SelectParameters>
            </asp:SqlDataSource>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

Alverant
  • 229
  • 2
  • 5
  • 13

1 Answers1

1

Two options:

Option 1 Specify the ItemCommand event handler for the DataList and use that to respond to the event:

<asp:DataList OnItemCommand="Item_Command" />

Then specify that function in the code behind:

Sub Item_Command(sender As Object, e As DataListCommandEventArgs) 
     If e.CommandName = "IncrementYear" Then
         ...
     End If
End Sub

Option 2 Specify the OnClick eventhandler for the link button

<asp:LinkButton OnClick="LinkButton_Click" runat="server"/>

Then

Sub LinkButton_Click(sender As Object, e As EventArgs) 
      ...
End Sub
nickles80
  • 1,101
  • 8
  • 10
  • Option 2 gave me the best solution. But the first link of the code for that should be `Dim lbIncrementYear As LinkButton = CType(sender, LinkButton)` So you know what the variables are for the linkbutton including the commandname and commandarguement. I had to pass two pieces of data to the subroutine so I had to use both commands. It's not best practices, but sometimes you do what you must. – Alverant Oct 19 '12 at 16:34
  • Cool, glad I could help. Keep in mind that Option 1 lets you gather info about the datalist as well. The DataListCommandEventArgs contains the datalistitem as well as the command argments (name and argument) so it may be helpful to pass extra data. – nickles80 Oct 19 '12 at 17:12