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";
}
}
}
|