Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion 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 May 19th, 2011, 08:43 AM
Registered User
 
Join Date: May 2011
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default DetailView in edit mode, findcontrol returning null c#

I have a basic page with a detailsView in Edit mode and a button. When the button is pressed, I want to calculate and populate a field (textbox) in the detailsview (which is currently in edit mode). The DetailsView (and edit mode) were created via toolbox and the edit mode autogenerated ....i.e. no real customization other than linking the DetailsView to a sqldatasource.

button_clicked(object sender, EventArgs e)
{
TextBox tx=(TextBox) DetailsView1.FindControl("[db_field_name]");
}

this returns tx as a null. How can I do this? Seems like it should be an easy thing.


<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT * FROM [Table1]"></asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateEditButton="True"
AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="SqlDataSource1"
DefaultMode="Edit" Height="50px" Width="474px">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="calculate" HeaderText="calculate"
SortExpression="calculate" />
</Fields>
</asp:DetailsView>
<asp:Button ID="Button1" runat="server" onclick="button_clicked"
Text="Button" />

and code...
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void button_clicked(object sender, EventArgs e)
{
TextBox tx = (TextBox)DetailsView1.FindControl("calculate");
if (tx != null)
{
tx.Text = "testing";
}
}
}
The Following User Says Thank You to aw_wolfe For This Useful Post:





Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested controls in UserControl return null (FindControl) Lug Velez Schmitz ASP.NET 3.5 Professionals 2 September 6th, 2010 06:56 AM
Detect Edit Mode everest ASP.NET 2.0 Basics 0 December 19th, 2006 05:32 PM
e.Item.FindControl("DropdownId") Returning Null anup_daware .NET Framework 1.x 1 March 27th, 2006 03:53 PM
Datagrid - edit mode Programator Classic ASP Professional 0 September 5th, 2003 03:13 AM





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