c#可点击的gridview

时间:2017-04-26 07:43:52

标签: c# asp.net gridview

我来到这里感谢非常好的教程。但问题是页面重新加载,所以点击工作,但其余的代码没有通过......任何想法为什么?

 protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
            e.Row.ToolTip = "Click to select this row.";
        }
    }
    protected void OnSelectedIndexChanged(object sender, EventArgs e)
    {

            int index = GridView1.SelectedRow.RowIndex;
            string Firstname = GridView1.SelectedRow.Cells[0].Text;
            string Surname = GridView1.SelectedRow.Cells[1].Text;
            string ID = GridView1.SelectedRow.Cells[2].Text;
            string Occupation = GridView1.SelectedRow.Cells[3].Text;
            string Place = GridView1.SelectedRow.Cells[4].Text;
            string Phone = GridView1.SelectedRow.Cells[5].Text;
            string Cellular = GridView1.SelectedRow.Cells[6].Text;
            string Mail = GridView1.SelectedRow.Cells[7].Text;
            Label1.Text = Firstname;
            Label2.Text = Surname;
            Label3.Text = ID;
            Label4.Text = Occupation;
            Label5.Text = Place;
            Label6.Text = Phone;
            Label7.Text = Cellular;
            Label8.Text = Mail;
            Panel1.Visible = true;


    }

1 个答案:

答案 0 :(得分:0)

你必须注册event handler method,f.e。在aspx上:

<asp:GridView ID="ContactsGridView"
    DataSourceID="YourGridId" 
    OnSelectedIndexChanged="OnSelectedIndexChanged"        
    OnRowDataBound="OnRowDataBound" 
    runat="server">
    <SelectedRowStyle BackColor="Aqua" />
  </asp:GridView>