DropDownList并在SelectedIndexChanged事件之后捕获值 [英] DropDownList and capturing values after SelectedIndexChanged event

查看:92
本文介绍了DropDownList并在SelectedIndexChanged事件之后捕获值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助 plz 请,我有两个DropDownLists(嵌套).我使用SelectedIndexChanged事件填充第二个DropDownList.

因此,问题在于我需要将DropDownLists的两个值都保存到数据库中,我可以轻松完成第一个DropDownList的操作,但是如何捕获第二个值(DropDownList2.SelectedItem.Value)?

这是我的代码的一部分..
.
.
.

Need help plz 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();

	}		
}

推荐答案

您在那里或多或少.只需将第二个下拉菜单的AutoPostBack属性也设置为true并处理选定的索引更改事件.现在,您可以为两个下拉列表选择选定的值,并将其添加到数据库中.

如果这不是您想要实现的,抱歉.请详细说明您的期望.
You are more or less there. Just set the AutoPostBack property of the second drop down also to true and handle the selected index changed event. Now you can the selected values for both the drop downs and add it to the database.

If this is not what you intended to achieve, sorry. Please elaborate on what you expect.


正如我之前所说,将其用作理解您的错误并以此为基础的东西.这只是您如何执行此操作的示例.如果您对此有所了解,我将很高兴!

ASPX标记:

As i said before, use this as something to understand your mistakes and build on it. This is just a sample of how you could do it. I would be happy if you understand something out of this!

ASPX Markup:

<%@ 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!


关键是PreRender事件,例如:

the key was PreRender event like:

protected void DropDownList2_PreRender(object sender, EventArgs e)
{ 
    Label1.Text = DropDownList2.SelectedValue.ToString(); 
}


这篇关于DropDownList并在SelectedIndexChanged事件之后捕获值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆