动态下拉列表始终返回第一个值,而不是选定的值 [英] Dynamic dropdownlist is always returning the first value, not the selected value

查看:58
本文介绍了动态下拉列表始终返回第一个值,而不是选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,



i有一个问题,我一直在争夺一段时间,我的动态DropDownList总是从数据库返回第一项,我做了一个关于如何解决这个问题的研究很少,包括为页面和DropDownList启用Viewstate并将代码放在if(!Page.IsPostback)上,但问题仍然存在!



这是我的代码:



我的尝试:



  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 [教室类别],[描述] FROM [Corona Schools_ Trust Council $ Teaching Room Level] WHERE [教室类别] ='{0} ' NURSERY);
BindDropDownList( this .ddlAppClass,query, 描述 教室类别 ---选择---);
}

}





绑定方法:



  private   void  BindDropDownList(DropDownList ddl,< span class =code-keyword> string  query, string  text, string   value  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源代码:



< pre lang =HTML> < asp:DropDownList ID = ddlAppClass runat = server AutoPostBack = True 高度 = 20px 宽度 = 188px OnSelectedIndexChanged = ddlAppClass_SelectedIndexChanged EnableViewState = true > < < span class =code-leadattribute> / 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;
}

}







感谢您的协助!

解决方案

教室水平] 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源代码:



< pre lang =HTML> < asp:DropDownList ID = ddlAppClass runat = server AutoPostBack = True 高度 = 20px 宽度 = 188px OnSelectedIndexChanged = ddlAppClass_SelectedIndexChanged EnableViewState = true > < < span class =code-leadattribute> / 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   = < span class =code-keyword> ddlAppClass    runat   =  server    AutoPostBack   =  True   高度  =  20px   宽度  =  188px    OnSelectedIndexChanged   =  ddlAppClass_SelectedIndexChanged    EnableViewState   =  true >  <   / asp:DropDownList  >  

< asp:TextBox ID = txtDateOfBirth OnTextChanged = < span class =code-keyword> txtDateOfBirth_TextChan ged AutoPostBack = true runat = server / >

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



< br / >
代码隐藏



 受保护  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


这篇关于动态下拉列表始终返回第一个值,而不是选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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