什么是回发属性以及如何使用它? [英] What is is-postback property and how to use that ?

查看:84
本文介绍了什么是回发属性以及如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class Dataset1 : System.Web.UI.Page
    {
    
        SqlConnection con = new SqlConnection("Data Source=HEMANTH-PC;Initial Catalog=Sample;Persist Security Info=True;User ID=sa;Password=abc");
      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataBind();
            }
        }
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("insert into tblEmployee(Name,DepartmentId,Location,Salary)values(@Name,@DepartmentId,@Location,@Salary)", con);
                    cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@DepartmentId", TextBox2.Text);
                    cmd.Parameters.AddWithValue("@Location", TextBox3.Text);
                    cmd.Parameters.AddWithValue("@Salary", TextBox4.Text);
                    cmd.CommandType = CommandType.Text;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    Console.WriteLine("Data Inserted Successfully");
                          
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            } 





我已编写上述代码将数据插入数据库。

这里我的问题是我在点击插入按钮时没有在gridveiw控件中获取插入的数据。上面的代码是正确的我是否以正确的方式使用ispostback请事先知道谢谢

当我刷新页面时我能够获取数据但是没有发布页面返回如何实现?



i have written the above code to insert the data into database.
here my problem is i am not getting the inserted data in gridveiw control when clicking insert button. Is the above code is correct am i used the ispostback in a right way or not please let me know thanks in advance
when i refreshing the page i can able to get the data but with out posting the page back how to achieve that?

推荐答案

void DataBindMethod()
{
 con.Open();
SqlDataAdapter da= new SqlDataAdapter("insert * from tblEmployee", con);  
DataSet ds=new DataSet();
Da.Fill(ds); 
 con.Close();
GridView1.DataSource=ds;
GridView1.DataBind();
}







protected void Button1_Click(object sender, EventArgs e)
 { try{
 SqlCommand cmd = new SqlCommand("insert into tblEmployee(Name,DepartmentId,Location,Salary)values(@Name,@DepartmentId,@Location,@Salary)", con);
 cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
 cmd.Parameters.AddWithValue("@DepartmentId", TextBox2.Text);
 cmd.Parameters.AddWithValue("@Location", TextBox3.Text);
 cmd.Parameters.AddWithValue("@Salary", TextBox4.Text);
 cmd.CommandType = CommandType.Text;
 con.Open();
 cmd.ExecuteNonQuery();
 con.Close();
 Console.WriteLine("Data Inserted Successfully");
DataBindMethod();//Call your Databind method
 }
 catch (Exception ex)
 {
 Console.WriteLine(ex);
 }


如果你想要每次刷新数据,那么取出

if(!IsPostBack)
If you want data every refresh then take out
if (!IsPostBack)


这篇关于什么是回发属性以及如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆