为asp.net网格视图添加OnClick事件

时间:2011-08-17 08:50:51

标签: asp.net ajax modalpopupextender

我在RowDatabound事件中动态添加了新事件,即Gridview_Onclick事件。但是当我尝试在服务器端代码中处理该事件时,我可以为该事件编写代码。这是我的网格视图的代码。

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PopUp.ascx.cs" Inherits="PopUp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<table class="style1">
<tr>
    <td>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CellPadding="4" ForeColor="#333333" GridLines="None" 
            onrowdatabound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField DataField="DeptId" HeaderText="ID"/>
                <asp:BoundField DataField="Name" HeaderText="Name"/>
                <asp:BoundField DataField="HeadName" HeaderText="HOD"/>
                <asp:BoundField DataField="HeadEmail" HeaderText="Email ID"/>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
        <asp:Button ID="btnTest" runat="server" style="display:none" />
    </td>
</tr> 
</table>

这是我的ascs.cs页面的代码

protected void Page_Load(object sender, EventArgs e)
{
    oCommand.CommandText = "select * from Department";
    oCommand.Connection = oConnection;
    oAdapter.SelectCommand = oCommand;
    DataTable dt = new DataTable();
    oAdapter.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(e.Row, string.Empty);
}
protected void GridView1_OnClick(object sender, GridViewRowEventArgs e)
{
    _ID = "10";
    _Name = "abc";
    _HOD = "abc";
    _Email = "abc";
}

网格视图在modelpopupextender中打开,所以当我点击网格中的任何地方时,它会被关闭,这不是我删除动态添加的事件的情况。简而言之,事件已添加但无法在代码中处理。 请指导我如何在编码中捕获此事件?

1 个答案:

答案 0 :(得分:1)

您需要在代码中进行以下更改: 1)

 e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(**btnTest**, string.Empty);

2)

 protected void GridView1_OnClick(object sender, **EventArgs** e)

3)<asp:Button ID="btnTest" runat="server" Style="display: none" **OnClick="GridView1_OnClick"** />

为什么?

原因是GridViewRow没有实现IPostBackEventHandler,因此不会触发回发事件。但按钮可以。在这种情况下,btnTest是用于事件触发的虚拟按钮。