【问题标题】:Datalist in vb. Trying to get the specific values of a rows or cells in datalistvb 中的数据列表。试图获取数据列表中行或单元格的特定值
【发布时间】:2012-01-28 15:51:28
【问题描述】:

我有一个 vb 网页表单的数据列表。

如何获取数据列表的特定行和单元格中的值?

对于detailsview我可以做,但是对于datalist怎么做呢?

下面是我的代码查看详情:

 Dim selectedCommentAns As String = DetailsView.Rows(0).Cells(1).Text

我为 datalist 尝试了相同的方法,但它没有要选择的行和单元格。

这是我的数据列表的 asp 标记:

<asp:DataList ID="DataListPhotoGallery" runat="server" CellPadding="5" 
    CellSpacing="12" DataKeyField="PhotographerPhotoId" 
    DataSourceID="SqlDataSourcePhotoGallery" RepeatColumns="3">
    <ItemTemplate>
        <asp:Image ID="Image1" runat="server" BorderColor="#C7B273" 
            BorderStyle="Groove" BorderWidth="12px" Height="200px" 
            ImageUrl='<%# Eval("PhotographerPhotoImgPath", "images/UserUploadedPhoto/{0}") %>' 
            Width="220px" />
        <br />
        Photo No:&nbsp;
        <asp:Label ID="PhotographerPhotoIdLabel" runat="server" 
            Text='<%# Eval("PhotographerPhotoId") %>' />
        <br />
        Photo Description:
        <asp:Label ID="PhotographerPhotoDescLabel" runat="server" 
            Text='<%# Eval("PhotographerPhotoDesc") %>' />
        <br />
        Photo Name:
        <asp:Label ID="PhotographerPhotoImgNameLabel" runat="server" 
            Text='<%# Eval("PhotographerPhotoImgName") %>' />
        <br />
        Photographer Name:
        <asp:Label ID="PhotographerIdLabel" runat="server" 
            Text='<%# Eval("PhotographerName") %>' />
        <br />
        <asp:Button ID="AddCommentBtn" runat="server" 
            CommandArgument='<%# Eval("PhotographerPhotoId") %>' Font-Bold="True" 
            Font-Size="Medium" onclick="AddCommentBtn_Click" Text="Add Comment" />
        <asp:Button ID="Button2" runat="server" 
            CommandArgument='<%# Eval("PhotographerPhotoId") %>' Font-Bold="True" 
            Font-Size="Medium" onclick="Button2_Click" Text="Order Photo" />
        <br />


【问题讨论】:

  • 您需要从哪里获取文本?您应该显示为 DataList 的 aspx 标记。
  • 我已经用 datalist 的 aspx 标记编辑了问题。我应该如何获取“PhotographerPhotoIdLabel”的文本?

标签: asp.net vb.net visual-studio-2010


【解决方案1】:

DataList 有一个名为 Items 的属性,而不是 RowsCells,可让您访问其数据绑定项的集合:

Dim itemIndex As Integer = 9
Dim label As Label = DataListPhotoGallery.Items(itemIndex).FindControl("PhotographerPhotoIdLabel")
Dim text As String = label.Text

如果你知道行和列索引但不知道项目索引,那么你可以这样计算项目索引:

  • 如果RepeatDirectionHorizontal itemIndex = rowIndex * RepeatColumns + columnIndex
  • 如果RepeatDirectionVertical如果您需要这个,请发表评论,因为它相当复杂。

【讨论】:

    【解决方案2】:

    您可以使用

    访问项目
    DataListPhotoGallery.Items
    

    如果你想访问特定的项目,你需要找到你正在寻找的对象。在这种情况下,您想要获取一个值为 PhotographerPhotoIdLabel 标签。

    var PhotographerPhotoIdLabel = DataListPhotoGallery.Items[itemsId].FindControl("PhotographerPhotoIdLabel") as Label;
    var yourString = PhotographerPhotoIdLabel.Text;
    

    希望有帮助

    【讨论】:

      【解决方案3】:

      您可以使用关注数据源来获取值。请参考以下代码以获取所需的值。

      Dim dv As DataView
      dv = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
      selectedCommentAns = dv.Table.Rows[0][1]
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多