gpt4 book ai didi

c# - GridView 数据绑定(bind)字段 - SQL 参数

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:35 24 4
gpt4 key购买 nike

在执行期间,DataBound GridView 字段何时绑定(bind)到 SQL 参数?我有一些与它们相关的 DataBound 字段和 UpdateParameters。有时我的 UpdateCommand 会成功执行,有时我会收到空​​引用错误。

这是我的代码:

数据源

<asp:SqlDataSource ID="reconcileDataSource" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="GetReconcileItems" SelectCommandType="StoredProcedure"
UpdateCommand=" UPDATE dbo.item SET Stat=@Status WHERE ItemID = @ID;
INSERT INTO dbo.transactions(ItemID, TransactionTypeID, LocationID, DestinationID, UserNote)VALUES(@ID, '14', @LocationID, @TransferLocation, 'TEST');" >

<UpdateParameters>
<asp:Parameter Name="ID" />
<asp:Parameter Name="Status" />
<asp:Parameter Name="LocationID" />
<asp:Parameter Name="TransferLocation" />
</UpdateParameters>

</asp:SqlDataSource>

ASP.NET

    <asp:UpdatePanel ID="reconcileUpdatePanel" runat="server">
<ContentTemplate>
<asp:GridView ID="reconcileGrid" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="reconcileDataSource" OnRowUpdating="reconcileGrid_Updating" >
<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">
<EditItemTemplate>
<asp:DropDownList ID="ddlTransferLocation" runat="server"
DataSourceID="ddlTransferLocationDataSource" DataTextField="Name"
DataValueField="ID" Enabled="false" ></asp:DropDownList>
<asp:SqlDataSource
ID="ddlTransferLocationDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name] FROM [TransferLocation]">
</asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

C#

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

reconcileDataSource.UpdateParameters["Status"].DefaultValue= ddlReconcileStatus.SelectedValue;
reconcileDataSource.UpdateParameters["TransferLocation"].DefaultValue = ddlTransferLocation.SelectedValue.ToString();

}

最佳答案

我认为您不必包含 Cell[0]。此外,GridViewUpdateEventArgs 具有您可以使用的 RowIndex 属性。

这应该可行

DropDownList ddlReconcileStatus = (DropDownList)reconcileGrid.Rows[e.RowIndex].FindControl("ddlReconcileStatus");
DropDownList ddlTransferLocation = (DropDownList)reconcileGrid.Rows[e.RowIndex].FindControl("ddlTransferLocation");

关于c# - GridView 数据绑定(bind)字段 - SQL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6169337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com