【问题标题】:on selecting the dropdownlist 2 nd time textbox value not showing blank在选择下拉列表第二次文本框值不显示空白
【发布时间】:2013-03-28 15:07:51
【问题描述】:

我写了以下代码。

我的第一个 orderid 有进程 1 和进程 2 的记录 但我的 2nd orderid 仅记录流程 1。

当我在选择 orderid 1 后选择 orderid 2 时,它会保留进程 2 文本框中的 orderid 1 的值。 我希望那个文本框是空白的

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DbConnect objdbc = new DbConnect();
    SqlConnection con = objdbc.openConnection();
    SqlCommand cmd = new SqlCommand("SELECT [orderid],[processid],[orgid],[processdesc] ,[perwet] FROM [anghan].[dbo].[ProcessOrder] where orderid='" + DropDownList1.SelectedItem.Value + "' and processid=1 ", con);

    cmd.CommandType = CommandType.Text;
    // cmd.Parameters.AddWithValue("@Id", value);
    cmd.Connection = con;
    //con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        TextBox2.Text = dr["perwet"].ToString();
        //  DropDownList1.Items(DropDownList1.SelectedIndex).Text = dr["service"].ToString();
    }
    dr.Close();
    //process 2

    SqlCommand cmd2 = new SqlCommand("SELECT [orderid],[processid],[orgid],[processdesc] ,[perwet] FROM [anghan].[dbo].[ProcessOrder] where orderid='" + DropDownList1.SelectedItem.Value + "' and processid=2 ", con);

    cmd2.CommandType = CommandType.Text;
    // cmd.Parameters.AddWithValue("@Id", value);
    cmd2.Connection = con;
    //con.Open();
    SqlDataReader dr2 = cmd2.ExecuteReader();
    while (dr2.Read())
    {

        TextBox3.Text = dr2["perwet"].ToString();
        //  DropDownList1.Items(DropDownList1.SelectedIndex).Text = dr["service"].ToString();
    }
    dr2.Close();

【问题讨论】:

  • 请详细说明。好像很迷茫。
  • 我同意,问题需要更多细节

标签: c# asp.net


【解决方案1】:

为什么不在代码的开头将 TextBox2 和 TextBox3 设置为空。这样你就可以看到你已经填充了什么。只有那些文本框的值会改变你的代码将重新分配的位置,否则它将保留旧值。

【讨论】:

    【解决方案2】:
    just put like this>>
    
    
    
     if(dr!=null)
        {
            while (dr.Read())
                {
                    if((dr["perwet"]!=null)||(dr["perwet"].ToString()!=""))
                    TextBox2.Text = dr["perwet"].ToString();
                    else
                    TextBox2.Text="";
    
    
                    //  DropDownList1.Items(DropDownList1.SelectedIndex).Text = dr["service"].ToString();
                }
        }
    

    它会起作用的。

    【讨论】:

    • 先检查 NULL 条件 (dr["perwet"]!=null ,否则可能会报错
    • 我认为,如果 dr["perwet"]=null then (dr["perwet"].toString() must be "".[不确定,但它似乎]
    • @Freelancer 不,你错了。如果你在一个空对象上调用ToString,你会得到一个NullPointerException。
    • 否,但如果 C# 将尝试检查第一个条件 .tostring 将通过异常 as ,这在 NULL 对象上是不可能的
    • 哦,感谢 devesh 提供的宝贵信息。请查看代码编辑。
    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多