DetailsView OnUpdating TextBox不返回新文本ASP.NET C#

时间:2017-10-05 05:01:25

标签: c# asp.net detailsview

我有一个DetailsView和一个SqlDataSource,如下所示。 NotesTBNameTB在后​​面的代码中不为空,但不保留输入的新值。它们返回最初绑定的旧值。我在互联网上搜索过,找不到原因,这让我很困惑。

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

1 个答案:

答案 0 :(得分:1)

你的绑定数据到你的详细视图的代码,你是否使用not is postback条件来确保它不会在回发后重新绑定数据?