简体   繁体   English

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

[英]binding grid view on button click in listview

I have a list view with a button for each row: 我有一个列表视图,每一行都有一个按钮:

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

On click of this button i retrieve the id of the clicked row, set the session with the same id and bind the grid view.This the code behind the button click in list view: 单击此按钮后,我将获取被单击行的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;

The session is retrieved in bindGrid function. 在bindGrid函数中检索会话。 But the problem is gridview is not displayed at first click but on second click it gets displayed but with the data of the previous id clicked.The session is set but while binding the grid it uses old session value. 但是问题是,第一次单击时不会显示gridview,但是在第二次单击时会显示gridview,但单击的是先前id的数据。设置了会话,但是在绑定网格时会使用旧的会话值。 Where am i going wrong to bind the grid? 我在哪里绑定网格错了? The code for the bindGrid() function is : 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();
           }
        }

Are you checking for IsPostBack in your Page_Load method? 您是否要在Page_Load方法中检查IsPostBack? You need to wrap your BindGrid call in your Page_Load in an if(!IsPostBack) statement to prevent the Page_Load from initially refreshing the data on postback. 您需要在if(!IsPostBack)语句中将BindGrid调用包装在Page_Load中,以防止Page_Load最初在回发时刷新数据。 This can prevent the changes you are making later on in your event handler. 这样可以防止您稍后在事件处理程序中进行更改。

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

This answer can provide additional info: How to update page data after event handling? 此答案可以提供其他信息: 事件处理后如何更新页面数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM