1

我有一个四列的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();

    }
}

}

4

2 回答 2

0

找到了一种 方法我遇到了同样的问题,我在 gridview 的编辑模板字段中有一个下拉菜单。下拉列表中的值更改在更新期间不会影响。我已经通过以下步骤解决了它。1.检查'RowUpdating'事件中下拉菜单返回的值。(这应该是您选择的下拉菜单的确切值)

使用下拉列表中的值查找并更改字段的值。就是这样......例如,行更新事件如下......`protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {

DropDownList dr = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropYearEdit"); 字符串 st = dr.SelectedValue; 字符串 sr = dr.SelectedItem.Text;

// 手动更改必填字段的内部值 e.NewValues["year"] = st; }`

现在更改的值将进入数据库..

于 2013-11-12T05:15:43.283 回答
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();
       }

    }
...
于 2013-09-24T05:18:15.297 回答