ASP.net Repeater individual cells

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 05:41:50

问题


Is it possible to grab the cell data from Repeater items property such as

Dim test As String = myRepeater.Items(index).DataItem(2)

where myRepeater has x rows and 10 columns and was bound from the DataTable

Seems like it should be trivial, but looks like individual cells CAN NOT be accessed. Please shed some light on this.


回答1:


Not directly -- remember, repeaters are very, very simple webcontrols and the repeater itself really has a limited idea of what is underneath it.

But you can easily get at items within. The best method is to use a control within the item to help you find stuff. EG, given this repeater "row":

<ItemTemplate>
  <asp:Button runat="server" ID="theButton" text="Click Me!" OnClick="doIt" /> <asp:TextBox id="theTextBox" value="Whateeavs" />
</ItemTemplate>

You can use the button's click handler to find other items in the row:

protected void doIt(object s, EventArgs e) {
   var c = (Control)s;
   var tbRef = c.NamingContainer.FindControl("theTextBox");
   var tb = (ITextControl)tbRef;
   doSomethingWith(tb.Text);
}

Typically much cleaner than finding things in rows using absolute positioning -- you don't have to worry about indexes and such.

PS: it has been a long time since I laid down significant web forms code, no garuntees I got the class names, etc, exactly right.



来源:https://stackoverflow.com/questions/4975140/asp-net-repeater-individual-cells

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!