问题
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??
回答1:
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.
来源:https://stackoverflow.com/questions/7090086/add-onclick-event-for-asp-net-grid-view