【问题标题】:How to access drop down list from EditItemTemplate of FormView如何从 FormView 的 EditItemTemplate 访问下拉列表
【发布时间】:2010-05-09 04:44:03
【问题描述】:

我的 aspx 页面上有一个表单视图,其中包含使用表格排列的各种控件。 我需要根据编辑模式中的角色启用或禁用 DDL“cboClients”。

这里的问题是我无法使用 FindControl() 方法获得该控件。

我已经尝试过以下代码 -

     DropDownList ddl = null;
       if (FormView1.Row != null)
        {
            ddl = (DropDownList)FormView1.Row.FindControl("cboClients");
            ddl.Enabled=false;        
}

即使我使用过同一个控件的 DataBound 事件 -

protected void cboClients_DataBound(object sender, EventArgs e)
    {
        if (FormView1.CurrentMode == FormViewMode.Edit)
        {
            if ((Session["RoleName"].ToString().Equals("Clients")) || (Session["RoleName"].ToString().Equals("Suppliers")))
            {
                DropDownList ddl = (DropDownList)sender;
                ddl.Enabled = false;
            }
        }
    }

但是这个数据绑定事件只发生一次,而不是在formview模式改变时发生。

谁能给我合适的解决方案?

感谢您分享您的时间。

【问题讨论】:

    标签: asp.net formview edititemtemplate


    【解决方案1】:

    尝试 ModeChanged 事件。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.modechanged.aspx

    更新..

    试试这个

    DropDownList ddl = FormView1.FindControl("cboClients") as DropDownList;
    if (ddl != null) {
      ddl.Enabled=false;        
    }
    

    【讨论】:

    • 谢谢 Raj,但我也用过。
    • 感谢 Raj,这对我有用。但是你能说出强制转换和使用“as”运算符有什么区别吗?
    • as 运算符类似于强制转换,只是它在转换失败时产生 null 而不是引发异常。 msdn.microsoft.com/en-us/library/cscsdfbt(vs.71).aspx
    猜你喜欢
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多