我在做一个网页,在网页上添一个控件DropDownList,选择上海,应该显示晴(要用到什么控件?数据绑定)

发布时间:2024-05-20 02:27 发布:上海旅游网

问题描述:

SQL数据库中有个表,表中有两列,城市和天气,例如我写了两组数据,上海 晴,北京 阴。
用C#.net 05写。。。希望大虾提供详细代码,会加分的。。。

问题解答:

页面加DropDownList 显示天气用什么控件 自己选 我这用的textbox
SqlConnection conn = new SqlConnection(你的sql连接串);
DataSet ds = new DataSet();
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from datafile", conn);
da.Fill(ds);
}
catch
{
}
finally
{
conn.Close();
}
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "城市";
DropDownList1.DataValueField = "天气";
DropDownList1.DataBind();
在DropDownList1的DropDownList1_SelectedIndexChanged 事件里写
TextBox1.Text = DropDownList1.SelectedValue.ToString();
当然 你也可以取DropDownList1的value 或text 然后再从数据库取 一对一的话着样相对简单点

.aspx
<asp:lable runat ="server" id="lab1"/>
<asp:DropDownList id="ddlrefectory" runat="server" AutoPostBack="True" onselectedindexchanged="ddlrefectory_SelectedIndexChanged"/>

.cs
protected void Page_Load(object sender, System.EventArgs e)
{
this.ddlrefectory.DataSource=数据源;
this.ddlrefectory.DataTextField="列名";
this.ddlrefectory.DataValueField="列名";
this.ddlrefectory.DataBind();
this.ddlrefectory.Items.Insert(0,new ListItem("请选择","0"));
this.ddlrefectory.SelectedIndex=0;
}
protected void ddlrefectory_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.lab1.Text = this.ddlrefectory.SelectedValue;
}
看不明白在问我

其实只要简单地设置控件就行了

DropDownList 绑定你数据库里的2列,,上海显示在dropDownList里,,晴 是隐藏的.

热点新闻