Question

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>

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top