Reputation: 471
I have a DetailsView
bound to an EntityDataSource
and am trying to get the values from my TextBox
es in the EditItemTemplates
.
Here is my code:
<asp:DetailsView ID="DetailsView1" DataKeyNames="Name" runat="server" AutoGenerateRows="False"
OnDataBound="DetailsView_DataBound" DataSourceID="eds2" BorderWidth="0"
OnModeChanging="OnModeChanging" AutoGenerateEditButton="true"
OnItemUpdated="DetailsView_OnItemUpdated" OnItemUpdating="DetailsView_OnItemUpdating"
EmptyDataText="N/A" OnDataBinding="DetailsView_OnDataBinding" CellPadding="0"
CellSpacing="7" GridLines="None" CssClass="Center">
<Fields>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<Fields>
</asp:DetailsView>
And the code behind:
protected void OnModeChanging(object sender, DetailsViewModeEventArgs e)
{
foreach (DetailsViewRow row in DetailsView1.Rows)
{
if (row.RowType != DataControlRowType.DataRow) continue;
foreach (DataControlFieldCell cell in row.Cells)
{
var textbox = cell.FindControl("txtName");
var textbox2 = row.FindControl("txtName");
}
}
}
textbox
and textbox2
are always null. What am I doing wrong? How can I get either the textbox or the value inside it?
Upvotes: 1
Views: 2807
Reputation: 83356
You have these textboxes declared in your edit template. These will only show up when your mode has been set to edit. I'm guessing this hasn't happened yet when the ModeChanging event is fired.
Put your code in your ModeChanged event, and check to see that you're editing.
void DetailsView1_ModeChanged(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode != DetailsViewMode.Edit)
return;
foreach (DetailsViewRow row in DetailsView1.Rows)
{
var textbox = row.FindControl("txtName");
}
}
Upvotes: 6