我绑定了从Oracle数据库读取的Gridview。我有一个编辑和删除按钮。我的目标是,当我选择某个表并按下编辑按钮时,将前两个文本框替换为下拉列表,以便限制用户可以选择的内容,以便使用下拉列表中的selectedValue将SQL语句运行到数据库中而不是文本框值。我没有TemplateFields可以引用Gridview中的列,因为它们是自动生成的。当我在Gridview上按下更新按钮时,它不会从DropDownList选定的值更新,而是从Gridview中的文本值更新。这是我的代码的片段:
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?感谢您的帮助,我将发布更多必要的代码。
答案 0 :(得分:0)
我解决了我的问题,我已经切换了Dropdownlist选择值和文本框文本的变量顺序。切换它们后,我的文本框会更改它们的值。