【问题标题】:how to get value from a datakey field inside datalist in vb.net如何从 vb.net 中 datalist 中的 datakey 字段中获取值
【发布时间】:2022-03-25 07:48:58
【问题描述】:

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

到目前为止我的代码是:

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

【问题讨论】:

    标签: asp.net vb.net datalist


    【解决方案1】:

    试试这样的。添加按钮作为项目命令后,设置为事件处理程序:

     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
    

    【讨论】:

      【解决方案2】:

      答案:

      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
      

      祝大家好运。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2014-03-12
        相关资源
        最近更新 更多