在更改下拉框的值后,Label控件中的值随之变化
前台代码
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<span>
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>无</asp:ListItem>
<asp:ListItem>特等功</asp:ListItem>
<asp:ListItem>一等功</asp:ListItem>
<asp:ListItem>二等功</asp:ListItem>
<asp:ListItem>留存奖</asp:ListItem>
</asp:DropDownList>
</span>
<span><asp:Label ID="Label6" runat="server" Text="Label6"></asp:Label></span>
</ContentTemplate>
</asp:UpdatePanel>
后台代码
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = this.DropDownList1.SelectedValue;//获取下拉框的值
this.Label6.Text = selected;
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
sqlcon.Open();
string strselect = "select * from kaoqinfakuan where id='1'";
SqlCommand sqlcmd = new SqlCommand(strselect, sqlcon);
SqlDataReader dr = sqlcmd.ExecuteReader();
if (dr.Read())
{
if (selected == "无")
{
this.Label6.Text = "0";
}
if (selected == "特等功")
{
this.Label6.Text = dr["tedenggong"].ToString();
}
if (selected == "一等功")
{
this.Label6.Text = dr["yidenggong"].ToString();
}
if (selected == "二等功")
{
this.Label6.Text = dr["erdenggong"].ToString();
}
}
dr.Close();
sqlcon.Close();
}