Datalist in vb. Trying to get specific row or cell values ​​in datalist

I have a datalist in a vb web form.

How can I get the value in specific rows and cells of a datalist?

I could do it for a detailed view, but how to do it for a datalist ??

Below is my code for a detailed view:

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

I tried the same for the datalist, but it has no rows and cells.

This is the asp markup of my directory:

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


+4
source share
3 answers

Instead of Rows and Cells , the DataList has a property called Items , which allows you to access its collection of data-bound items:

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

If you know the row and column indices, but not the index of the element, then you can calculate the index of the element as follows:

  • If RepeatDirection - Horizontal : itemIndex = rowIndex * RepeatColumns + columnIndex
  • If RepeatDirection is Vertical : Post a comment if you need it, because it's quite complicated.
+5
source

You can access the items using

 DataListPhotoGallery.Items 

If you want to access a specific item, you need to find the object you are looking for. In this case, you want to get the value of the PhotographerPhotoIdLabel label.

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

Hope that helps

+1
source

You can use the concern data source to get the value. Please refer to the code below to get the required value.

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

Hope this helps.

0
source

Source: https://habr.com/ru/post/1393517/


All Articles