我有一个支持更新记录的gridview。
我有一个带有Dropdownlist(ddl)的编辑模板,该模板替换了文本框。
DDL绑定到数据源,我需要附加一个值(该字段的当前值到DDL)。这使用户可以从DDL中选择当前值以及替代值。

问题是我需要绑定DDl(''),因此更新功能可以正常工作,但是在将字段的当前值附加到DDL之后,我需要对其进行绑定现在在RowDataBound事件期间发生。

简而言之;在绑定之前,我需要获取附加到DDL的字段的当前值,这样我的更新才能正常工作(否则我得到的DDL不包含谷值错误)。最早可以在gridview中检索字段值(单击编辑按钮后)以便在绑定发生之前进行一些检查的事件/事件是什么?

救命?

最佳答案

有趣的问题!您可以处理RowEditing事件,当该行进入“编辑模式”时(单击“编辑”按钮时),将触发该事件。然后,只需使用NewEditIndex属性找到要编辑的行。

因此,您的“代码隐藏”中的内容如下:

protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    // the row you're editing
    int rowToEdit = e.NewEditIndex;

    // The numeric ordinal of your column where your DropDownList is.  I just picked 5 at random
    int ddlColumnIndex = 5;

    // Get the DropDownList you're interested in modifying
    DropDownList myDDL = (DropDownList)myGridView.Rows[rowToEdit].Cells[ddlColumnIndex].FindControl("myDDL");

    // Do whatever processing you need to do here
}

关于c# - ASP.NET Gridview在绑定(bind)之前将值附加到编辑模板控件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7690100/

10-12 03:27