选择下拉列表第二个时间文本框值不显示空白

时间:2013-03-28 05:53:22

标签: c# asp.net

我写了以下代码。

我的第一个orderid包含进程1和进程2的记录 但是我的第二个订单只记录了流程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();

2 个答案:

答案 0 :(得分:0)

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();
            }
    }

它会起作用。

答案 1 :(得分:0)

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

相关问题