【问题标题】:Textboxes dont update when dropdownlist value is changed ASP.NET更改下拉列表值时文本框不更新 ASP.NET
【发布时间】:2013-12-30 11:27:21
【问题描述】:

我有四个文本框,我需要根据下拉列表中的选择更改它们的值。但是当我更改值时,文本框不会更新

我的代码:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
 DataSourceID="Employees"  DataTextField="FullName" DataValueField="FullName"           
 OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
    <asp:SqlDataSource ID="Employees" runat="server" 
     ConnectionString="<%$ConnectionStrings:MyConnectionString %>"     
     SelectCommand="SELECT [FullName] FROM [Employees]   ORDER BY      [FirstName]">           
   </asp:SqlDataSource>

还有类文件:

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string cn = "Data Source=.;Initial Catalog=DBRE ;Integrated Security=True";
        SqlConnection scon = new SqlConnection(cn);

        SqlCommand scmd = new SqlCommand("Select * from Employees where FullName = '" + DropDownList1.SelectedItem.Value + "'", scon);
        SqlDataReader sdr;

        try
        {
           scon.Open();
           sdr = scmd.ExecuteReader();
           txtName.Text = sdr["FirstName"].ToString();
           txtSurname.Text = sdr["LastName"].ToString();
           txtDepartment.Text=sdr["Dept"].ToString();
           txtCostCentre.Text=sdr["CostCentre"].ToString();
        }
        catch (Exception ex)
        {
        }

        finally
        {
            scon.Close();
        }

我在这里做错了什么?

【问题讨论】:

  • 在下拉列表中更改索引时是否发生回发?
  • 首先移除空的 catch 块。如果你有空的 catch 块,你不会看到任何异常。

标签: c# asp.net


【解决方案1】:

我可能认为检查异常,使用 Reader like 或使用 ExecuteScalar

using (var reader = cmd.ExecuteReader())
    {
        if (reader.Read())
        {
            learerLabel.Text = reader.GetString(reader.GetOrdinal("somecolumn"))
        }
    }

检查一下

dynamically filled DropDownList does not retain value on postback ASP.net c#

试试 EnableViewState="true"

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"  EnableViewState="true" DataSourceID="Employees"  DataTextField="FullName" DataValueField="FullName"          
 OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>

【讨论】:

  • 不是例外,因为我已经转换为 Tostring
  • 你能与文本框分享代码吗?你在使用更新面板吗?试试 EnableViewState="true"
【解决方案2】:

使用 AutoPostBack="true" 设置您的下拉列表,并将所选索引更改如下:

     <asp:DropDownList ID="ddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlPNo_selectedChanged" ></asp:DropDownList>

并在您的代码中使用以下代码将数据加载到您的下拉列表中:

     String conn = System.Configuration.ConfigurationManager.ConnectionStrings["CONSTRING"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(conn);
            SqlDataAdapter da = new SqlDataAdapter("select Code from table1", con);
            DataSet ds = new DataSet();
            con.Open();
            da.Fill(ds);
            con.Close();
            ddl.DataSource = ds.Tables[0];
            ddl.DataValueField = "Code";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("-- select --"));
        }
    }

现在您可以在下拉列表上绑定数据,更改为以下代码:

     protected void ddl_selectedChanged(object sender, EventArgs e)
    {
        String strConnString = ConfigurationManager.ConnectionStrings["CONSTRING"].ConnectionString;
        String strQuery = "select description from table1 where Code = @Code";
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();

        cmd.Parameters.AddWithValue("@Code", ddl.SelectedItem.Value);
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQuery;
        cmd.Connection = con;

        try
        {
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                txtbdescription.Text = dr["description"].ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }

其中 txtbdescription 是您的文本框的 ID。

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    删除AutoPostBack="True"

    <asp:DropDownList ID="DropDownList1" runat="server"
     DataSourceID="Employees"  DataTextField="FullName" DataValueField="FullName"           
     OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    

    并添加 if (!ispostback) 条件

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
           if (!isPostback)
            {
            string cn = "Data Source=.;Initial Catalog=DBRE ;Integrated Security=True";
            SqlConnection scon = new SqlConnection(cn);
    
            SqlCommand scmd = new SqlCommand("Select * from Employees where FullName = '" + DropDownList1.SelectedItem.Value + "'", scon);
    
            SqlDataReader sdr;
    
            try
            {
               scon.Open();
    
               sdr = scmd.ExecuteReader();
               txtName.Text = sdr["FirstName"].ToString();
               txtSurname.Text = sdr["LastName"].ToString();
               txtDepartment.Text=sdr["Dept"].ToString();
               txtCostCentre.Text=sdr["CostCentre"].ToString();
            }
    
            catch (Exception ex)
            {
            }   
            finally
            {
                scon.Close();
            }
        }
    }
    

    【讨论】:

    • 如果"AutoPostBack="False""怎么会有回帖?
    • 尝试添加:if (!isPostback) 但收到错误消息“名称为 !postback 在当前上下文中不存在
    • @user3058255 你需要包含system.web.page,所以使用!Page.IsPostBack更容易,我也应该大写
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    相关资源
    最近更新 更多