Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理同一asp.net页面中的多个按钮_C#_Asp.net_Ado.net - Fatal编程技术网

C# 处理同一asp.net页面中的多个按钮

C# 处理同一asp.net页面中的多个按钮,c#,asp.net,ado.net,C#,Asp.net,Ado.net,大家好,我在同一个asp.net网页上使用两个按钮。两个按钮都包含不同的代码 第一个按钮从数据库获取数据这里是代码 protected void Button1_Click(object sender, EventArgs e) { string username = Request.QueryString["username"]; SqlConnection conn = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;I

大家好,我在同一个asp.net网页上使用两个按钮。两个按钮都包含不同的代码 第一个按钮从数据库获取数据这里是代码

protected void Button1_Click(object sender, EventArgs e)
{
    string username = Request.QueryString["username"];
    SqlConnection conn = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=swa1;User Id=swa1;Password=swa1;");
    conn.Open();

    try 
    {
        string checkaddress = "select address,city,zipcode from regforswa where username=" + username;
        SqlCommand com = new SqlCommand(checkaddress, conn);
        using (var reader = com.ExecuteReader())
        {
            while (reader.Read())
            {
                var tmp = reader["address"];
                if (tmp != DBNull.Value)
                {
                    laddress.Visible = true;
                    laddress.Text = reader["address"].ToString();
                }
                var cty = reader["city"];
                if (cty != DBNull.Value)
                {
                    lcity.Visible = true;
                    lcity.Text = reader["city"].ToString();
                }
                var zip = reader["zipcode"];
                if (zip != DBNull.Value)
                {
                    lzipcode.Visible = true;
                    lzipcode.Text = reader["zipcode"].ToString();
                }
            }
        }
    }
    finally
    {
        conn.Close();
    }
}
protected void submit_Click(object sender, EventArgs e)
{
    string username = Request.QueryString["username"];
    string address=TextBox4.Text;
    string city=TextBox5.Text;
    string zipcode=TextBox6.Text;
    SqlConnection conn = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=swa1;User Id=swa1;Password=swa1;");
    conn.Open();

    try 
    {
        string updateaddress = "UPDATE regforswa SET address=@address,city=@city,zipcode=@zipcode WHERE username="+username;
       SqlCommand com = new SqlCommand(updateaddress, conn);
       com.Parameters.AddWithValue("@address",address);
       com.Parameters.AddWithValue("@city",city);
       com.Parameters.AddWithValue("@zipcode",zipcode);
       // com.Parameters.AddWithValue("@username",username);
       if (com.ExecuteNonQuery() == 1)
       {
           result.Visible = true;
           result.Text = "congradulations.your address has been changed";
       }
       else
       {
           result.Visible = true;
           result.Text = "sorry please try again"; 
       }
    }
    catch(Exception ex)
    {
        Response.Write(ex.Message);
    }
    finally
    {
        conn.Close();
    }
}
第二个按钮使用文本框值更新数据库中的值。下面是代码

protected void Button1_Click(object sender, EventArgs e)
{
    string username = Request.QueryString["username"];
    SqlConnection conn = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=swa1;User Id=swa1;Password=swa1;");
    conn.Open();

    try 
    {
        string checkaddress = "select address,city,zipcode from regforswa where username=" + username;
        SqlCommand com = new SqlCommand(checkaddress, conn);
        using (var reader = com.ExecuteReader())
        {
            while (reader.Read())
            {
                var tmp = reader["address"];
                if (tmp != DBNull.Value)
                {
                    laddress.Visible = true;
                    laddress.Text = reader["address"].ToString();
                }
                var cty = reader["city"];
                if (cty != DBNull.Value)
                {
                    lcity.Visible = true;
                    lcity.Text = reader["city"].ToString();
                }
                var zip = reader["zipcode"];
                if (zip != DBNull.Value)
                {
                    lzipcode.Visible = true;
                    lzipcode.Text = reader["zipcode"].ToString();
                }
            }
        }
    }
    finally
    {
        conn.Close();
    }
}
protected void submit_Click(object sender, EventArgs e)
{
    string username = Request.QueryString["username"];
    string address=TextBox4.Text;
    string city=TextBox5.Text;
    string zipcode=TextBox6.Text;
    SqlConnection conn = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=swa1;User Id=swa1;Password=swa1;");
    conn.Open();

    try 
    {
        string updateaddress = "UPDATE regforswa SET address=@address,city=@city,zipcode=@zipcode WHERE username="+username;
       SqlCommand com = new SqlCommand(updateaddress, conn);
       com.Parameters.AddWithValue("@address",address);
       com.Parameters.AddWithValue("@city",city);
       com.Parameters.AddWithValue("@zipcode",zipcode);
       // com.Parameters.AddWithValue("@username",username);
       if (com.ExecuteNonQuery() == 1)
       {
           result.Visible = true;
           result.Text = "congradulations.your address has been changed";
       }
       else
       {
           result.Visible = true;
           result.Text = "sorry please try again"; 
       }
    }
    catch(Exception ex)
    {
        Response.Write(ex.Message);
    }
    finally
    {
        conn.Close();
    }
}
但问题是,当我点击第一个按钮时,与第二个按钮相关的验证控件不允许重新加载页面,因此我无法获取数据


我的问题是,我们可以在同一个网页上使用两个按钮,但功能不同吗?

我认为您可以使用“验证组”来解决问题

我想你应该把它们放在不同的
中。我假设你正在使用。是这样吗?这个Try
url?=username=username;删除数据库swa
,学习如何避免SQL注入。