asp.net中用datatable给dropdownlist绑定数据 130
根据当前登录用户名,在dropdownlist里显示对应的姓名,一个用户名对应多个姓名,所以要显示的是一列,就用了datatable,然后旁边有个按钮,点击之后要给gou...
根据当前登录用户名,在dropdownlist里显示对应的姓名,一个用户名对应多个姓名,所以要显示的是一列,就用了datatable,然后旁边有个按钮,点击之后要给goupiao表插入一行数据
前台:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
乘车人:<asp:DropDownList ID="DropDownList1" runat="server" Width="298px"> </asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="预定" />
</asp:Content>
后台:
protected void Page_Load(object sender, EventArgs e, string cmdText)
{
if (!IsPostBack)
{
string sqlConStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(sqlConStr); ;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = cmdText;
string UserName = Session["UserName"].ToString();
DataTable dr = new DataTable("select Name from ChengCheRen where UserName " + UserName);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.Fill(dr);
DropDownList1.DataSource=dr;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "UserId";
DropDownList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string Orderid;
if (Session["UserName"] == null)
{
Alert.AlertAndRedirect("您还没有登录,请登录后再购买,谢谢合作!", "Default.aspx");
}
else
{ sqlconn.Open();
string strid = Page.Request.QueryString["CheCiID"];
string sql1 = "select * from CheCi where CheCiID=" + Request.QueryString["CheCiID"].ToString();
SqlDataReader drhb = data.GetDataReader(sql1);
if (drhb.Read())
{
string sqlstr = "insert into GouPiao"
+ "(OrderID,OrderMember,CheCiID,CheCiName,CheCiPrice,IsCheckout,shuliang,ZuoWei,ChengCheRen)"
+ " values('" + Orderid + "','" + Session["UserName"].ToString() + "','" + Request.QueryString["CheCiID"].ToString() + "','"
+ drhb["CheCiName"].ToString() + "','" + drhb["CheCiPrice"].ToString() + "','否','1','" + Request.QueryString["BianHao"].ToString()+ "')";
data.RunSql(sqlstr);
data.RunSql("update ZuoWei set ShouChu='是' where id='" + Request.QueryString["id"].ToString() + "'");
Response.Redirect("Shopping.aspx");
}
}
我现在能运行....但是dropdownlist没有取到值,然后上面内个inset into的语句该改成啥样,才能把取到的值插入到goupiao表的ChengCheRen中 展开
前台:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
乘车人:<asp:DropDownList ID="DropDownList1" runat="server" Width="298px"> </asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="预定" />
</asp:Content>
后台:
protected void Page_Load(object sender, EventArgs e, string cmdText)
{
if (!IsPostBack)
{
string sqlConStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(sqlConStr); ;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = cmdText;
string UserName = Session["UserName"].ToString();
DataTable dr = new DataTable("select Name from ChengCheRen where UserName " + UserName);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.Fill(dr);
DropDownList1.DataSource=dr;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "UserId";
DropDownList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string Orderid;
if (Session["UserName"] == null)
{
Alert.AlertAndRedirect("您还没有登录,请登录后再购买,谢谢合作!", "Default.aspx");
}
else
{ sqlconn.Open();
string strid = Page.Request.QueryString["CheCiID"];
string sql1 = "select * from CheCi where CheCiID=" + Request.QueryString["CheCiID"].ToString();
SqlDataReader drhb = data.GetDataReader(sql1);
if (drhb.Read())
{
string sqlstr = "insert into GouPiao"
+ "(OrderID,OrderMember,CheCiID,CheCiName,CheCiPrice,IsCheckout,shuliang,ZuoWei,ChengCheRen)"
+ " values('" + Orderid + "','" + Session["UserName"].ToString() + "','" + Request.QueryString["CheCiID"].ToString() + "','"
+ drhb["CheCiName"].ToString() + "','" + drhb["CheCiPrice"].ToString() + "','否','1','" + Request.QueryString["BianHao"].ToString()+ "')";
data.RunSql(sqlstr);
data.RunSql("update ZuoWei set ShouChu='是' where id='" + Request.QueryString["id"].ToString() + "'");
Response.Redirect("Shopping.aspx");
}
}
我现在能运行....但是dropdownlist没有取到值,然后上面内个inset into的语句该改成啥样,才能把取到的值插入到goupiao表的ChengCheRen中 展开
展开全部
public void LoadDropDownList()
{
try
{
//获取list
List<model> list = new List<model>();
list = ...;
//创建DataTable
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
foreach (var model in list)
{
DataRow dr = dt.NewRow();
dr["ID"] = model.ID;
dr["Name"] = model.Name;
dt.Rows.Add(dr);
}
//绑定数据
DropDownList.DataSource = dt;//指定数据
{
try
{
//获取list
List<model> list = new List<model>();
list = ...;
//创建DataTable
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
foreach (var model in list)
{
DataRow dr = dt.NewRow();
dr["ID"] = model.ID;
dr["Name"] = model.Name;
dt.Rows.Add(dr);
}
//绑定数据
DropDownList.DataSource = dt;//指定数据
追问
内个list=......是啥 原来都不用查询的么....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询