[SOLVED] how to get value from a datakey field inside datalist in vb.net

Issue

My question is:
How to get the value from the DataKey field inside the DataList in VB.Net when I click on "Read more"?

My Code so far is:

<asp:DataList ID="DataList1" runat="server" BackColor="White" 
            BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
            DataKeyField="IDRewaya" DataSourceID="SqlDataSource1" Font-Bold="False" 
            Font-Italic="False" Font-Overline="False" Font-Strikeout="False" 
            Font-Underline="False" ForeColor="Black" GridLines="Horizontal" 
            HorizontalAlign="Center" RepeatColumns="3" RepeatDirection="Horizontal" 
            Width="800px" CellSpacing="2">
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            <ItemTemplate>
                IDRewaya:
                <asp:Label ID="IDRewayaLabel" runat="server" Text='<%# Eval("IDRewaya") %>'/>
                <br />
                Pic:
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Pic") %>'  style="max-height: 200px;max-width: 200px;" />
                <%--<asp:Label ID="PicLabel" runat="server" Text='<%# Eval("Pic") %>' />--%>
                <br />
                NameRewaya:
                <asp:Label ID="NameRewayaLabel" runat="server" 
                    Text='<%# Eval("NameRewaya") %>' />
                <br />
                <asp:HyperLink ID="HyperLink1" runat="server">Read More</asp:HyperLink>
                &nbsp;<br />
            </ItemTemplate>
            <SelectedItemStyle BackColor="#FFFFCC" Font-Bold="True" ForeColor="White" 
                Font-Italic="False" Font-Overline="False" Font-Strikeout="False" 
                Font-Underline="False" />
        </asp:DataList>

I want to get a value "IDRewaya" when I press "Hyper link or Button"

Solution

The answer:

asp.net code:

<asp:DataList ID="DataList1" runat="server" DataKeyField="IDRewaya" 
    DataSourceID="SqlDataSource1" RepeatColumns="3" 
    RepeatDirection="Horizontal" onitemcommand="DataList1_ItemCommand">
    <ItemTemplate>
        <table border="1" class="style1">
            <tr>
                <td bgcolor="#66FF66" class="style2" style="text-align: center">
                    <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Large" 
                        Text='<%# Eval("NameRewaya") %>'></asp:Label>
                </td>
            </tr>
            <tr>
                <td bgcolor="#FFFF99" class="style3" style="text-align: center">
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("Type") %>'></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style4" style="text-align: center">
                    <asp:Image ID="Image1" runat="server" Height="200px" 
                        ImageUrl='<%# Eval("Pic") %>' Width="200px" />
                </td>
            </tr>
            <tr>
                <td class="style5" style="text-align: center">
                    <asp:Button ID="Button1" runat="server" BackColor="#6699FF" 
                        CommandName="Viewdetails" CommandArgument='<%# Eval("IDRewaya") %>' Font-Bold="True" ForeColor="White" 
                        Text="View Details" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
        <br />
    </ItemTemplate>
</asp:DataList>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Rewayat]"></asp:SqlDataSource>

vb.net code:

    Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.ItemCommand
    If (e.CommandName = "Viewdetails") Then
        Response.Redirect("Default3.aspx?id=" + e.CommandArgument.ToString)
    End If
End Sub

Good luck for everyone.

Answered By – N.Odeh

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *