I have a DetailsView
and a SqlDataSource
as below. The NotesTB
and the NameTB
are not null in the code behind but are not retaining the new values entered in. They are returning the old values binded originally. I have searched the internet and can not find the reason for this and it is perplexing me.
<asp:DetailsView ID="PhotoDetailsDV" runat="server" Height="50px" Width="125px" DefaultMode="Edit" AutoGenerateRows="False" DataKeyNames="PhotoID" DataSourceID="XXXXXXXXXX" OnDataBound="PhotoDetailsDV_DataBound" OnItemUpdating="PhotoDetailsDV_ItemUpdating1" >
<Fields>
<asp:TemplateField HeaderText="Notes" SortExpression="Notes">
<EditItemTemplate>
<asp:TextBox ID="NotesTB" runat="server" Text='<%# Bind("Notes") %>'></asp:TextBox>
<asp:HiddenField runat="server" ID="PhotoIdHF" Value='<%# Bind("PhotoID") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="NotesLabel" runat="server" Text='<%# Bind("Notes") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="NameTB" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource runat="server" ID="XXXXX" ConnectionString="<%$ ConnectionStrings:XXXXXXXXXXXXXXX %>" SelectCommand="SELECT [Notes], Photoid, [Name] FROM [XXXXXXXX] WHERE ([FileID] = @FileID)" UpdateCommand="UPDATE [XXXXXXX] SET [Notes] = @Notes, [Name] = @Name WHERE [PhotoID] = @PhotoID">
<SelectParameters>
<asp:Parameter Name="FileID" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Notes" Type="String" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="PhotoID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
My code behind is as follows
protected void PhotoDetailsDV_ItemUpdating1(object sender, DetailsViewUpdateEventArgs e)
{
TextBox NameTB = (TextBox) PhotoDetailsDV.FindControl("NameTB");
TextBox NotesTB = (TextBox) PhotoDetailsDV.FindControl("NotesTB");
e.NewValues["Notes"] = NotesTB.Text;//here NotesTB.Text is "" even when something is entered or it is the old value
e.NewValues["Name"] = NameTB.Text;//here NameTB.Text is "" even when something is entered or it is the old value
}
the code your bind data to your detailview, do you use a not is postback condition to make sure it will not re-bind the data after postback?