HighTechTalks DotNet Forums  

Fetching control values from a gridview TemplateField

ASP.net ASP.net discussions (microsoft.public.dotnet.framework.aspnet)


Discuss Fetching control values from a gridview TemplateField in the ASP.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
msch.prv@gmail.com
 
Posts: n/a

Default Fetching control values from a gridview TemplateField - 01-03-2008 , 08:51 AM






I have a gridview wich contains a mix of boundfields and
templatefields. While the boundfields can be retrieved w/o problems, I
am at a loss to get the values of the template fields. I googled
around and tried various methods to no avail.

TIA for any pointers.

Protected Sub grdOrders_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Dim rowIdx As Integer = Me.grdOrders.SelectedRow.RowIndex
Dim strTmp As String

strTmp = Me.grdOrders.Rows(rowIdx).Cells(0).Text ' first column
strTmp = Me.grdOrders.Rows(rowIdx).Cells(1).Text ' Works OK
strTmp = CType(Me.grdOrders.Rows(rowIdx).FindControl("lblUs er"),
Label).Text ' Empty
strTmp = CType(Me.grdOrders.Rows(rowIdx).Cells(2).Controls( 1),
Label).Text ' Empty
strTmp =
CType(Me.grdOrders.Rows(rowIdx).Cells(2).Controls( 1).FindControl("lblUser"),
Label).Text ' Empty
End Sub

<asp:GridView ID="grdOrders" DataKeyNames="OBaseID"
DataSourceID="sdsCustOrders"
runat="server" AllowSorting="True"
OnSelectedIndexChanged="grdOrders_SelectedIndexCha nged">
<Columns>
<asp:CommandField ShowSelectButton="True" />

<asp:BoundField DataField="UserName" HeaderText="Status" />

<asp:TemplateField HeaderText="User" >
<ItemTemplate>
<asp:Label ID="lblUser" runat="server"><%#Eval("UserName")%></
asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Reply With Quote
  #2  
Old   
Eliyahu Goldin
 
Posts: n/a

Default Re: Fetching control values from a gridview TemplateField - 01-03-2008 , 09:16 AM






Your FindControl call is fine. The problem could be that Label.Text doesn't
come back in the postback. Replace the label with a textbox and see if it
works.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


<msch.prv (AT) gmail (DOT) com> wrote

Quote:
I have a gridview wich contains a mix of boundfields and
templatefields. While the boundfields can be retrieved w/o problems, I
am at a loss to get the values of the template fields. I googled
around and tried various methods to no avail.

TIA for any pointers.

Protected Sub grdOrders_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Dim rowIdx As Integer = Me.grdOrders.SelectedRow.RowIndex
Dim strTmp As String

strTmp = Me.grdOrders.Rows(rowIdx).Cells(0).Text ' first column
strTmp = Me.grdOrders.Rows(rowIdx).Cells(1).Text ' Works OK
strTmp = CType(Me.grdOrders.Rows(rowIdx).FindControl("lblUs er"),
Label).Text ' Empty
strTmp = CType(Me.grdOrders.Rows(rowIdx).Cells(2).Controls( 1),
Label).Text ' Empty
strTmp =
CType(Me.grdOrders.Rows(rowIdx).Cells(2).Controls( 1).FindControl("lblUser"),
Label).Text ' Empty
End Sub

asp:GridView ID="grdOrders" DataKeyNames="OBaseID"
DataSourceID="sdsCustOrders"
runat="server" AllowSorting="True"
OnSelectedIndexChanged="grdOrders_SelectedIndexCha nged"
Columns
asp:CommandField ShowSelectButton="True" /

asp:BoundField DataField="UserName" HeaderText="Status" /

asp:TemplateField HeaderText="User"
ItemTemplate
asp:Label ID="lblUser" runat="server"><%#Eval("UserName")%></
asp:Label
/ItemTemplate
/asp:TemplateField
/Columns
/asp:GridView



Reply With Quote
  #3  
Old   
msch.prv@gmail.com
 
Posts: n/a

Default Re: Fetching control values from a gridview TemplateField - 01-03-2008 , 09:50 AM



Right on the bull's eye, Eliyahu! Thank you very much! I spent quite a
few hours trying to figure this out.

Happy New Year!

Reply With Quote
  #4  
Old   
msch.prv@gmail.com
 
Posts: n/a

Default Re: Fetching control values from a gridview TemplateField - 01-03-2008 , 10:00 AM



Right on the bull's eye, Eliyahu! Thank you very much!

Happy New Year.

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.