|
Subject:
|
Accessing server Controls embeded in DetailsView
|
|
Posted By:
|
KittyKat
|
Post Date:
|
1/25/2005 4:15:36 PM
|
Hi, everyone. I hope someone can help me with this.
I wanted to access the textbox control "TB_firstName", but the Page is not letting me because "TB_firstName" is embeded in the "DetailsView1". I tried doing (TextBox)DetailsView1.findControl("TB_firstName") but that does let me access it either.
The reason i am using <asp:TemplateField> instead of <asp:BoundField> because i wanted to use the <asp:RequiredFieldValidator>
Any suggestion would be appriciated.
<asp:DetailsView ID="DetailsView1" Runat="server" DataSourceID="ObjectDataSource2" DataKeyNames="ContactID"> <Fields> <asp:TemplateField HeaderText="First Name:" > <ItemTemplate> <%#DataBinder.Eval(Container.DataItem, "FirstName")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TB_firstName" Runat="server" MaxLength=15 Text='<%#DataBinder.Eval(Container.DataItem, "FirstName")%>'/> <asp:RequiredFieldValidator ID="RFV_firstName" Runat="server" ControlToValidate="TB_firstName" Text="*" ErrorMessage="First name is required."/> </EditItemTemplate> </asp:detailView>
|
|
Reply By:
|
AbhijitSinha
|
Reply Date:
|
11/10/2006 1:52:26 AM
|
Hey Buddy ... First of all... If u are using .Net 2.0, u may to shorten ur code by directly using the Eval method as shown below ..
<asp:DetailsView ID="DetailsView1" Runat="server" DataSourceID="ObjectDataSource2" DataKeyNames="ContactID"> <Fields> <asp:TemplateField HeaderText="First Name:" > <ItemTemplate> <%#Eval("FirstName")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TB_firstName" Runat="server" MaxLength=15 Text='<%#Eval("FirstName")%>'/> <asp:RequiredFieldValidator ID="RFV_firstName" Runat="server" ControlToValidate="TB_firstName" Text="*" ErrorMessage="First name is required."/> </EditItemTemplate> </asp:detailView>
Now to trap the TextBox from the Code Behind .. If you want the TextBox from a particular row ... Just enter the row index ... Else u may want to use a foreach loop or a for-next loop .. The code below tells u how to trap the value of the TextBox from each row of the detailview ...
// To get the Value of a field bound to a Grid View or DetailView ... // Run a foreach loop or a for (int i = 0; i<=DetailsView1.Rows.Count; // i++)
foreach (DetailsViewRow dtl_vwRow in DetailsView1.Rows) { string strFirstName = ((TextBox)dtl_vwRow.FindControl("TB_firstName")).Text; }
This should work ... Enjoy !!! 
|
|
Reply By:
|
kbghost
|
Reply Date:
|
12/8/2006 2:22:58 PM
|
wow, excellent post. Exactly what I was looking for AbhijitSinha. One question, how would you do it if it were boundfield? Becuase for some reason it doesn't seem to work when I just go DetailsView1.Rows[0].Cell[1].toString()
|