0

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
    }
4
  • 1
    can you post your backend bind codes? seems you did not include bind into !IsPostback Commented Oct 5, 2017 at 5:56
  • What do you mean by backend bind codes? Commented Oct 5, 2017 at 6:12
  • 1
    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? Commented Oct 5, 2017 at 6:13
  • Good point. I forgot the !isPostback. I am at home now but I will fix that in the morning at work. If you make it an Answer I will accept it in the morning if it works. Commented Oct 5, 2017 at 6:21

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.