问题描述
需要帮助请,我有两个DropDownLists(嵌套).我使用SelectedIndexChanged事件填充第二个DropDownList.
因此,问题在于我需要将DropDownLists的两个值都保存到数据库中,我可以轻松完成第一个DropDownList的操作,但是如何捕获第二个值(DropDownList2.SelectedItem.Value)?
这是我的代码的一部分..
.
.
.
Need help please, i have two DropDownLists (nested). I use SelectedIndexChanged event to populate the second DropDownList.
So, the problem is that i need to save the both values of DropDownLists to a database, i can do easily for the first DropDownList , but how to capture the second value ( DropDownList2.SelectedItem.Value)?
this is a part of my code ..
.
.
.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
// create SqlConnection object
string ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
// create SqlCommand object
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
try
{
myConnection.Open();
myCommand.CommandText = "SELECT category_id, name FROM categories order by 2";
// run query
SqlDataReader myReader = myCommand.ExecuteReader();
// set up the list
DropDownList1.DataSource = myReader;
DropDownList1.DataBind();
// close the reader
myReader.Close();
}
finally
{
// open the database connection
myConnection.Close();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// create SqlConnection object
string ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
// create SqlCommand object
string CommandText = "SELECT sub_cat_id, name, category_id FROM sub_category WHERE category_id = " + DropDownList1.SelectedItem.Value;
SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
try
{
// open the database connection
myConnection.Open();
// run query
SqlDataReader myReader = myCommand.ExecuteReader();
// setup the GridView
DropDownList2.DataSource = myReader;
DropDownList2.DataBind();
// close the reader
myReader.Close();
}
finally
{
// open the database connection
myConnection.Close();
}
}
推荐答案
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="SampleWebApp.test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="dd1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dd1_Changed"></asp:DropDownList>
<br />
<asp:DropDownList ID="dd2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dd2_Changed"></asp:DropDownList>
<br />
<asp:Label ID="lbl1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
C#代码隐藏:
C# code-behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SampleWebApp
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dd1.DataSource = Data.GetSource(5);
dd1.DataTextField = "Text";
dd1.DataValueField = "Value";
dd1.DataBind();
}
}
protected void dd1_Changed(object sender, EventArgs e)
{
dd2.DataSource = Data.GetSource(int.Parse(dd1.SelectedValue));
dd2.DataTextField = "Text";
dd2.DataValueField = "Value";
dd2.DataBind();
}
protected void dd2_Changed(object sender, EventArgs e)
{
lbl1.Text = dd1.SelectedValue.ToString() + " - " + dd2.SelectedValue.ToString();
}
}
public class Data
{
public string Text { get; set; }
public int Value { get; set; }
public static List<Data> GetSource(int n)
{
List<Data> d = new List<Data>();
Enumerable.Range(1, n).ToList().ForEach(delegate(int i)
{
Data dd = new Data { Text = i.ToString(), Value = i };
d.Add(dd);
});
return d;
}
}
}
希望这可以帮助!让我(我们)知道您是否有任何疑问!
Hope this helps! Do let me (us) know if you have any questions!
protected void DropDownList2_PreRender(object sender, EventArgs e)
{
Label1.Text = DropDownList2.SelectedValue.ToString();
}
这篇关于DropDownList并在SelectedIndexChanged事件之后捕获值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!