Solved

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

Posted on 2009-05-15
5
571 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:Shepwedd
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:ajolly
ID: 24397930
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.
0
 

Author Comment

by:Shepwedd
ID: 24410028
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

0
 

Author Comment

by:Shepwedd
ID: 24410058
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

0
 
LVL 7

Expert Comment

by:ajolly
ID: 24411210
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;
 }
0
 

Accepted Solution

by:
Shepwedd earned 0 total points
ID: 24413705
I solved this by using the OnSelectedIndexChanged event of my dropdownlist.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question