1

我在每个地方都用谷歌搜索过,但没有明确的答案,我正在尝试使用以下代码使用详细视图更新记录:

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" 
        BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" 
        CellPadding="2" DataKeyNames="Rec_ID" DataSourceID="ContactsMasterDS" 
        ForeColor="Black" GridLines="None" PageSize="3">
        <AlternatingRowStyle BackColor="PaleGoldenrod" />
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            <asp:BoundField DataField="Full_Name" HeaderText="Full_Name" SortExpression="Full_Name" />
            <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
            <asp:BoundField DataField="AgeGroup" HeaderText="AgeGroup" SortExpression="AgeGroup" />
            <asp:BoundField DataField="Nationality" HeaderText="Nationality" SortExpression="Nationality" />
            <asp:BoundField DataField="Occupation" HeaderText="Occupation" SortExpression="Occupation" />
            <asp:BoundField DataField="Resident" HeaderText="Resident" SortExpression="Resident" />
        </Columns>
        <FooterStyle BackColor="Tan" />
        <HeaderStyle BackColor="Tan" Font-Bold="True" />
        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        <SortedAscendingCellStyle BackColor="#FAFAE7" />
        <SortedAscendingHeaderStyle BackColor="#DAC09E" />
        <SortedDescendingCellStyle BackColor="#E1DB9C" />
        <SortedDescendingHeaderStyle BackColor="#C2A47B" />
    </asp:GridView>
    <asp:SqlDataSource ID="ContactsMasterDS" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        SelectCommand="Select * from Contacts"></asp:SqlDataSource>
    <br />
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        DataKeyNames="Rec_ID" DataSourceID="ContactsDetailsDS" Height="50px" 
        Width="545px" onitemupdated="DetailsView1_ItemUpdated">
        <Fields>
            <asp:BoundField DataField="Rec_ID" HeaderText="Rec_ID" ReadOnly="True" SortExpression="Rec_ID" />
            <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
            <asp:BoundField DataField="AgeGroup" HeaderText="AgeGroup" SortExpression="AgeGroup" />
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            <asp:BoundField DataField="Full_Name" HeaderText="Full_Name" SortExpression="Full_Name" />
            <asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
            <asp:BoundField DataField="Phone_No" HeaderText="Phone_No" SortExpression="Phone_No" />
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
            <asp:BoundField DataField="Nationality" HeaderText="Nationality" SortExpression="Nationality" />
            <asp:BoundField DataField="Account_No" HeaderText="Account_No" SortExpression="Account_No" />
            <asp:BoundField DataField="Occupation" HeaderText="Occupation" SortExpression="Occupation" />
            <asp:BoundField DataField="Resident" HeaderText="Resident" SortExpression="Resident" />
            <asp:BoundField DataField="Room_No" HeaderText="Room_No" SortExpression="Room_No" />
            <asp:BoundField DataField="Last_Branch" HeaderText="Last_Branch" SortExpression="Last_Branch" />
            <asp:BoundField DataField="Last_Date" HeaderText="Last_Date" SortExpression="Last_Date" />
            <asp:BoundField DataField="Last_Time" HeaderText="Last_Time" SortExpression="Last_Time" />
            <asp:CheckBoxField DataField="isComplete" HeaderText="isComplete" SortExpression="isComplete" />
            <asp:CommandField ShowEditButton="True" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="ContactsDetailsDS" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        SelectCommand="Select * from Contacts where [Rec_ID] = @Rec_ID" 
        UpdateCommand="Update Contacts Set Room_No=@Room_No Where Rec_ID=@Rec_ID" >
        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1" Name="Rec_ID" PropertyName="SelectedValue" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Room_No" Type="String" />
            <asp:Parameter Name="Rec_ID" Type="String" DefaultValue="0" />
        </UpdateParameters>
    </asp:SqlDataSource>

当我将 where 子句更改为“Where Rec_ID = 2”时,它可以工作,但是当使用参数时,它不会进行所需的更新,请提供任何帮助

4

2 回答 2

1
    private void OnDetailsViewItemUpdating(object sender, DetailsViewUpdateEventArgs e) {
        if (String.Equals((string)e.NewValues["firstName"], "john", StringComparison.OrdinalIgnoreCase)) {
            // "John" is not a valid name, so change it to "Steve":
            e.NewValues["firstName"] = "Steve";
        }
        if (String.Equals((string)e.NewValues["lastName"], "doe", StringComparison.OrdinalIgnoreCase)) {
            // If "Doe" is the last name, cancel the whole operation
            e.Cancel = true;
        }

}

您必须写“OnItemUpdated”而不是 onitemupdated。检查案例。

希望这对您有所帮助。DetailsView 控件的 ItemUpdating 事件的参数包含原始数据(如果可用)以及用户键入的新数据。下面是一个如何检查数据并可选择修改数据的示例

于 2013-10-23T05:12:17.863 回答
0

I found the solution as follows:

1- in the where clause: Change the Rec_ID into any other name like ID for example.

before: UpdateCommand="Update Contacts Set Room_No=@Room_No Where [Rec_ID]=@Rec_ID">

after : UpdateCommand="Update Contacts Set Room_No=@Room_No Where [Rec_ID]=@ID">

2- in the UpdateParameters get the value of ID as such:

before: <asp:Parameter Name="Rec_ID" Type="Int32" />

after: <asp:ControlParameter ControlID="DetailsView1" Name="ID" PropertyName="SelectedValue" />

this will allow you to keep the Rec_ID as read only or even remove hide it from the DetailsView

Thanks for all of you

于 2013-10-23T09:40:05.660 回答