By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,470 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

DropDownList SelectedValue Problems

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.