我在页面中有一个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(); }
}
非常感谢你,它现在正常工作 – 2013-02-10 18:33:44
很高兴它做到了,随时接受它作为答案:) – 2013-02-10 18:35:10