views:

291

answers:

1

Hello friends, I have a formview on my aspx page containing various controls arranged using table. There is a DDL "cboClients" which i need to enable or disabled depending upon role within Edit mode.

The problem here is that i am not able to get that control using FindControl() method.

I have tried following code -

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

Even I ave used the DataBound event of the same control -

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;
            }
        }
    }

But this databound event occurs only once, but not when formview mode is changed.

Can anyone provide me proper solution?

Thanks for sharing your time.

+1  A: 

Try the ModeChanged event. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.modechanged.aspx

update..

Try this

DropDownList ddl = FormView1.FindControl("cboClients") as DropDownList;
if (ddl != null) {
  ddl.Enabled=false;        
}
Raj Kaimal
Thanks Raj, but i have also used that.
IrfanRaza
Thanks Raj, that worked for me. But can u tell what is the difference between casting and using "as" operator?
IrfanRaza
The as operator is like a cast except that it yields null on conversion failure instead of raising an exception.http://msdn.microsoft.com/en-us/library/cscsdfbt(vs.71).aspx
Raj Kaimal