Reputation: 133
i have load the DropDownList successfully. all student numbers i have loaded when i select the student no relavent student name should diplay on the below textbox. but now if select any number only one student name is shown John name only. if i select diffent student numbers. i don't know why.
DropDownList Load code
string cmdstr = "select id from records"; SqlCommand cmd = new SqlCommand(cmdstr, con);
con.Open();
read = cmd.ExecuteReader();
DropDownList1.Items.Clear();
while (read.Read())
{
DropDownList1.Items.Add(read["id"].ToString());
}
con.Close();
Selected data
<asp:DropDownList ID="DropDownList1" ViewStateMode="Enabled" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
if (Page.IsPostBack == true)
{
string cmdstr = "select firstname from records where id = " +
DropDownList1.SelectedValue;
SqlCommand cmd = new SqlCommand(cmdstr, con);
con.Open();
read = cmd.ExecuteReader();
while (read.Read())
{
name.Text = read["firstname"].ToString();
}
con.Close();
}
Upvotes: 0
Views: 809
Reputation: 924
I have a example: evt. this helps
<asp:DropDownList ID="ddlFruits" runat="server">
<asp:ListItem Text="Please Select" Value=""></asp:ListItem>
<asp:ListItem Text="Mango" Value="1"></asp:ListItem>
<asp:ListItem Text="Apple" Value="2"></asp:ListItem>
<asp:ListItem Text="Orange" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:Button Text="Get Selected Text Value" runat="server" OnClientClick="return GetSelectedTextValue()" />
<script type="text/javascript">
function GetSelectedTextValue() {
var ddlFruits = document.getElementById("<%=ddlFruits.ClientID %>");
var selectedText = ddlFruits.options[ddlFruits.selectedIndex].innerHTML;
var selectedValue = ddlFruits.value;
alert("Selected Text: " + selectedText + " Value: " + selectedValue);
return false;
}
</script>
You can get the value from the list or the selected text (innerHTML) !
Upvotes: 0