Does anyone have any idea how I can set the text of a textbox inside a DetailsView field (c#)?
I’ve tried a few variations but all return out of context and object reference not set errors.
Heres my code…
ASPX:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px">
<Fields>
<asp:TemplateField HeaderText="Header Text">
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text="test"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
CS:
TextBox txt = (TextBox)DetailsView1.FindControl("TextBox1");
txt.Text = "Text ";
Cheers
That error likely means that there is nothing in your
DetailsViewyet. I bet if you put this in your code:You’ll see that
txtis, in fact,null– this is because theFindControlmethod didn’t find anything named “TextBox1” in theDetailsView.You need to move this code to a point where you know the
DetailsViewhas been populated (if it’s bound to aDataSource, you could do this in theDataBoundevent of yourDetailsView).Also, I noticed that your
TextBoxis in anInsertItemTemplate. You won’t find thatTextBox1until you put theDetailsViewin edit mode, either by default:Or in code behind:
Hopefully that helps.