编辑模式下的GridView行

我之前发过类似的问题,但我仍然有一些问题。 我使用asp.net 4和c#。

请帮助我理解当用户单击单行的EDIT按钮时,我需要更改Label uxTest的TEXT。

任何的想法? 再次感谢你们,如果看起来像一个重复的问题,那就很抱歉。


    protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { // Create a new table. DataTable taskTable = new DataTable("TaskList"); // Create the columns. taskTable.Columns.Add("Id", typeof(int)); taskTable.Columns.Add("Description", typeof(string)); taskTable.Columns.Add("IsComplete", typeof(bool) ); //Add data to the new table. for (int i = 0; i < 20; i++) { DataRow tableRow = taskTable.NewRow(); tableRow["Id"] = i; tableRow["Description"] = "Task " + i.ToString(); tableRow["IsComplete"] = false; taskTable.Rows.Add(tableRow); } //Persist the table in the Session object. Session["TaskTable"] = taskTable; //Bind data to the GridView control. BindData(); } } protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e) { TaskGridView.PageIndex = e.NewPageIndex; //Bind data to the GridView control. BindData(); } protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e) { //Set the edit index. TaskGridView.EditIndex = e.NewEditIndex; //Bind data to the GridView control. BindData(); } protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { //Reset the edit index. TaskGridView.EditIndex = -1; //Bind data to the GridView control. BindData(); } protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) { //Retrieve the table from the session object. DataTable dt = (DataTable)Session["TaskTable"]; //Update the values. GridViewRow row = TaskGridView.Rows[e.RowIndex]; dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text; dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text; dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked; //Reset the edit index. TaskGridView.EditIndex = -1; //Bind data to the GridView control. BindData(); } private void BindData() { TaskGridView.DataSource = Session["TaskTable"]; TaskGridView.DataBind(); }    GridView example   

代码解决方案

  if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit){ Label dl = (Label)e.Row.FindControl("uxLblTest");// Retrive control here } 

为什么需要更改它,您的ItemTemplate和EditItemTemplate可以拥有完全不同的数据/控件。 下面我将显示项目/编辑模板的2个不同标签(请参阅ID)。 您可以随意更改,但这是使用不同视图/编辑模式的基本方法。