【问题标题】:Add OnClick event for asp.net grid view为 asp.net 网格视图添加 OnClick 事件
【发布时间】:2011-08-17 08:50:51
【问题描述】:

我在 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 中打开,因此当我单击网格中的任意位置时,它会关闭,这不是我删除动态添加的事件的情况。简而言之,事件已添加但无法在代码中处理。 请指导我如何在编码中捕获此事件??

【问题讨论】:

    标签: asp.net ajax modalpopupextender


    【解决方案1】:

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

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

    2)

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

    3)&lt;asp:Button ID="btnTest" runat="server" Style="display: none" **OnClick="GridView1_OnClick"** /&gt;

    为什么?

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

    【讨论】:

    • 你写的**是什么意思??
    • 我强调了必要的更改。我以为它们会变粗,但代码编辑器不支持它(
    • 什么 onclick 属性被渲染为行? 喜欢这样吗?
    • 没有得到你的要求..:(
    • 请打开 Firebug(或类似的东西)或单击浏览器中的查看源代码。您的 GridView 像这样渲染到 html ' ... ' Every ' tr' 标签具有 onclick 属性。什么是属性值?
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签