|
| |
| joe_phillips | Asp.Net User |
| maintaining selectedindex in dropdownlist custom control? | 6/9/2005 3:06:17 AM |
0/0 | |
|
I am having a difficult problem here that I have been playing with for quite a while. I am attempting to create a custom control that will basically function as a US states dropdownlist. All states and territories that I specified are to be bound to the control. The states are being bound correctly to the list.
I have created 3 properties to control the value of the 'header' ListItem of the list, eg 'select a state'. One is the boolean to see if we want a header on the list, and the other two are the value and the text of the ListItem.
My list 'header' also binds correctly. My problem lies after the initial insert of the state I selected to the database. The state that I selected binds correctly from the database when I come back to edit the list. The ONLY thing that will not work is if my header ListItem is what I selected and sent as my selection to the database. When that happens, the first state on the list (Alabama) is selected on databind. My header ListItem is still in the list, but not selected.
Here is my code in my custom control where I am trying to track the SelectedIndex of the dropdownlist. I have tried to simplify it, but this is the most basic form that still retains the most functionality.
protected override void OnLoad(System.EventArgs e) { #region if(base.EnableViewState) { //the problem here is that, when top entry is selected, //not binding as top entry. binding as Alabama
//only do all this first time if(!Page.IsPostBack) { //set our 'old' selected index, as this value will get lost selected_index = base.SelectedIndex;
base.DataBind();
base.SelectedIndex = selected_index;
ListItem li = new ListItem(this.top_entry_text,this.top_entry_value);
base.Items.Insert(0,li);
//if our new value is not equal to the value //of our header, then raise the index by one if(base.SelectedValue.ToString() != this.top_entry_value.ToString()) { base.SelectedIndex = selected_index + 1; } else { base.SelectedIndex = -1; } } } #endregion }
If anyone can make any sense out of that, I would love any input.
Thanks, Joe |
| joe_phillips | Asp.Net User |
| Re: maintaining selectedindex in dropdownlist custom control? | 6/13/2005 8:55:40 PM |
0/0 | |
|
That makes sense as well :). The problem that I am having though is not with the actual binding, but with my attempt to provide some 'customizability' on the front end. Eg I want to be able to specify over several apps (and I have had the use already for different headers) to say 'Select a state' or '' (no value), or anything else that may come up. I am using this in, for instance, collecting info about a system user:
The problem I have is that the user enters their info: state is not required, and is a blank ('') on the top. -> user selects ''(blank), and submits info to database -> ''(blank) is value in database -> user comes back to edit their information -> 'Alabama', as the first state, is selected. -> ''(blank) is there is a choice, but not pre-selected as it should be. It can be changed, and selected again, but this obviously will not work.
Everything binds correctly, and sends the proper info to the database when submitted. It is just when the item is bound the on edit in this case that I have problems. I think that I will have the same difficulty if I load every state in onInit because I will still have to manipulate the selectedinxed property. (I also have my 'header' ListItem as an optional field.) If the class property is set to false, none of the header stuff will take place.
Anyway, thanks much for your help. I may end up simplifying this, and require that the header have a value. I think then that I can get around this annoying problem. Or just make multiples of this control for different cases, but I wanted to get it really right.
Thanks, Joe |
|
| |
Free Download:
Books: Professional VB 2005 Authors: Bill Evjen, Billy Hollis, Rockford Lhotka, Tim McCarthy, Rama Ramachandran, Kent Sharkey, Bill Sheldon, Pages: 1066, Published: 2006 Web:Extended Dropdown List not maintaining SelectedValue after ... Extended Dropdown List not maintaining SelectedValue after postback ... Filed under: custom control, postback, viewstate treenode, Dropdown ... 15 Seconds : Creating a Custom .NET Web Control With Events The first step will be to add the DropDownList control on to the form. .... # Code public int SelectedIndex { get{ return storeList. ... p2p.wrox.com Forums - Maintaining the viewstate in a custom web ... However, I can't get it to maintain the viewstate. I've never made a custom control before so I'm not entirely sure if I'm doing it right. ... Inside Microsoft: Code Reuse with ASP.NET Part 3: Custom Controls Since the custom control for this example is a modified drop down list, we’ve called it .... SelectedIndex > -1 Then ‘Return the value of the selected item ... CodeProject: Trick/Tip: Raise a GridView RowCommand event from a ... How can we filter items in a dependent DropDownList based on the selection ... of maintaining a user control or a web custom control in my code library. ... Custom DropDownList Control with client side data by Programming ... NET dropdownlist control that will maintain the selected value after postback, ... SelectedValue; lblTest2.Text = "JS Dropdownlist 1 data: " + ddlJS. ... 'SelectedIndex' and 'SelectedValue' attributes are mutually ... Can't access databound dropdownlist properties in user control ... Maintaining treeview expand collapse status per node? ... asp.net interview questions: Bind an XML file to a dropdownlist SelectedItem.Text + " and ID is " + DropDownList1.SelectedItem.Value); ... Custom Controls related ASP.NET Interview Questions are listed in this article. ... maintaining dropdownlist used in a control and used on a master ... maintaining dropdownlist used in a control and used on a master page. .... Silverlight: Skinnable Custom Controls – Change to generic.xaml. Silverlight ... Anatomy of a Server Control (Part 3) Maintaining State; Firing a custom Event; Populating the List ..... Conclusions: By building a DropDownList Server Control, we have demonstrated how a ... |
|
Search This Site:
|
|