Link to home
Start Free TrialLog in
Avatar of Shepwedd
Shepwedd

asked on

How do I change control attributes in C# asp.net?

I have written a C# website using visual studio 2008. I am using the asp.net 3.5 listview control as a frontend for my backend sql server 2005 database. I am having difficulty trying to reference the right event in the listview for disabling one of the controls in my insertitemtemplate. What I want is my textbox control in this template to become disabled when a certain value is selected from my dropdownlist. I thought it would be the itemdatabound event but when the value is selected in my dropdownlist the textbox is still enabled. Is it because I am using update panels that the page isn't getting refreshed upon selecting different dropdown choices? I tried AutoPostBack="true" but that did nothing also?
Avatar of ajolly
ajolly
Flag of India image

In your item databound:

 Control tb = find text box control;
 if (dropdown.SelectedValue='Something')
 {
      tb.Enabled=false;
 }

Set autopostback property of dropdown to true.
The listview control should be inside the update panel.
Avatar of Shepwedd
Shepwedd

ASKER

I have written the attached within my item databound event but I get the "object reference not set to an instance of an object" error on the below line?

if (ddlFrequency.SelectedValue == "5")
DropDownList ddlFrequency = (DropDownList)lvPayments.FindControl("FrequencyEditDDL");
        TextBox textBoxStopDate = (TextBox)lvPayments.FindControl("StopDateEditTextBox");
 
        if (ddlFrequency.SelectedValue == "5")
        {
            textBoxStopDate.Enabled = false;
        }

Open in new window

Sorry, I have now solved this error by using the attached code.

There is now no error but whenever I choose the value in the dropdown that should disable my textbox the textbox remains enabled as if the autopostback is not occurring?
DropDownList ddlFrequency = (DropDownList)lvPayments.InsertItem.FindControl("FrequencyInsertDDL");
        TextBox textBoxStopDate = (TextBox)lvPayments.InsertItem.FindControl("StopDateInsertTextBox");
 
        if (ddlFrequency.SelectedValue == "5")
        {
            textBoxStopDate.Enabled = false;
        }

Open in new window

Make sure:
 1. dropdown has the 'AutoPostBack' property set to 'true'
 2. The 'textbox' which is being disables is inside the update panel.
 3. Code reaches to the block:

 if (ddlFrequency.SelectedValue == "5")
 {
       textBoxStopDate.Enabled = false;
 }
ASKER CERTIFIED SOLUTION
Avatar of Shepwedd
Shepwedd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial