【问题标题】:Handling multiple buttons in same asp.net page处理同一asp.net页面中的多个按钮
【发布时间】:2014-04-03 18:03:57
【问题描述】:

大家好,我在同一个 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();
    }
}

但问题是当我点击第一个按钮时,与第二个按钮相关的验证控件不允许重新加载页面,因此我无法获取数据。

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

【问题讨论】:

  • 我认为你应该把它们放在不同的<form>s中。
  • 我假设您使用的是webforms。是这样吗?这个question may be of use
  • 试试url?=username=username;DROP DATABASE swa 并学习如何避免 SQL 注入。

标签: c# asp.net ado.net


【解决方案1】:

我认为您可以使用“验证组”来解决您的问题。 http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-19
    • 2016-03-17
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多