Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

20 Experts available now in Live!

Get 1:1 Help Now