为 ASP.NET 网格视图添加 OnClick 事件

发布于 2024-11-29 17:44:27 字数 2765 浏览 1 评论 0原文

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

I have added new event i.e Gridview_Onclick event dynamically in RowDatabound event. But when I try to handle that event in the server side code, I can code for that event. This is the code for my grid view.

<%@ 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>

This is the code for my ascs.cs page

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";
}

The grid view opens in modelpopupextender so when I click anywhere in grid, its getting closed which is not the case of I remove the event that is added dynamically. In short the event is added but can not be handled in code.
Please guide me how can I catch this event in coding??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋檐 2024-12-06 17:44:27

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

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

2)

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

3)

为什么?

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

You need to do following changes at your code:
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"** />

Why?

The reason is GridViewRow doesn't implement IPostBackEventHandler, therefore doesn''t fire postback events. But button can. At this case btnTest it is dummy button for event firing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文