Solved

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

Posted on 2009-05-15
5
568 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 IIS and Windows Authentication to SQL 21 54
Problem with Filesystemobject or .NET version 3 35
Resolve Dependency Issues 4 47
SQL Exceptions 3 39
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:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now