CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

ASP.NET Web Hosting – 3 Months Free!



Zone: > NEWSGROUP > Asp.Net Forum > windows_hosting.hosting_open_forum Tags:
Item Type: NewsGroup Date Entered: 6/9/2005 3:06:17 AM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 4 Views: 99 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
5 Items, 1 Pages 1 |< << Go >> >|
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
Raterus
Asp.Net User
Re: maintaining selectedindex in dropdownlist custom control?6/10/2005 9:15:25 PM

0/0

I don't know if any of this will help, but here goes nothing!

When I create my own custom controls, I really never check if !Page.IsPostBack(), I leave it up to the calling page to make that determination, and chug away at methods in my control accordingly.  In your case, I likely would override the DataBind method of the control, and write the code you've shown there.  Then I'd create another property DataSource, which my calling page can set.  Does this model sound familiar at all?, it's in all the DataBound controls that I know of!


Ask and it will be given to you; seek and you will find; knock and the door will be opened to you. Luke 11:9
joe_phillips
Asp.Net User
Re: maintaining selectedindex in dropdownlist custom control?6/13/2005 8:32:22 PM

0/0

Michael,

I tried to put that code into the DataBind() method. I didn't have any success, and am going to give up on it for now.

I didn't want to have a datasource specified by the calling page because the states and terrirories are static, and I wanted a little self-contained control that I could just plug into the .aspx page with nothing in the code-behind to have to call to initialize it. It's depressing, because it is so close; it just won't bind the correct value if I have the calling-page-specified 'top value' (eg 'select a state') as my selected value. Eg no choice was made initially. I am going to leave it for a while and come back in a week with a bit of a fresh look at it.

Thanks much for your insight, and that does make sense to model my controls after those that are already there a little more.

Thanks,
Joe
Raterus
Asp.Net User
Re: maintaining selectedindex in dropdownlist custom control?6/13/2005 8:39:20 PM

0/0

Since they are states, and for as long as I've been alive there have been 50 of them, could you just have a bunch of lines in the init method and forget databinding?

this.Items.Add("Alaska");
this.Items.Add("Arkansas");


Ask and it will be given to you; seek and you will find; knock and the door will be opened to you. Luke 11:9
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
5 Items, 1 Pages 1 |< << Go >> >|


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:










about a xml question in tutorials

field's tab index lost after postback asp.net 2.0

reading mail server messages (pop3)

how can i enumerate class or control properties ?

this maze should be more automated (trying to access my asp.net app from the network)

.tmp files appearing in solution explorer

image creation

c# culture issue

storing name value pair : where to store

i am new to unit testing , can i please get some help on this question ?

bizarre request regarding the excution of asp.net websites

a string literal was expected but no opening quote character was found

about [system.threading.threadabortexception]

file information

login web controls using custom database instead of aspnetdb.mdf

tab

media player

display results of an http post

the name 'thefilename' does not exist in the current context

date data type question

app.config in a class library referenced in a web application

getting index number of new row

login (asp.new with vb.net)

stdole

how do i test for double quotes in a regular expression?

check if object exsists?

unable to send mail

download dialog box for export image file

newsletter manager

autoeventwireup

  Privacy | Contact Us
All Times Are GMT