且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态下拉列表始终返回第一个值,而不是选定的值

更新时间:2022-12-02 22:42:38

教室水平] WHERE [教室类别] ='{0}', NURSERY);
BindDropDownList( this .ddlAppClass,query, 描述 教室类别 ---选择---);
}

}





绑定方法:



  private   void  BindDropDownList(DropDownList) ddl,字符串查询,字符串文本,字符串  string  defaultText)
{
string conString = ConfigurationManager.ConnectionStrings [ DatabaseDemo数据库NAV(8-0)1]。ConnectionString;
SqlCommand cmd = new SqlCommand(query);
使用(SqlConnection con = new SqlConnection(conString))
{
使用(SqlDataAdapter sda = new SqlDataAdapter())
{
cmd .Connection = con;
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value ;
ddl.DataBind();
con.Close();
}
}
ddl.Items.Insert( 0 new ListItem(defaultText, 0));
}





我的DropdownList源代码:



< asp:DropDownList ID = ddlAppClass runat = server AutoPostBack = True 高度 = 20px 宽度 = 188px OnSelectedIndexChanged = ddlAppClass_SelectedIndexChanged EnableViewState = true > < / asp:DropDownList >





阅读价值:

 如果 this  .ddlAppClass.SelectedIndex!=  0 
{
SchoolAge( this 跨度> .ddlAppClass.SelectedItem.Text);
}





SchoolAge方法:



 private void SchoolAge(string _classType)
{
try
{
var select =SELECT [ClassType],[age] FROM [Corona Schools_ Trust Council


AddAge] WHERE [ClassType] = @ClassType;
SqlDataReader rder = null;
using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings [DatabaseDemo Database NAV(8-0)1]。ConnectionString))
{
using(var command = new SqlCommand(select) ,connection))
{
command.CommandType = CommandType.Text;
command.Parameters.Add(@ ClassType,SqlDbType.VarChar,50).Value = _classType;
connection.Open();
rder = command.ExecuteReader();
while(rder.Read())
{

ValidateAge.Text =(rder [age]。ToString());

}

}
}
}
catch(SqlException ex)
{
// lblMsg。 Text = ex.Message;
//lblMsg.ForeColor = System.Drawing.Color.Red;
}

}







感谢您的协助!


 <   asp:DropDownList     ID   =  ddlAppClass    runat   =  server    AutoPostBack   =  True   高度  =  20px   宽度  =  188px    OnSelectedIndexChanged   =  ddlAppClass_SelectedIndexChanged    EnableViewState   =  true >  <   / asp:DropDownList  >  

< asp:TextBox ID = txtDateOfBirth OnTextChanged = txtDateOfBirth_TextChan ged AutoPostBack = true runat = server / >

所选项目:< asp:Literal ID = LiteralResult runat = server / >




代码隐藏



 受保护  void  Page_Load( object  sender,EventArgs e)
{
if (!Page.IsPostBack)
{
string query = string .Format( SELECT [教室类别],[描述] FROM [Corona Schools_ Trust Council

Hello Everyone,

i have an issue i have been battling with for sometime now, my dynamic DropDownList always return to the first item from database, i have done a few researches on how to resolve this including enabling Viewstate for the page and DropDownList and placing the code on if(!Page.IsPostback) but the issue persists!

Here is my code:

What I have tried:

if (!Page.IsPostBack)
                {
                    string appType2 = Session["applicationType"].ToString();
                    if (appType2 == "nur")
                    {

                        FetchApplicationFeeFromDB("NURSERY");
                        this.ddlAppClass.Items.Clear();
                        this.ddlApplicationType.Items.Insert(0, "NURSERY SCHOOL");

                        string query = string.Format("SELECT [Teaching Room Category] ,[Description] FROM [Corona Schools_ Trust Council$Teaching Room Level] WHERE [Teaching Room Category] = '{0}'", "NURSERY");
                        BindDropDownList(this.ddlAppClass, query, "Description", "Teaching Room Category", "---Select---");
                    }

                }



Binding Method:

private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
    {
        string conString = ConfigurationManager.ConnectionStrings["DatabaseDemo Database NAV (8-0)1"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                con.Open();
                ddl.DataSource = cmd.ExecuteReader();
                ddl.DataTextField = text;
                ddl.DataValueField = value;
                ddl.DataBind();
                con.Close();
            }
        }
        ddl.Items.Insert(0, new ListItem(defaultText, "0"));
    }



My DropdownList Source code:

<asp:DropDownList ID="ddlAppClass" runat="server" AutoPostBack="True" Height="20px" Width="188px" OnSelectedIndexChanged="ddlAppClass_SelectedIndexChanged" EnableViewState="true"></asp:DropDownList>



Reading Value:

if (this.ddlAppClass.SelectedIndex != 0)
                {
                    SchoolAge(this.ddlAppClass.SelectedItem.Text);                    
                }



SchoolAge Method:

private void SchoolAge(string _classType)
        {
            try
            {
                var select = "SELECT [ClassType],[age] FROM [Corona Schools_ Trust Council$AddAge] WHERE [ClassType] = @ClassType";
                SqlDataReader rder = null;
                using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseDemo Database NAV (8-0)1"].ConnectionString))
                {
                    using (var command = new SqlCommand(select, connection))
                    {
                        command.CommandType = CommandType.Text;
                        command.Parameters.Add("@ClassType", SqlDbType.VarChar, 50).Value = _classType;
                        connection.Open();
                        rder = command.ExecuteReader();
                        while (rder.Read())
                        {

                            ValidateAge.Text = (rder["age"].ToString());

                        }

                    }
                }
            }
            catch (SqlException ex)
            {
                //lblMsg.Text = ex.Message;
                //lblMsg.ForeColor = System.Drawing.Color.Red;
            }

        }




I appreciate your assistance!

Teaching Room Level] WHERE [Teaching Room Category] = '{0}'", "NURSERY"); BindDropDownList(this.ddlAppClass, query, "Description", "Teaching Room Category", "---Select---"); } }



Binding Method:

private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
    {
        string conString = ConfigurationManager.ConnectionStrings["DatabaseDemo Database NAV (8-0)1"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                con.Open();
                ddl.DataSource = cmd.ExecuteReader();
                ddl.DataTextField = text;
                ddl.DataValueField = value;
                ddl.DataBind();
                con.Close();
            }
        }
        ddl.Items.Insert(0, new ListItem(defaultText, "0"));
    }



My DropdownList Source code:

<asp:DropDownList ID="ddlAppClass" runat="server" AutoPostBack="True" Height="20px" Width="188px" OnSelectedIndexChanged="ddlAppClass_SelectedIndexChanged" EnableViewState="true"></asp:DropDownList>



Reading Value:

if (this.ddlAppClass.SelectedIndex != 0)
                {
                    SchoolAge(this.ddlAppClass.SelectedItem.Text);                    
                }



SchoolAge Method:

private void SchoolAge(string _classType)
        {
            try
            {
                var select = "SELECT [ClassType],[age] FROM [Corona Schools_ Trust Council


AddAge] WHERE [ClassType] = @ClassType"; SqlDataReader rder = null; using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseDemo Database NAV (8-0)1"].ConnectionString)) { using (var command = new SqlCommand(select, connection)) { command.CommandType = CommandType.Text; command.Parameters.Add("@ClassType", SqlDbType.VarChar, 50).Value = _classType; connection.Open(); rder = command.ExecuteReader(); while (rder.Read()) { ValidateAge.Text = (rder["age"].ToString()); } } } } catch (SqlException ex) { //lblMsg.Text = ex.Message; //lblMsg.ForeColor = System.Drawing.Color.Red; } }




I appreciate your assistance!


<asp:DropDownList ID="ddlAppClass" runat="server" AutoPostBack="True" Height="20px" Width="188px" OnSelectedIndexChanged="ddlAppClass_SelectedIndexChanged" EnableViewState="true"></asp:DropDownList>

<asp:TextBox ID="txtDateOfBirth" OnTextChanged="txtDateOfBirth_TextChanged" AutoPostBack="true" runat="server" />

Selected item: <asp:Literal ID="LiteralResult" runat="server" />



code-behind

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string query = string.Format("SELECT [Teaching Room Category] ,[Description] FROM [Corona Schools_ Trust Council