473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DropDownList SelectedValue Problems

Forget about the controlParameter for the moment, for testing purposes i have
created the following

Markup:

<asp:Table ID="tblSelectRoute" runat="server" CssClass="asp-table">
<asp:TableRow>
<asp:TableCell CssClass="asp-table-header">Select
Route<asp:Label ID="lblTest" runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell CssClass="asp-table-cells">
<asp:DropDownList ID="DDLSelectRoute" runat="server"
AutoPostBack="True" DataSourceID="SDSRoutesList" DataTextField="Route"
DataValueField="RouteID" CausesValidation="True"
OnSelectedIndexChanged="DDLSelectRoute_SelectedInd exChanged">
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
</asp:Table>

Code in the PageLoad event handler:

lblTest.Text = DDLSelectRoute.SelectedValue;

For some reason i cannot fathom, the label control is blank untill the value
in the DDL is changed. The HTML rendered to the browser shows thus

<select name="ctl00$ContentPlaceHolder1$DDLSelectRoute"
onchange="javascript:setTimeout('WebForm_DoPostBac kWithOptions(new
WebForm_PostBackOptions("ctl00$ContentPlaceHolder1 $DDLSelectRoute", "", true,
"", "", false, true))', 0)" id="ctl00_ContentPlaceHolder1_DDLSelectRoute">
<option selected="selected" value="1">1 - Test</option>
<option value="2">2 - Test</option>

<option value="3">3 - Test</option>

So the first item is selected.

If i add DDLSelectRoute.SelectedIndex = 4; at the begining of the pageload
event handler then the selected index is chyanged but the label control is
still blank until i change the value in the DDL.

If i replace lblTest.Text = DDLSelectRoute.SelectedValue; with lblTest.Text
= DDLSelectRoute.SelectedItem.Value; i get an "Object reference not set to an
instance of an object" exception. But if i then cut lblTest.Text =
DDLSelectRoute.SelectedItem.Value; from the page load event and then paste it
inro the SelectedIndexChanged event handler for the DDL as soon as you change
the DDL then the label control is updated.

I am totally puzzled, it is acting as if the SelectedValue is not set until
you change the DDL at least once.


"Phillip Williams" wrote:
You description (which you posted several times) is still a bit difficult to
understand. Your description sounds as if you are claiming that a
dropdownlist.selectedValue is null unless you change the selection. This is
not the reality though. A dropdownlist’s selectedvalue is either the first
item in the list or whatever has been marked as selected=true by default.
Then you are saying that you have a problem setting a controlparameter to
that value but you did not elaborate why.

Can you post a sample code that reproduces the problem that you described?

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"clickon" wrote:
I want to use the selected value of a drop down list to filter a data source.
When i have done this before i have used a DataSourceControl and i have just
made the drop down a control parameter, which works well. However this time
i need to combine a set of results from two databases into a single data
source so i need to use ADO to do it. The problem i come up against is that
until the value in the drop ,down box is changed, the SelectedValue property
does not seem to exist (or is set to null). I have tried setting the
SelectedIndex to a default value, and this work in so far as that index is
selected in the drop down list on the screen, however the SelectedValue
property is still null.

How do i get the vlue that is selected by default in a drop down list,
before anyone selects a different value. This just works with DataSource
controls and control parameters but you can't seem to add a controlparameter
to a ADO DataCommands Parameters collection.
Jul 24 '06 #1
1 5384
Ignore the above post, the problem was cause by the fact that the
DropDownList ahnd't been data bound yet during the PageLoad event.

"clickon" wrote:
Forget about the controlParameter for the moment, for testing purposes i have
created the following

Markup:

<asp:Table ID="tblSelectRoute" runat="server" CssClass="asp-table">
<asp:TableRow>
<asp:TableCell CssClass="asp-table-header">Select
Route<asp:Label ID="lblTest" runat="server"></asp:Label>
</asp:TableCell>
<asp:TableCell CssClass="asp-table-cells">
<asp:DropDownList ID="DDLSelectRoute" runat="server"
AutoPostBack="True" DataSourceID="SDSRoutesList" DataTextField="Route"
DataValueField="RouteID" CausesValidation="True"
OnSelectedIndexChanged="DDLSelectRoute_SelectedInd exChanged">
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
</asp:Table>

Code in the PageLoad event handler:

lblTest.Text = DDLSelectRoute.SelectedValue;

For some reason i cannot fathom, the label control is blank untill the value
in the DDL is changed. The HTML rendered to the browser shows thus

<select name="ctl00$ContentPlaceHolder1$DDLSelectRoute"
onchange="javascript:setTimeout('WebForm_DoPostBac kWithOptions(new
WebForm_PostBackOptions("ctl00$ContentPlaceHolder1 $DDLSelectRoute", "", true,
"", "", false, true))', 0)" id="ctl00_ContentPlaceHolder1_DDLSelectRoute">
<option selected="selected" value="1">1 - Test</option>
<option value="2">2 - Test</option>

<option value="3">3 - Test</option>

So the first item is selected.

If i add DDLSelectRoute.SelectedIndex = 4; at the begining of the pageload
event handler then the selected index is chyanged but the label control is
still blank until i change the value in the DDL.

If i replace lblTest.Text = DDLSelectRoute.SelectedValue; with lblTest.Text
= DDLSelectRoute.SelectedItem.Value; i get an "Object reference not set to an
instance of an object" exception. But if i then cut lblTest.Text =
DDLSelectRoute.SelectedItem.Value; from the page load event and then paste it
inro the SelectedIndexChanged event handler for the DDL as soon as you change
the DDL then the label control is updated.

I am totally puzzled, it is acting as if the SelectedValue is not set until
you change the DDL at least once.


"Phillip Williams" wrote:
You description (which you posted several times) is still a bit difficult to
understand. Your description sounds as if you are claiming that a
dropdownlist.selectedValue is null unless you change the selection. This is
not the reality though. A dropdownlist’s selectedvalue is either the first
item in the list or whatever has been marked as selected=true by default.
Then you are saying that you have a problem setting a controlparameter to
that value but you did not elaborate why.

Can you post a sample code that reproduces the problem that you described?

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"clickon" wrote:
I want to use the selected value of a drop down list to filter a data source.
When i have done this before i have used a DataSourceControl and i have just
made the drop down a control parameter, which works well. However this time
i need to combine a set of results from two databases into a single data
source so i need to use ADO to do it. The problem i come up against is that
until the value in the drop ,down box is changed, the SelectedValue property
does not seem to exist (or is set to null). I have tried setting the
SelectedIndex to a default value, and this work in so far as that index is
selected in the drop down list on the screen, however the SelectedValue
property is still null.
>
How do i get the vlue that is selected by default in a drop down list,
before anyone selects a different value. This just works with DataSource
controls and control parameters but you can't seem to add a controlparameter
to a ADO DataCommands Parameters collection.
>
Jul 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CK | last post by:
Hi All, I have a class and one property is an Enum. I am trying to persist the DropDownList.SelectedValue to the class property. I am using job.ExpRelated =...
1
by: wbosw | last post by:
I would like to open a new window to a url that I have set as the dropdownlist.selectedvalue on the selected index change event. The selectedvalue is the url path and it will open in the existing...
2
by: wbosw | last post by:
I would like to open a new window to a url that I have set as the dropdownlist.selectedvalue on the selected index change event. The selectedvalue is the url path and it will open in the existing...
18
by: Redhairs | last post by:
Is it possible to get DropDownList.SelectedValue in Page_PreInit() event during the postback?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.