ASPX页面
<div>
<asp:UpdatePanel id="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDL1_SelectedIndexChange" Width="100px"></asp:DropDownList><br /><br />
<asp:DropDownList id="DropDownList2" runat="server" Width="100px"></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
c#页面
protected void Page_Load(object sender, EventArgs e)
{
this.username.Text = Convert.ToString(Session["username"]);
this.gongsiname.Text = Convert.ToString(Session["gongsiname"]);
string type2 = Request.Params["DropDownList2.SelectedValue"];
if (!IsPostBack)
{
BindDrop();
}
}
private void BindDrop()
{
//将数据捆绑到下拉列表中
SqlConnection conn = comy_user_user_fabu.Getconn();
string sql = "select * from Province";
SqlDataAdapter myCommand = new SqlDataAdapter(sql, conn);
DataSet myds = new DataSet();
myCommand.Fill(myds, "Authors");
DropDownList1.DataSource = myds.Tables["Authors"].DefaultView;
DropDownList1.DataTextField = "Province_Name"; //设置列表显示的字段
DropDownList1.DataValueField = "ProvinceType"; //设置列表提交后获得的字段
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("请选择类别", ""));//第一项中加入内容,重点是绑定后添加
DropDownList2.Items.Insert(0, new ListItem("请选择细分类别", ""));//第一项中加入内容,重点是绑定后添加
}
protected void DDL1_SelectedIndexChange(object sender, EventArgs e)
{
SqlConnection conn = comy_user_user_fabu.Getconn();
//注:在SQL语句中的函数 substring(dqdm,1,2) 是从数字1开始算起;而在C#中 Substring(0, 2) 是从0开始算起;
string sql2 = "select * from City where type='" + DropDownList1.SelectedIndex + "'";
//string sql2 = "select * from City where type='"+ DropDownList1.DataValueField.ToString()+"'";
//string sql2 = "select * from City where str.substring(4,str.length)='" + DropDownList1.SelectedValue.Trim().Substring(0, 2) + "'";
SqlDataAdapter myCommand2 = new SqlDataAdapter(sql2, conn);
DataSet myds2 = new DataSet();
myCommand2.Fill(myds2, "Authors");
DropDownList2.DataSource = myds2.Tables["Authors"].DefaultView;
DropDownList2.DataTextField = "City_Name"; //设置列表显示的字段
DropDownList2.DataValueField = "type"; //设置列表提交后获得的字段
DropDownList2.DataBind();
}
protected void Bt_tijiao_Click(object sender, EventArgs e)
{
SqlConnection conn = comy_user_user_fabu.Getconn();
SqlCommand mycom = new SqlCommand();
mycom.Connection = conn;
mycom.Parameters.AddWithValue("compy_id",this.username.Text);
mycom.Parameters.AddWithValue("gongsiname",this.gongsiname.Text);
mycom.Parameters.AddWithValue("type1", this.DropDownList1.SelectedItem.Text);
mycom.Parameters.AddWithValue("type2", this.DropDownList2.SelectedValue.ToString());
mycom.Parameters.AddWithValue("title", this.title.Text);
mycom.Parameters.AddWithValue("content",this.content.Value);
DateTime dt=DateTime.Now;
mycom.Parameters.AddWithValue("dt", dt);
mycom.CommandText = "insert into Company(Compy_id,Compy_name,Company_type,Company_Info_title,Company_type2,Company_Info_content,Company_Info_SetTime)values(@compy_id,@gongsiname,@type1,@title,@type2,@content,@dt)";
conn.Open();
mycom.ExecuteNonQuery();
Response.Redirect("~/index.aspx");
}
数据库表
Province.tb
Province_ID
Province_Name
ProvinceTypeCity.tb
City_ID
City_Name
type从第二个dropdownlist中取到是他们的type标识,是怎么回事。求助,谢谢
<div>
<asp:UpdatePanel id="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDL1_SelectedIndexChange" Width="100px"></asp:DropDownList><br /><br />
<asp:DropDownList id="DropDownList2" runat="server" Width="100px"></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
c#页面
protected void Page_Load(object sender, EventArgs e)
{
this.username.Text = Convert.ToString(Session["username"]);
this.gongsiname.Text = Convert.ToString(Session["gongsiname"]);
string type2 = Request.Params["DropDownList2.SelectedValue"];
if (!IsPostBack)
{
BindDrop();
}
}
private void BindDrop()
{
//将数据捆绑到下拉列表中
SqlConnection conn = comy_user_user_fabu.Getconn();
string sql = "select * from Province";
SqlDataAdapter myCommand = new SqlDataAdapter(sql, conn);
DataSet myds = new DataSet();
myCommand.Fill(myds, "Authors");
DropDownList1.DataSource = myds.Tables["Authors"].DefaultView;
DropDownList1.DataTextField = "Province_Name"; //设置列表显示的字段
DropDownList1.DataValueField = "ProvinceType"; //设置列表提交后获得的字段
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("请选择类别", ""));//第一项中加入内容,重点是绑定后添加
DropDownList2.Items.Insert(0, new ListItem("请选择细分类别", ""));//第一项中加入内容,重点是绑定后添加
}
protected void DDL1_SelectedIndexChange(object sender, EventArgs e)
{
SqlConnection conn = comy_user_user_fabu.Getconn();
//注:在SQL语句中的函数 substring(dqdm,1,2) 是从数字1开始算起;而在C#中 Substring(0, 2) 是从0开始算起;
string sql2 = "select * from City where type='" + DropDownList1.SelectedIndex + "'";
//string sql2 = "select * from City where type='"+ DropDownList1.DataValueField.ToString()+"'";
//string sql2 = "select * from City where str.substring(4,str.length)='" + DropDownList1.SelectedValue.Trim().Substring(0, 2) + "'";
SqlDataAdapter myCommand2 = new SqlDataAdapter(sql2, conn);
DataSet myds2 = new DataSet();
myCommand2.Fill(myds2, "Authors");
DropDownList2.DataSource = myds2.Tables["Authors"].DefaultView;
DropDownList2.DataTextField = "City_Name"; //设置列表显示的字段
DropDownList2.DataValueField = "type"; //设置列表提交后获得的字段
DropDownList2.DataBind();
}
protected void Bt_tijiao_Click(object sender, EventArgs e)
{
SqlConnection conn = comy_user_user_fabu.Getconn();
SqlCommand mycom = new SqlCommand();
mycom.Connection = conn;
mycom.Parameters.AddWithValue("compy_id",this.username.Text);
mycom.Parameters.AddWithValue("gongsiname",this.gongsiname.Text);
mycom.Parameters.AddWithValue("type1", this.DropDownList1.SelectedItem.Text);
mycom.Parameters.AddWithValue("type2", this.DropDownList2.SelectedValue.ToString());
mycom.Parameters.AddWithValue("title", this.title.Text);
mycom.Parameters.AddWithValue("content",this.content.Value);
DateTime dt=DateTime.Now;
mycom.Parameters.AddWithValue("dt", dt);
mycom.CommandText = "insert into Company(Compy_id,Compy_name,Company_type,Company_Info_title,Company_type2,Company_Info_content,Company_Info_SetTime)values(@compy_id,@gongsiname,@type1,@title,@type2,@content,@dt)";
conn.Open();
mycom.ExecuteNonQuery();
Response.Redirect("~/index.aspx");
}
数据库表
Province.tb
Province_ID
Province_Name
ProvinceTypeCity.tb
City_ID
City_Name
type从第二个dropdownlist中取到是他们的type标识,是怎么回事。求助,谢谢
解决方案 »
- 怎么用VS 2005/8来做一个ftp server啊?大家给个思路啊……
- 初学者,一个简单的ASP.NET问题!
- vs2003与vs2005?
- ~~~请问如何让服务端button 点击后不提交页面呢???~~~
- XP 怎样才能让 IIS 开机自动运行?
- ext页面问题
- 请教如何动态调用aspx?
- 我怎么能够取到别的网站里面的内容,放在我们自已的网站里面
- table 和 div 那个速度快,或者说那个系统开销少!
- 在DataGird里我把绑定列转换成模板列以后,删除时读取Dim Did As String = e.Item.Cells(2).Text,已经读不到表格中机构编码了
- 问个程序运行顺序的问题,
- ASP.NET中关于类型转换的问题
DropDownList2.DataTextField = "City_Name"; //设置列表显示的字段
DropDownList2.DataValueField = "type"; //设置列表提交后获得的字段
DropDownList2.DataTextField = "City_Name"; //设置列表显示的字段
DropDownList2.DataValueField = "City_ID"; //设置列表提交后获得的字段
是City_ID吧