我有一个有四列的gridview。前三个是绑定字段(STARTENDYEAR,ENDYEAR和IDENTIFIER),第三个是下拉列表(CHANGE)。下拉列表的目的是更改IDENTIFIER字段的值。前三列是从一个oracle表中获取的。每个STARTENDYEAR都有一个适用于它的IDENTIFIER值列表。这些适用值可在不同的表格中找到。已获取这些值并已成功填充到下拉列表中。我使用ASP.NET与C#
要求:每当用户从下拉列表中选择不同的值时,都需要使用新的IDENTIFIER值更新gridview和backend表。
到目前为止已实施:已使用第二个表中的适用值列表填充了下拉列表。每当用户选择一个新值时,我就能够在后端代码中捕获新值。
问题:无法使用获取的新IDENTIFIER值更新gridview和数据库。我怀疑它可能是一个数据绑定问题,它阻止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#CODE:
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();
}
}
}
答案 0 :(得分:0)
您必须在OracleCommand对象上调用ExecuteNonQuery来执行base中的指令。
...
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)";
using (OracleConnection con = new OracleConnection(ConString))
{
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));
SqlCodeUpdate.Connection.Open();
SqlCodeUpdate.ExecuteNonQuery();
}
}
...
答案 1 :(得分:0)
找到方法 我遇到了同样的问题,我在gridview的编辑模板字段中有一个下拉列表。下拉列表中的值更改不会在更新期间影响。我通过以下步骤解决了这个问题。 1.检查'RowUpdating'事件中下拉列表返回的值。(这应该是您选择的下拉列表的确切值)
使用下拉列表中的值查找并更改字段的值。多数民众赞成...例如,行更新事件如下所示。 `protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e) {
DropDownList dr =(DropDownList)GridView1.Rows [e.RowIndex] .FindControl(“DropYearEdit”); string st = dr.SelectedValue; string sr = dr.SelectedItem.Text;
//手动更改必填字段的内部值 e.NewValues [“year”] = st; }`
现在更改的值将转到数据库..