Monday, May 5, 2008

Gridview Select Problem with ItemTemplate

Problem: I have a gridview where instead of using the asp:boundfield, I have used TemplateField with custom ItemTemplateField to populate the data by
<%
# Eval("FieldName")%>

<asp:TemplateField >
<ItemTemplate />
</asp:TemplateField>

Problem with this was that I wasn't able to get the cells[0].Text with the ItemTemplate.
So, here is my solution

<asp:TemplateField SortExpression="Name">

<HeaderTemplate>NameHeaderTemplate>

<ItemStyle Width="200px" />

<ItemTemplate>

<asp:Label runat="server" ID="lblName_InGridView" Text=' <%# Eval("Name")%>' />

ItemTemplate>

<EditItemTemplate><asp:TextBox CssClass="fakeInput" TextMode="SingleLine" Text='<%# Eval("Name")%>' Width="200px" runat="server" ID="txtName1" />EditItemTemplate>

<asp:TemplateField>

I have created a label instead the ItemTemplate.

then on RowCommand(sender, e)
you can

Label lblDepartmentID_InGridView = (Label)selectedRow.Cells[2].Controls[1];

string temp = lblDepartmentID_InGridView.Text;

This how you get the value of the this particular value.

This isn't very strong solution but it is. I wasn't able to find a more elegance solution at this time. The better solution may exist.


Keywords: [GridView Select BoundField ItemTemplate]


1 comment:

bellcristi said...

Hi,

Another question related to gridView:
How can I get the value of a Cell that is hidden. I mean I have the id, I select "visible=false" in Edit Columns. Since it is on the position 0 I have indeed Cells[0] - which is unfortunately void..

Any ideas?