【问题标题】:I want my controls value.Insert only one time when I click the button我想要我的控件值。单击按钮时只插入一次
【发布时间】:2013-02-11 02:20:38
【问题描述】:

我的页面中有一个 DropDownlist 和一个文本框,当我单击按钮时,Button1_Click 事件触发并获取值 od 文本框和 droppdownlist 并将它们发送到我在数据库中的表中,它会向我显示结果(在我在数据库中的表)在gridview中。 我的问题是,如果我刷新页面,单击按钮后我的意思是单击重新加载当前页面,我的下拉列表值和文本框值再次插入我的表格中。 不知道怎么解决? 我的 gridview 使用 sqldatasource,这是我的代码:

   protected void Button1_Click(object sender, EventArgs e)
  {
    int brandid = Convert.ToInt32(BrandDropDownList.SelectedValue);
    String catname = TextBox2.Text;
    SqlConnection cn = new SqlConnection();
    cn.ConnectionString = "server = . ; database = mobile_store ; Trusted_Connection=true";
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = cn;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "FirstInitialiseCategories";
    cmd.Parameters.AddWithValue("@brandid ", brandid);
    cmd.Parameters.AddWithValue("@catname ", catname);
    try
    {
        cn.Open();
        cmd.ExecuteNonQuery();
        Label1.ForeColor = System.Drawing.Color.Green;
        Label1.Text = "با موفقیت ثبت شد!";
        SqlDataSource2.DataBind();

        GridView1.DataBind();
    }

    catch (Exception ex)
    {
        Label1.Text = ex.ToString();
        Label1.ForeColor = System.Drawing.Color.Red;
        Label1.Text = "خطا در ثبت!";
    }
    finally
    { cn.Close(); }
}

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    您可以在完成第一次插入后执行 HTTP 重定向(到同一页面),它应该可以解决问题:

    Response.Redirect("samePage.aspx");
    

    【讨论】:

      【解决方案2】:

      下拉列表中的第一项是什么?您可以为第一项设置空值,当您将值插入数据库时​​,您可以添加代码以清除文本框和下拉列表。

      终于 { cn.Close(); 标签1.Text =“”; ddl.SelectedIndex = 0; }

      【讨论】:

      • 谢谢你的回答,我之前试过了,但是没用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多