!IsPostBack总是返回true

时间:2015-03-07 04:19:13

标签: asp.net

我正在尝试为我的gridview实现搜索操作。当我第一次加载我的页面时!IsPostBack工作正常但是当我点击搜索按钮时我的页面再次加载并且!IsPostBack返回真值。所以,我无法执行我的搜索操作 这是我的代码。

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        ((Label)Master.FindControl("Label1")).Text = (string)Session["sname"];

        fillData();
    }
}

public void fillData()
{
    if (con.State == ConnectionState.Open)
    {
        con.Close();
    }

    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT * FROM EBR_Supplier where DeleteFlag=" + 0 + " ORDER BY RowId ASC", con);
    SqlDataAdapter adap = new SqlDataAdapter(cmd);

    DataSet ds = new DataSet();

    adap.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();    
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    fillData();
}

protected void search_Click(object sender, EventArgs e)
{
    string id = txtid.Text;
    if (con.State == ConnectionState.Open)
    {
        con.Close();
    }

    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT * FROM EBR_Supplier where DeleteFlag=" + 0 + " and SupplierId='" + id + "' ORDER BY RowId ASC", con);
    SqlDataAdapter adap = new SqlDataAdapter(cmd);

    DataSet ds = new DataSet();
    adap.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
}

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案我在母版页上有一个表单控件。我删除了它帮助了我。我感谢大家抽出时间回答我的问题。