简体   繁体   中英

After Edit Linkbutton is pressed only certain columns can be Updated with the rest set to null

When you select the "Edit" button the first two columns: idt and datetime allows you to update.

(as long as the Primary Key is Unique)

The last three columns:col1, col2, and col3 come up null no matter what you enter before clicking "Update".

Here is the error code after entering "Update":

System.FormatException: Input string was not in a correct format.

Here is my code:

 <asp:GridView ID="GridView1" 
    runat="server"
    DataSourceID="SqlDataSource1"
    AutoGenerateColumns="false"
    DataKeyNames="idt" 
    AutoGenerateEditButton="true" 
    AutoGenerateDeleteButton="true"
    showfooter="true">
    <Columns>
        <asp:BoundField DataField="idt" HeaderText="idt" Readonly="true" SortExpression="idt" />
        <asp:BoundField DataField="datetime" HeaderText="datetime" SortExpression="datetime" />
        <asp:TemplateField SortExpression="col1" HeaderText="col1">
                <ItemTemplate>
                    <asp:TextBox ID="txt1" runat="server" Text='<%# Eval("col1") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:textbox id="col1TextBox" text='<%#Eval("col1")%>' runat="server" />
                </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField SortExpression="col2" HeaderText="col2">
                <ItemTemplate>
                    <asp:TextBox ID="txt2" runat="server" Text='<%# Eval("col2") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:textbox id="col2TextBox" text='<%#Eval("col2")%>' runat="server" />
                </EditItemTemplate>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField SortExpression="col3" HeaderText="col3">
                <ItemTemplate>
                    <asp:TextBox ID="txt3" runat="server" Text='<%# Eval("col3") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:textbox id="col3TextBox" text='<%#Eval("col3")%>' runat="server" />
                </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource
    id="SqlDataSource1"
    ConnectionString="<%$ ConnectionStrings:Total %>"
    SelectCommand="SELECT * FROM [test];"
    UpdateCommand="UPDATE [test] SET [datetime] = @datetime, [col1] = @col1, [col2] = @col2, [col3] = @col3 WHERE [idt] = @idt;"
    DeleteCommand="DELETE FROM [test] WHERE [idt] = @idt;"
    runat="server">
    <DeleteParameters>
        <asp:Parameter Name="idt" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="idt" Type="Int32" />
        <asp:Parameter Name="datetime" Type="String" />
        <asp:Parameter Name="col1" Type="Int32" />
        <asp:Parameter Name="col2" Type="Int32" />
        <asp:Parameter Name="col3" Type="Int32" />
    </UpdateParameters>    </asp:SqlDataSource>

I think Bind should be used instead of Eval. As Bind is for two way databinding and used when you want to edit or insert.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM