Click here to Skip to main content
15,904,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I bind a dropdownlist control to values retrieve from database. I want to use an inline expression to display the text selected by user from the dropdown control, but it's not working. Here is the code i use.
ASP.NET
<asp:Label ID="Label1" runat="server" Text="<%# DropDownListVehicleGpsDateTime.SelectedText.ToString()%>"> 
Posted
Updated 11-Oct-11 9:36am
v2

You are only showing the one line of code, so it's difficult to know exactly what is happening here, but I think you are having trouble with client-side vs. server-side programming.

The inline code is interpreted at the server in codebehind, during postback. But the user selects from the dropdown list at runtime at the client, without causing a postback. So the inline code is not having the opportunity to run.

If you need this event to be client-side, you need to set an onchange event for the dropdown, and write some javascript to update the value of Label1.
 
Share this answer
 
Comments
ahmedfaruk88 13-Oct-11 7:48am    
I really appreciate your response and the time you give to answer this question. On_Page load of the .aspx file the dropdownlist is binded and the selected value is display in the label control here is what i finally did.
<asp:Label ID="Label1" runat="server" Text="<%# DropDownListVehicleGpsDateTime.SelectedItem.ToString()%>">
Hi,

its works only on pageloads from server.

what exactly you want to do.

If you want to bind dropdown selected value into Label.

check this code once

JavaScript
$("#Label").text($("#DropDownListVehicleGpsDateTime").Value);


invoke this javascript line on dropdownlist selction change

All the Best
 
Share this answer
 
Comments
ahmedfaruk88 13-Oct-11 7:48am    
I really appreciate your response and the time you give to answer this question. On_Page load of the .aspx file the dropdownlist is binded and the selected value is display in the label control here is what i finally did.
<asp:Label ID="Label1" runat="server" Text="<%# DropDownListVehicleGpsDateTime.SelectedItem.ToString()%>">
Muralikrishna8811 13-Oct-11 7:55am    
Hi,

you 've to write my code in

onchange event of dropdownlist
like
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script language ="javascript" >

function f1() {

document.getElementById('<%=Label1.ClientID %>').innerText = document.getElementById('<%=DropDownList1.ClientID %>').value
}
</script>
</head>
<body önload="f1()">
<form id="form1" runat="server">

<asp:Label ID="Label1" runat="server" Text="Label">

<asp:DropDownList ID="DropDownList1" runat="server" onchange="f1()">
<asp:ListItem Text ="gfhdg" Value ="gfhgh">
<asp:ListItem Text ="gfgdfghdg" Value ="gfdfghgh">
<asp:ListItem Text ="gfhdfgdfdg" Value ="gffgdfgfghgh">


</form>
</body>
</html>
Muralikrishna8811 13-Oct-11 7:56am    
coming to your question if you use that its just loads on page refresh only that means when page loads from server.
I really appreciate your response and the time you give to answer this question. On_Page load of the .aspx file the dropdownlist is binded and the selected value is display in the label control here is what i finally did.
<asp:Label ID="Label1" runat="server" Text="<%# DropDownListVehicleGpsDateTime.SelectedItem.ToString()%>"> </asp:Label>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900