GridView RowUpdating - UpdateParameters

时间:2011-05-27 14:10:18

标签: c# asp.net gridview drop-down-menu

我在设置所有更新参数时遇到问题。我的GridView中的大多数字段都是DataBound,但其中两个是下拉列表,一个指定了列表项,另一个指向另一个数据源。我想将其值设置为UpdateParameters,以便在用户单击GridView上的“Update”时发送到我的存储过程。虽然我不确定如何去做。

以下是我正在使用的代码,两个下拉列表是“ddlTransferLocation”和“ddlReconcileStatus”:

DATA SOURCE

<asp:SqlDataSource ID="reconcileDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="GetReconcileItems" SelectCommandType="StoredProcedure"
    UpdateCommand="UpdateReconcileItems" UpdateCommandType="StoredProcedure">

    <UpdateParameters>
        <asp:Parameter Name="ItemID" />
        <asp:Parameter Name="TransType" Type="String" />
        <asp:Parameter Name="LocationID" Type="String" />
        <asp:Parameter Name="Description" Type="String" />
        <asp:Parameter Name="Stat" Type="String" />
        <asp:Parameter Name="TransferLocation" Type="String" />
    </UpdateParameters>

</asp:SqlDataSource>

C#ROW UPDATING

    protected void reconcileGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      DropDownList ddlReconcileStatus = (DropDownList)reconcileGrid.Rows[e.RowIndex].Cells[0].FindControl("ddlReconcileStatus");

      reconcileDataSource.UpdateParameters["Stat"].DefaultValue = ddlReconcileStatus.SelectedValue.ToString();

    }

ASP.NET GRIDVIEW

    <asp:UpdatePanel ID="reconcileUpdatePanel" runat="server">
        <ContentTemplate>
            <asp:GridView ID="reconcileGrid" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="ItemID" DataSourceID="reconcileDataSource" OnRowUpdating="reconcileGrid_RowUpdating"  >
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ReadOnly="true" />
                    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" ReadOnly="true"/>
                    <asp:BoundField DataField="last_known_location" HeaderText="Last Known Location" SortExpression="last_known_location" ReadOnly="true" />
                    <asp:TemplateField HeaderText="Status">
                        <ItemTemplate>
                            <asp:Label ID="lblStatus" runat="server" Text='<%# evalStatus(Eval("Stat")) %>'></asp:Label></ItemTemplate><EditItemTemplate>
                            <asp:DropDownList ID="ddlReconcileStatus" runat="server" OnSelectedIndexChanged="ddlReconcileStatus_SelectedIndexChanged" AutoPostBack="true" >
                                <asp:ListItem Value="3" Text="Allocated"></asp:ListItem><asp:ListItem Value="4" Text="Transferred"></asp:ListItem></asp:DropDownList></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Transfer Location" Visible="false" >
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlTransferLocation" runat="server" 
                                DataSourceID="ddlTransferLocationDataSource" DataTextField="Name" 
                                DataValueField="ID" Enabled="true" ></asp:DropDownList>
                        <asp:SqlDataSource 
                                ID="ddlTransferLocationDataSource" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                                SelectCommand="SELECT [ID], [Name] FROM [TransferLocation]">
                        </asp:SqlDataSource>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="ItemID" HeaderText="ItemID" ReadOnly="true" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

1 个答案:

答案 0 :(得分:1)

我认为您应该使用SqlDataSource.Updating事件并执行类似

的操作
DropDownList ddlReconcileStatus = (DropDownList)reconcileGrid.Rows[reconcileGrid.EditIndex].Cells[0].FindControl("ddlReconcileStatus");
reconcileDataSource.UpdateParameters["Stat"].DefaultValue = ddlReconcileStatus.SelectedValue.ToString();