【问题标题】:DropDownList Selection instead of a TextBox in AutoGenerated Gridview ColumnsDropDownList 选择而不是自动生成的 Gridview 列中的文本框
【发布时间】:2018-08-06 21:46:41
【问题描述】:

我绑定了从我的 Oracle 数据库读取的 Gridview。我有一个编辑和删除按钮。我的目标是,当我选择了某个表并按下编辑按钮时,我将前两个文本框替换为下拉列表,因此我限制用户可以选择的内容,以便使用下拉列表中的选定值将我的 SQL 语句运行到我的数据库而不是文本框值。我没有模板字段来引用我在 Gridview 中的列,因为它们是自动生成的。当我按下 Gridview 上的更新按钮时,它不会从 DropDownList 选择的值更新,而是从 Gridview 中的文本值更新。这是我的代码的 sn-p:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                     //Locate the textboxs I want to replace
                    TextBox disableJOBRun = e.Row.Cells[2].Controls[0] as TextBox;

                    TextBox disableAction = e.Row.Cells[3].Controls[0] as TextBox;
                   //Create the new DropDownLists
                    DropDownList JobRunSelection = new DropDownList();

                    DropDownList ActionSelection = new DropDownList();
                    //Bind the DropDownLists

                   JobRunSelection.DataSource = ds;
                   JobRunSelection.DataValueField = "JOBNAME";
                   JobRunSelection.DataBind();

                    ActionSelection.DataSource = dsTwo;
                    ActionSelection.DataValueField = "NAME";
                    ActionSelection.DataBind();
                    con.Close();

                     //Change Text of Textbox 
                    JobRunSelection.SelectedValue = disableJOBRun.Text;

                    ActionSelection.SelectedValue = disableAction.Text;

                    //Add the DropdownsLists to the Gridview
                    e.Row.Cells[2].Controls.Add(JobRunSelection);

                    e.Row.Cells[3].Controls.Add(ActionSelection);


                    JobRunSelection.Attributes.Add("runat", "server");

                    ActionSelection.Attributes.Add("runat", "server");


                    JobRunSelection.ID = "DropDownJobRunSelection";

                    ActionSelection.ID = "DropDownActionSelection";
                    //Hide Textboxes
                    disableJOBRun.Visible = false;
                    disableAction.Visible = false;
                  }
}  
protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)

        //Get Connection to database
        GridViewRow row = GridView1.Rows[e.RowIndex];
        String newJobID = ((TextBox)(row.Cells[2].Controls[0])).Text;
        String newAction = ((TextBox)(row.Cells[3].Controls[0])).Text;
        //Update Table
        cmd.CommandText = "UPDATE " + SelectedTable + " SET JOBID = :param2,   WHERE " + KeyName + " = " + "'" + KeyValue + "'";
        cmd.Parameters.Add(":param2", newJobID);
        cmd.Parameters.Add(":param3", newAction);

也许 Page_Load 会将文本重置为原始文本?如果是这样,我将如何实施我的 DropDownLists?感谢您的帮助,我将发布更多必要的代码。

【问题讨论】:

  • 使用模板字段。
  • 如何将数据绑定到 GridView?你在使用IsPostBack 支票吗?
  • @VDWWD 我从数据库加载读取的数据并将其加载到绑定到我的 Gridview 的数据表中。我目前没有对我的 BindGrid 方法使用 IsPostBack 检查。
  • 开始使用一个。如果不是,更新值将被旧值覆盖。
  • @wazz 我不确定如何使用模板字段,因为我的 Gridview 是自动生成的,并且我的表格中的数据决定了 Gridview 规格

标签: c# asp.net gridview


【解决方案1】:

我解决了我的问题,我已经切换了下拉列表选择值和文本框文本的变量顺序。一旦我切换它们,我的文本框就会改变它们的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-23
    • 2011-06-05
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多