【发布时间】: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