1

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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

What is the meaning of ** that you have written??
I highlighted necessary changes. I thought they'll become bold, but code editor doesn't support it (
What onclick atribute is rendred for row? <tr onclick="javascript:__doPostBack('btnTest','')"> like this?
Not getting what you are asking..:(
Please, open Firebug(or something simmilar) or click View Source in your browser. Your GridView is render to html like this '<table...attributes...><tbody><tr></tr>...<tr></tr></tbody></table>' Every 'tr' tag has onclick attribute. What is value of attribute?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.