按钮上的绑定网格视图单击列表视图

时间:2015-01-13 09:42:16

标签: c# asp.net

我有一个列表视图,每行都有一个按钮:

 <td> <asp:Button ID="Button2" runat="server" Text="select" CommandName="view" CommandArgument='<%# Eval("inquiry_id")%>'   onclick="buttonClick"/></td>

单击此按钮,我将检索所单击行的ID,使用相同的ID设置会话并绑定网格视图。此按钮后面的代码单击列表视图:

ListViewItem item = (sender as Button).NamingContainer as ListViewItem;
    Button butDetails = (Button)item.FindControl("Button2");
    Int64 inquiryID = Convert.ToInt64(butDetails.CommandArgument);
    Session["session_view_id"] = inquiryID;
    this.BindGrid();               
    return;

在bindGrid函数中检索会话。但问题是第一次点击时不显示gridview,但是第二次点击它会显示,但是点击了前一个id的数据。会话已设置但是在绑定网格时它会使用旧的会话值。我错在哪里绑定网格? bindGrid()函数的代码是:

 int inquiryID = Convert.ToInt32(Session["session_view_id"]);
   MySqlConnection conn = null;
    try
    {          MySqlCommand cmd = new MySqlCommand("SELECT * FROM crm_support_inquiry inner join  crm_inquiry_perticipant on crm_support_inquiry.inquiry_id=?id inner join crm_mailer_types on crm_support_inquiry.mailer_id=crm_mailer_types.mailer_id limit 4", connect);

        using (MySqlDataAdapter sda = new MySqlDataAdapter())
        {
            cmd.Parameters.AddWithValue("?id", inquiryID);
            cmd.Connection = connect;
            sda.SelectCommand = cmd;
            using (DataTable dt = new DataTable())
           {

               sda.Fill(dt);
                lblComp.Text = dt.Rows[0]["company"].ToString();
                lblCname.Text = dt.Rows[0]["contact_name"].ToString();
              lblEmail.Text = dt.Rows[0]["email"].ToString();
                GridView1.DataSource = dt;
                GridView1.DataBind();
           }
        }

1 个答案:

答案 0 :(得分:0)

您是否在Page_Load方法中检查IsPostBack?您需要在Page_Load中将您的BindGrid调用包装在if(!IsPostBack)语句中,以防止Page_Load最初在回发时刷新数据。这可以防止您稍后在事件处理程序中进行的更改。

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        BindGrid();
    }
}

此答案可提供其他信息:How to update page data after event handling?