Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2003, 02:17 PM
Authorized User
 
Join Date: Oct 2003
Location: Golden, CO, USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Usercontrol dropdown list databinding

I have a dropdownlist in an ASCX usercontrol on my page, and I want to pre-set it with a value, ie, something from a database.

The trouble is, it seems like the databinding happens after my calling page's Page_Load() method happens. I tried asking it to spit out the amount of items in the dropdownlist and it gives me 0. Only if I call the page again (ie, form postback) it tells me the right amount.

Could someone point out to me how to access the usercontrol and set a value in the dropdownlist from the calling aspx page? Thanks in advance.

"A spirit with a vision is a dream with a mission"
__________________
\"A spirit with a vision is a dream with a mission\"
 
Old December 8th, 2003, 03:42 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You could set the selected item later on in the page like during PreRender. Write a handler that handles MyBase.PreRender and set it there. By then, it should be bound to the data.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 11th, 2003, 03:13 PM
Authorized User
 
Join Date: Oct 2003
Location: Golden, CO, USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're great! Now that I can set those drop down lists, I'm struggling to get what was selected in a button click event (submitting the form). Can you help me figure out how to access those items?

I can do it in the method that handles MyBase.PreRender like this:

Dim dl_ControlAccount as DropDownList
dl_ControlAccount = ControlAccountList.ddl_ControlAccountList
dl_ControlAccount.Items.FindByValue(Trim(oDS.Table s(0).Rows(0).Item("Control_Account_Number"))).Sele cted=True

(be aware of wrapping)

But this doesn't work in the form postback method. Any suggestions? Thank you.

"A spirit with a vision is a dream with a mission"
 
Old December 11th, 2003, 03:55 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Did you post the correct code? The code I'm looking at is for setting a list item as selected not for retrieving the value.

You should handle things for a button event in the event handler (not PreRender in this case).

To access the selected item of a DDL, you just need to look at the SelectedItem property of the control. There is also SelectedIndex, SelectedValue and SelectedText.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 11th, 2003, 04:02 PM
Authorized User
 
Join Date: Oct 2003
Location: Golden, CO, USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're right... sorry for not being so clear... I put that in there to show how I was accessing the stuff to set the value, but I don't actually have any code for the fetching.

I understand how to get it out of a dropdownlist... but doing this:

(in the class declaration)

Public dl_ControlAccount as DropDownList

(then later in setting the value with the PreRender)

dl_ControlAccount = ControlAccountList.ddl_ControlAccountList 'ControlAccountList is the namespace of my user control, ddl_ControlAccountList is the dropdown

dl_ControlAccount.Items.FindByValue(Trim(oDS.Table s(0).Rows(0).Item("Control_Account_Number"))).Sele cted=True

and then... in the button click method:

Public Sub btn_SubmitUpdate_OnClick(Sender as Object, e as System.EventArgs)
   Response.Write(dl_ControlAccount.SelectedItem.Valu e)
End Sub

I get a "Object Reference not set to an instance of an object" error. I guess what I need to know is how do I find out what the .NET page is calling that user control. Thank you!

"A spirit with a vision is a dream with a mission"
 
Old December 11th, 2003, 04:08 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Usually this happens when SelectedItem is Nothing. But for a dropdownlist, you should almost never have no selected item. But it's possible for dl_ControlAccount to be Nothing. You'll have to check for those. If the DDL is nothing, then there might be another problem.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 11th, 2003, 04:11 PM
Authorized User
 
Join Date: Oct 2003
Location: Golden, CO, USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes... IsNothing(dl_ControlAccount) in my btn_SubmitUpdate_OnClick() returns "True".

"A spirit with a vision is a dream with a mission"
 
Old December 12th, 2003, 03:31 AM
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

DataTable dt1=aSqlData.GetDataTable("select FCTY from Custorder group by FCTY","FCTY");
                ddlFTY.DataSource=dt1.DefaultView;
                ddlFTY.DataValueField="FCTY";
                ddlFTY.DataBind();
                ddlFTY.Items.Add("ALL");
                ddlFTY.SelectedIndex=ddlFTY.Items.IndexOf(ddlFTY.I tems.FindByText("ALL"));

 
Old December 12th, 2003, 12:06 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

If I may make a suggestion to snowhu's code: It's not very efficient to go and search back thru the whole item collection by a text string to select it when you could access the item object directly:

ListItem objItem = new ListItem("All");
ddlFTY.Items.Add(objItem);
<s>ddlFTY.Items.Add("ALL");</s>
ddlFTY.SelectedIndex=ddlFTY.Items.IndexOf(objItem) ;
<s>ddlFTY.SelectedIndex=ddlFTY.Items.IndexOf(ddlFT Y.Items.FindByText("ALL"));</s>

Might even be better to do this:

ListItem objItem = new ListItem("All");
ddlFTY.Items.Add(objItem);
ddlFTY.SelectedIndex = -1; //make sure nothing's selected
objItem.Selected = true;

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 15th, 2003, 12:48 PM
Authorized User
 
Join Date: Oct 2003
Location: Golden, CO, USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This code works... I can find the ControlAccountList user control as a child control of my form.

Dim FF as Control
FF = Page.FindControl("frm_EditForm").FindControl("Cont rolAccountList")

However I still am struggling how to figure out how to access what's in that control. It's a dropdown list. as in:

Dim eFF as Accounting.control_account
Dim oDL as DropDownList = eFF.ddl_ControlAccountList

I'm close... someone push me over the edge....

"A spirit with a vision is a dream with a mission"




Similar Threads
Thread Thread Starter Forum Replies Last Post
Phantom Databinding on drop down list Jerminator ASP.NET 1.x and 2.0 Application Design 0 February 6th, 2008 12:33 AM
Databinding Parent Dropdown from Popup bijukprm ASP.NET 2.0 Basics 0 September 5th, 2007 11:49 PM
Dropdown list maha .NET Framework 2.0 7 April 28th, 2007 12:32 AM
fill dropdown list with items when parent list isaac_cm Pro PHP 1 July 10th, 2006 05:41 AM
usercontrol dropdown list... help forkhead ASP.NET 1.0 and 1.1 Professional 1 November 19th, 2003 06:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.