如何从vb.net的数据列表中的datakey字段获取值

时间:2018-09-17 19:41:31

标签: asp.net vb.net datalist

我的问题是:
当我单击“阅读更多”时,如何从 VB.Net DataKey内的DataList字段中获取值?

到目前为止,我的代码是:

<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>

当我按下“超级链接或按钮”时,我想获得一个值“ IDRewaya”

谢谢。

2 个答案:

答案 0 :(得分:0)

尝试类似这样的方法。在将按钮添加为项命令之后,将其设置为事件处理程序:

 Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs)
     If e.CommandName = "Item" Then
         Dim label1 As Label = CType(DataList1.Items(e.Item.ItemIndex).FindControl("IDRewayaLabel"), Label)
         Dim labelValue As String = label1.Text
     End If
 End Sub

答案 1 :(得分:0)

答案:

asp.net代码:

<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代码:

    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

祝大家好运。