Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print a selected value from a drop list in asp.net

Status
Not open for further replies.

eirikr

Programmer
Oct 31, 2008
15
US
In main.aspx page, i have a drop down list:

<asp:DropDownList ID="ddl" runat="server" DataSourceID="filter_datasource"
DataTextField="status"
DataValueField="status"
AppendDataBoundItems="True"
AutoPostBack="True"
Font-Names="Arial"
Font-Size="8pt">
<asp:ListItem Value="-1" Selected="True">select</asp:ListItem>
</asp:DropDownList> &nbsp;cases.
----------
in my behind code main.aspx.cs, i can see these values:

Response.Write("index: " + ddl.SelectedIndex + "<br>");
Response.Write("item: " + ddl.SelectedItem + "<br>");
Response.Write("value: " + ddl.SelectedValue + "<br>");

My question is how can i print these values in main.aspx;
I had tried <% ddl.SelectedValue; %> ...etc. but it does not work. Please help
 
What do you mean print the values?
Resonse.write() will write a string to the page, which by the way, should only be used for debugging purposes.
 
Yes, what i mean for debugging, so i can see what the value i get from selecting status, so i can set up sqlcommand like:

if status==-1 (list all the resords)
<asp: sqldatasource ...
selectCmd = "select * from table where status is not null"
else status=='Open'
selectCmd = "select * from table where status = @status"
...
 
It's best to step through your code using the debugger. But I still am confused on what you are trying to accomplish. If you want the selected value of a dropdownlist, then use, Dropdownlist.selectedvalue.
 
Here is my main.aspx page

<% Response.Write("status: " + ddl_1.SelectedIndex); %>
<div>
<asp:SqlDataSource ID="sds_1" runat="server"
ConnectionString="<%$ConnectionStrings:... %>"
ProviderName="<%$ ConnectionStrings:... %>"

//-- ISSUE is here: if status=-1 i want to use this sql command to list all records
SelectCommand="SELECT * FROM [Atable] where status is not null" >
//-- else i want to use
SelectCommand="SELECT * FROM [Atable] where status = @status
//-- HOW CAN I PLACE THE STATUS VALUE HERE



<SelectParameters>
<asp:ControlParameter Name="status" ControlID="ddl_1" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</div>

<div>
<asp:SqlDataSource ID="sds_2" runat="server"
ConnectionString="<%$ ConnectionStrings:... %>"
SelectCommand="SELECT [status] FROM [StatusTbl]">
</asp:SqlDataSource>
</div>

<div>
<span style="font-family:Arial; font-size:8pt;">Show&nbsp;
<asp:DropDownList ID="ddl_1" runat="server" DataSourceID="sds2"
DataTextField="status"
DataValueField="status"
AppendDataBoundItems="True"
AutoPostBack="True"
Font-Names="Arial"
Font-Size="8pt">
<asp:ListItem Value="-1" Selected="True">---select---</asp:ListItem>
</asp:DropDownList> &nbsp;cases.
</span>
<br /><br />

<asp:GridView ID="GridView1" ...</asp:GridView>
<br /><br /><br />
</div>
</asp:Content>

--What i want is, when the ddl_1.selectedVal=-1 then in GridView will list all the open/close/pending records
But if the selectedVal='open or close or pending'
then GridView only those records corresponded to the selectedValue.
 
log it. this way you are not polluting the GUI with debug information. a logging library like log4net makes this very easy.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
The problem you are having is that you are using the datasoruce controls. I strongly recommend you don't. They are more trouble then they are worth. They cannot be degugged and maniupulated easily.

If you want your sql to change, based on the ddl selection, use the SelectedIndexChanged event of the dropdown list to change the select statement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top