Solved

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

Posted on 2009-05-15
5
577 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

630 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