Drop down list control values OK but not showing selected value on UI

 Hi,

I am having trouble with the following:

ddlIndustry.Items.FindByText(oReader["industry"].ToString()).Selected = true;

 I am trying to get the ddlIndustry drop down list to bind to a specific product industry got from a database like say "Agriculture". Even when i assign a hardwired and existent value like "Agriculture" as from the database,  the Selected value or text does change from initial option from the list with (this I verified in Debug mode) but when the page is generated, the Drop down list maintains value 0 and text "--select one --".

Is there something i am missing here ?

TIA,

Tuka

0
tuka
1/20/2007 10:48:17 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

4 Replies
902 Views

Similar Articles

[PageSpeed] 11

Dear,

first check if oReader["industry"].ToString() is returning a value, if it;s so try the below code

string id = oReader["industry"].ToString() ;

ListItem item =

new ListItem(id,id);    // the first id is the text the second is the value considering that your dropdown list text and value for each item are identical

int i = ListBox1.Items.IndexOf(item);

ListBox1.Items[i].Selected = true;

happy Coding

 


Haissam Abdul Malak
MCAD.NET
| Blog |
0
Haissam
1/21/2007 12:02:30 AM
sorry just replace the ListBox1 by ddlIndustry, i have tested it on my pc using a listbox it's the same for the drop down list
Haissam Abdul Malak
MCAD.NET
| Blog |
0
Haissam
1/21/2007 12:12:46 AM

Hi Haissam,

 Thanks for your reply.

A little more testing and your code helped me identify where the problem is and I hope a final solution.

Instead of exactly placing your code as was presented, I placed something very similar that works on a simple test web form:

ddlIndustry.SelectedIndex = ddlIndustry.Items.IndexOf(ddlIndustry.Items.FindByText(oReader["industry"].ToString()));

This has the advantage that it does not require me to know the value of the list item, the text is sufficient. ( And I didnt want to alter any existing sql to get that stuff.... Wink 

 Now here is where it gets tricky, I noticed that while on a simple page (no inclusions i.e @ Register tag Prefixes ) this worked fine, if I included the above on my complex page (with 5 inclusions) , it did not work.  Disabling the inclusions one at a time and testing, I discovered that the one that represents the header of my page was the culprit. Simply excluding it allowed the above code to work.

So I went to work in what part of the controls was causing problems and I figured that it was the Page.Databind() I includede in each of the controls.

Now I got everything to work (I think) becasue I removed Page.Databind from each one of those controls. I have one C# function that is bound and mixed in with ASP.NET code within each of the ascx files that are included on my application pages. Such as: 

<%# SetImagePath() %>

  I do have this throbbing question though, removing Page.Databind from each of the ascx pages, do I then have to ALWAYS do a Page.Databind() on the main page / ASPX form for the include to work properly ?

I see that I can call just Databind() on my ascx controls in the code behind -> is that the same as Page.Databind() ?

Thanks for any comments and explanation.

Tuka

0
tuka
1/21/2007 2:09:35 AM
tuka:

Now here is where it gets tricky, I noticed that while on a simple page (no inclusions i.e @ Register tag Prefixes ) this worked fine, if I included the above on my complex page (with 5 inclusions) , it did not work. Disabling the inclusions one at a time and testing, I discovered that the one that represents the header of my page was the culprit. Simply excluding it allowed the above code to work.

So I went to work in what part of the controls was causing problems and I figured that it was the Page.Databind() I includede in each of the controls.

Now I got everything to work (I think) becasue I removed Page.Databind from each one of those controls. I have one C# function that is bound and mixed in with ASP.NET code within each of the ascx files that are included on my application pages. Such as:

<%# SetImagePath() %>

I do have this throbbing question though, removing Page.Databind from each of the ascx pages, do I then have to ALWAYS do a Page.Databind() on the main page / ASPX form for the include to work properly ?

I see that I can call just Databind() on my ascx controls in the code behind -> is that the same as Page.Databind() ?

Hi, the databind method will cause the data source to be binded to the control and its child controls. So, invoke the page.Databind() in the main page is enough, it will incurrs the user controls' databind to be called.
And your problem is actually about databind being called multiple times.
Since the Page property in a user control references to the page that the control resides in. In your pormer code, the databind of the page is called twice.
What you need is just to call databind in main page.
Hope it helps.

0
Raymond
1/23/2007 1:45:45 AM
Reply:

Similar Artilces:

Drop Down List Web User Control: How to access the selected value
With a texbox user control, the method for accessing text keyed in doesn't work for accessing the selection made on a drop down menu. This is because an event is fired with the drop down listbox, as compared to the textbox where no event is fired. Here's how to read the selection made in the drop down list control and make it available to the page containing it. It works for me. -- In the .aspx page (user_control_exp.aspx), hosting the Web user control (a drop down list box): -- <code> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Hand...

Drop down list in Form view returns error when value in bound field (data binding
I get the following error when I click edit to edit the details in the web form:'ddlUserRestrictionType' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value  I would like it to accept any value which currently exists in the database. Is this easily achievable? With what you've posted, all I can tell is that you are trying to set the SelectedValue of a ddl to a value that does not exist in the list.  It would be helpful if you can post some code and the expected values of this control.~ Timwww.bucketo...

when loading a previously created record on to form, some controls are missing values(controls are depended on values selected in other controls)
Hello - I have created a page for Editing records.  this page will open up a chosen record and allow user to edit the data.On the form, I have 3 drop down lists.   The second dropdown list should fill values depending on value chosen in first dropdown list.  The third dropdown list should show/fill values depending on value chosen in second dropdown list.So taking this into count, i have several records with various data.  When I open-up/load a record in Edit form, only the first dropdown list has a value selected that was the pre-existing va...

HTML.select (UI helper) how to build a select list with value not equal to the display value
Hi Guys, I have a newbie question on HTML.select (UI helper). One of the tutorial I have come across shows the following: "string [] SelectList = new string[]{'Apple', 'Orange','Mango','Banana'};" "Html.Select('FruitList',SelectList)" it will give me:value = "Apple"  "Apple " However, I would like to have the following instead: value = "1"  "Apple " What should I do on the VIEW in order to achieve this? thx, George what you need to do is create a collection to use for your data sou...

Dropdownlist selected value passes first value in list no matter what value is selected. why ?
 HI . I have a dropdownlist. Databinds ok but when I must pass a value as a Selected value it always passes the first value in the list even if I select another value. I tried to set the ddl to enableviewstate false but it doesn't change anything. Any ideas ? Code below, thanksLoading the ddl:       productCategories = CatalogAdmin.Admin_Product_Categories_NotIn(productId);        for (int i = 0; i < productCategories.Rows.Count; i++)        {       &...

Selecting a Value in a Drop Down List Based on the Value of a Textbox
I have a formview control (.NET 2.0/VS2005) in which users enter company contact information.  When a user enters a postal code in the appropriate text box, the territory for that company needs to be calculated and displayed in a territory drop down list. I know how to calculate and retrieve the territory from my SQL Server database, but I can't seem to nail down how to trigger the code and don't want the user to have to click a button. I tried using the OnTextChanged event, but that doesn't seem to be what I want because I need this calculation to be performed pri...

How to show value in textbox from selected value in Dropdown list
I have two text box, txtbox1 should be from two field for example i have one dropdownlist where i select item and another txtbox2 i want to put value, when i first time to write manually value in txtbox2 in and selected item from ddl i want to result in concatenate from in txtbox1. for example  I haveif not me.ispostback thenDim cm as new sqlcommand("Select AccMjrid, Names from Accgrp", me.sqlconnection1.open())me.sqldataAdapter1.fill(Dataset1, "Accgrp") Me.cmbAccMajID.DataSource = Me.DataSet11.Tables("AccMajor") Me.cmbAccMajID.DataTextField = "AccName" Me.cmbAccMajID.DataValueField ...

How to get the list of second drop-down-list based on value selected in the first drop-down-list
I have two drop-down-lists, the second one depends on the value chosen in the first drop-down-list.  In the first drop-down-list, I have AutoPostBack = "true" and OnSelectedIndexChanged = "SelectedIndexChanged" In the selectedIndexChanged method, I do the following: 1    protected void SelectedIndexChanged(object sender, EventArgs e)2        {3            if (IsPostBack)4            {5      &nbs...

Select value from drop down then other fields shows value in textfield depends on the dropdown.
 Hi,em new in VB.NET & develop a web based transaction which is almost 50% complete.my problem is that i have a form of Item properties, which consist on a following fields,1. Item2. Unit3. Old Price4. New Price my client wants that if user select Item from the dropdown then other fields shows value automaticlly on the textfields which is depend on the item name.for example:if user select Colgate from the dropdown then Unit, Old Price and New Price shows in textfeilds seperately.  if u have any code and tutorial about the above example then kindly share with me i will...

Drop Down List Populated Depending Upon the value Selected in another Drop Down List
  I have five drop down lists.Each One populated based on the value selected in the previious one. I have used SqlDataSource for binding data to each drop down.And I need some initial value so I put "AppendDataBoundItems" as true.Functionality is ok. The problem is, I have selected one from first dropdown.then second drop down is populated.but now i want to select a different value from first dropdown.If i do so,To my second drop down list the values r being appended.not refreshed.I think this is because AppendDataBoundItems=true Plz suggest me something. Thanks in Advan...

Populate a second drop down list upon selection value of the first drop down list in a gridview
How to Populate a second drop down list upon selection value of the first drop down list in a gridview? DropDownList1 ******************* <asp:TemplateField HeaderText="SystemName" SortExpression="SystemName">                <EditItemTemplate>                    <asp:DropDownList ID="sysidddl" runat="server" DataSourceID="sysods1"    &nbs...

Sending email uses the first value in drop down list instead of value selected by the user
I have a web form with a dropdownbox, when the user clicks submit, I insert into a database, and send an email confirmation.  But, I can't get the value the user selects for the dropdownbox.  Instead, it's always the first value in the dynamic drop down box.   Here's the code for the part of the email where I'm trying to get the text associated with the value they select from the drop down list.  Any ideas why I always get the first value in the drop down list for RequestingAttorney?  sString += "Type of search:&nbsp;&nbsp;&nb...

Web Control drop down list, displaying the commonly used values
Hi I'm pretty new to asp.net so need a bit of help if anyone is willing. I have a webpage where u can add users to a SQL table. Currently I have a Web Control drop down list that displays the Title for a person eg. Mr or Mrs. This is a look up field on a table in the SQL database. The problem is that there are over 50 records in this table and the webcontrol is displaying them in alphabetical order. I would like to display the commonly used records at the top of the list. ie. Mr, Miss, Mrs, Ms & Dr. I thought of perhaps adding a ranking field to this table and perhaps display...

Selected value in select list with javascript based off value in database
 I've got a classic asp page with a select list. I need the select list's selected value to be whatever the user selected when they created their account. I've been trying to use <body onLoad="onLoad();"> to call my function, and then I'm trying to specify which value should be selected in the function. Also, when the select list selected values change, there is a javascript function I call to disable text boxes. I think there is an issue with my javascript function, and I can't find any examples of this from good ole' google.  My j...

Web resources about - Drop down list control values OK but not showing selected value on UI - asp.net.web-forms

Portal:Law/Nominate/Selected article - Wikipedia, the free encyclopedia
Add the article in to a subpage of this portal, such as Portal:Law/Selected article/# , replacing the "#" with the number above the current highest. ...

F8: Facebook’s FbStart Initiative Will Give Selected Developers A $30K Kick-Start
... successful applications is likely a whole lot easier with $30,000 in free tools and services to start off with, and that’s exactly what selected ...

LEGO Shop - The LEGO Technic team has selected the winner... - Facebook
The LEGO Technic team has selected the winner of the Co-Creation Challenge! LEGO will be producing a limited run of 20,000 of this stunning set.... ...

baby365- Grand Doctor Ma Selected Students for iPad on the iTunes App Store
Get baby365- Grand Doctor Ma Selected Students on the App Store. See screenshots and ratings, and read customer reviews.

Selected Cheney Vice Presidential Photographs
... for photos found within these albums are Paul Morse, Karen Ballard, Kimberlee Hewitt, Shealah Craighead, and Chris Greenberg. These selected ...

[iOS Advice] How To Clarify Your Email Replies By Quoting Selected Text - YouTube
Follow me on Twitter: http://www.twitter.com/macmixing Like my Facebook page: http://www.facebook.com/macmixing RATE, COMMENT, SUBSCRIBE! :-) ...

Sait Faik Abasıyanık’s Selected Stories are tales that beguile all for their brevity
The influential Turkish writer's collection of short stories is often bizzare but never boring.

Animal of the Day: Lucius Has Been Selected as the U.K.’s ‘Happiest Cat’
He’s got one blue eye, one green eye and a heart of gold. British mobile phone company O2 recently held some “cat auditions” to find “the UK’s ...

Michael Sam becomes first openly gay player selected in an NFL Draft
Defensive end Michael Sam of the University of Missouri became the first openly gay player to be selected in a National Football League Draft ...

Refugee spinner selected for PM's XI
Fawad Ahmed has gone from asylum seeker to rubbing shoulders with Julia Gillard at The Lodge.

Resources last updated: 11/30/2015 2:16:16 AM