DetailsView OnUpdating TextBox 不返回新文本 ASP.NET C#
DetailsView OnUpdating TextBox not returning new text ASP.NET C#
我有一个 DetailsView
和一个 SqlDataSource
,如下所示。 NotesTB
和 NameTB
在后面的代码中不为空,但不保留输入的新值。它们返回最初绑定的旧值。我在网上搜索过,找不到这个原因,这让我很困惑。
<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>
我后面的代码如下
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
}
将数据绑定到详细视图的代码,您是否使用 not is postback 条件来确保它不会在 postback 后重新绑定数据?
我有一个 DetailsView
和一个 SqlDataSource
,如下所示。 NotesTB
和 NameTB
在后面的代码中不为空,但不保留输入的新值。它们返回最初绑定的旧值。我在网上搜索过,找不到这个原因,这让我很困惑。
<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>
我后面的代码如下
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
}
将数据绑定到详细视图的代码,您是否使用 not is postback 条件来确保它不会在 postback 后重新绑定数据?