views:

115

answers:

2

In a datalist, you usually extract row data with a FindControl on a control that is assigned a value via a databind using say, Eval.

What if in your datalist, there is no bind to an ASP.NET server control? It's in a datalist, "straight up", in say a table cell or on its own.

You can't do a FindControl, so is it possible to extract a row value if it's not bound and not a datakey?

+1  A: 

I'm assuming there is data bound to your datalist (cause it's rendering an item template). I would use something like this in the datalist's OnItemDatabound event:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { myobject obj = (myobject)e.Item.DataItem; }

Once you have cast the dataitem than u can extract the value you need.

FiveTools
I'm actually setting the data in this event and want to get data via a button click. I guess I could save the data in this event in viewstate, but I'd have 2 copies of the same data. I don't think I can avoid using runat server.
Steve
I think you may want to wire the data you need captured into the command argument of the target button - then the value is available in the OnClick method you created.
FiveTools
+1  A: 

If your value is within a DataList, you can still FindControl it, provided that it's inside a control marked with runat="server":

<asp:Literal id="ltFixedValue" runat="server">5</asp:Literal>

or even

<span id="spFixedValue" runat="server">5</span>
Dexter
I wanted to stay away from literal, but I think I have no choice. Maybe span will might be a little better.
Steve