我有一个四列的gridview。前三个是绑定字段(STARTENDYEAR、ENDYEAR 和 IDENTIFIER),第三个是下拉列表(CHANGE)。下拉菜单的目的是更改 IDENTIFIER 字段的值。前三列是从一个 oracle 表中获取的。每个 STARTENDYEAR 都有一个适用于它的 IDENTIFIER 值列表。这些适用值可在不同的表中找到。这些值已成功提取并填充到下拉列表中。我正在使用带有 C# 的 ASP.NET
要求:每当用户从下拉列表中选择不同的值时,gridview 和后端表都需要使用新的 IDENTIFIER 值进行更新。
到目前为止已实施:下拉列表已填充第二个表中的适用值列表。每当用户选择一个新值时,我都可以在后端代码中捕获新值。
问题:无法使用获取的新 IDENTIFIER 值更新网格视图和数据库。我怀疑这可能是一个数据绑定问题,它阻止了 sql 更新查询在后端 C# 代码中成功触发。
ASP 代码:
<%@ Page Title="Database" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Database.aspx.cs" Inherits="About" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource2" onrowdatabound="GridView1_RowDataBound1" DataKeyNames="IDENTIFIER">
<Columns>
<asp:BoundField DataField="STARTENDYEAR" HeaderText="STARTENDYEAR"
SortExpression="STARTENDYEAR" />
<asp:BoundField DataField="ENDYEAR" HeaderText="ENDYEAR" SortExpression="ENDYEAR" />
<asp:BoundField DataField="IDENTIFIER" HeaderText="IDENTIFIER"
SortExpression="IDENTIFIER" />
<asp:TemplateField HeaderText="CHANGE" SortExpression="CHANGE">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="CODE" DataValueField="CODE"
OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:RonSql %>"
ProviderName="<%$ ConnectionStrings:RonSql.ProviderName %>"
SelectCommand="select STARTENDYEAR, ENDYEAR, IDENTIFIER from R70A109 where (STATE = :SegState) order by STARTENDYEAR, ENDYEAR">
<SelectParameters>
<asp:SessionParameter Name="SegState" SessionField="SegState" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:RonSql %>"
ProviderName="<%$ ConnectionStrings:RonSql.ProviderName %>">
</asp:SqlDataSource>
C#代码:
public partial class About : System.Web.UI.Page
{
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{ DataRowView CurrentRow = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
int StartYear = Convert.ToInt32(CurrentRow["StartYear"].ToString());
if (StartYear > 2005)
StartYear = 2005;
DataTable dtcodes = GetByState(StartYear);
DropDownList CurrentDropDown = (DropDownList)e.Row.FindControl("DropDownList1");
CurrentDropDown.DataSource = dtcodes;
CurrentDropDown.DataBind();
}
}
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList SelectedDropDown = (DropDownList)sender;
GridViewRow SelectedRow = (GridViewRow)SelectedDropDown.NamingContainer;
int StartYear = Convert.ToInt32(SelectedRow.Cells[0].Text);
int EndYear = Convert.ToInt32(SelectedRow.Cells[1].Text);
int IdentifierOld = Convert.ToInt32(SelectedRow.Cells[2].Text);
int IdentifierNew = Convert.ToInt32(SelectedDropDown.SelectedItem.Value);
if (StartYear < 2005)
{
string ConString = "Data Source=ORACLE;User ID=****;Password=****";
string CodeUpdate = "update R70A109 set IDENTIFIER = :New_Identifier where StartYear = :StartYear) AND (EndYear = :EndYear) AND (IDENTIFIER = :Old_Identifier)";
OracleConnection con = new OracleConnection(ConString);
con.Open();
OracleCommand SqlCodeUpdate = new OracleCommand(CodeUpdate, con);
SqlCodeUpdate.Parameters.Add(new OracleParameter("@New_Identifier", IdentifierNew));
SqlCodeUpdate.Parameters.Add(new OracleParameter("@StartYear", StartYear));
SqlCodeUpdate.Parameters.Add(new OracleParameter("@EndYear", EndYear));
SqlCodeUpdate.Parameters.Add(new OracleParameter("@Old_Identifier", IdentifierOld));
OracleDataAdapter DatAdpt = new OracleDataAdapter();
DatAdpt.UpdateCommand = SqlCodeUpdate;
con.Close();
}
}
}