 |
| 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
|
|
|
|

December 8th, 2003, 02:17 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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\"
|
|

December 8th, 2003, 03:42 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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.
|
|

December 11th, 2003, 03:13 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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"
|
|

December 11th, 2003, 03:55 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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.
|
|

December 11th, 2003, 04:02 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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"
|
|

December 11th, 2003, 04:08 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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.
|
|

December 11th, 2003, 04:11 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes... IsNothing(dl_ControlAccount) in my btn_SubmitUpdate_OnClick() returns "True".
"A spirit with a vision is a dream with a mission"
|
|

December 12th, 2003, 03:31 AM
|
|
Registered User
|
|
Join Date: Dec 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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"));
|
|

December 12th, 2003, 12:06 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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.
|
|

December 15th, 2003, 12:48 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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"
|
|
 |