Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 25th, 2005, 05:15 PM
Registered User
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to KittyKat
Default Accessing server Controls embeded in DetailsView

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>



 
Old November 10th, 2006, 02:52 AM
Registered User
 
Join Date: Nov 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to AbhijitSinha
Default



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")).T ext;
        }


This should work ... Enjoy !!! :)



 
Old December 8th, 2006, 03:22 PM
Registered User
 
Join Date: Dec 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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()






Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing DropDownList in DetailsView firblazer ASP.NET 3.5 Basics 2 June 21st, 2008 06:48 AM
Accessing Controls austinf Javascript 0 July 21st, 2006 02:42 AM
Accessing Controls in FormView cryspe ASP.NET 2.0 Basics 0 May 16th, 2006 03:59 PM
Accessing Controls in Web Form BSkelding ASP.NET 1.0 and 1.1 Professional 1 March 24th, 2005 04:48 PM
Accessing controls in another form Ramakrishna General .NET 1 September 6th, 2004 10:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.