locked
OnClick javascript handler in GridView RRS feed

  • Question

  • User-1912369449 posted

    Hi,

    First of all, thanks for developing the CSS friendly control adapters! [:)]

    The only problem I have found with the controls so far is that I can't seem to add javascript handlers to the generated HTML.

    For example, I like to create my GridViews so that a Gridview row can be selected with a mouseclick anywhere on the row. For example like this:

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick",
                                     Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex));
            }
        }

    This usually works fine, but after adding the CSS friendly GridView adapter the onclick handler is gone. Is there any workaround for this besides editing the Adapter code myself?

     Thanks,

     Adrian

     

    Tuesday, October 14, 2008 6:06 AM

Answers

  • User1564875471 posted

     Hi,

    Sorry I'm mistaken , 

    The RenderComplete event will be fired before RenderContents method , and so any changes will be overriden , 

    I think you need to change "GridViewAdapter.cs" file from the  css adapter source code and rebuild the source code , then add the new dll to your website ,

    to modify that file , download the Css adapters source code , and open "GridViewAdapter.cs" , go to line 166 and uncomment this code block :

     

    //Uncomment the following block of code if you want to add arbitrary attributes.
    /*
    foreach (string key in row.Attributes.Keys)
    {
        writer.WriteAttribute(key, row.Attributes[key]);
    }
    */

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 14, 2008 4:11 PM

All replies

  • User1564875471 posted

     Hi

    try to register the onclick in Page_PreRenderComplete event handler, this event will be fired after the Css adapters modify the control ( after render phase) , 

      

    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            row.Attributes.Add("onclick",
                                 Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + row.RowIndex));
        }
    }
     
    Tuesday, October 14, 2008 8:03 AM
  • User-1912369449 posted

     Thanks for your fast reply.

     

    I tried your suggestion, but unfortunately it only works as long as I do not use the CSS friendly GridView adapter. As soon as I turn the adapter on, the onclick method is, once again, missing.

     Thanks again for your help,

     Adrian

     

     

    Tuesday, October 14, 2008 9:28 AM
  • User1564875471 posted

     Hi,

    Sorry I'm mistaken , 

    The RenderComplete event will be fired before RenderContents method , and so any changes will be overriden , 

    I think you need to change "GridViewAdapter.cs" file from the  css adapter source code and rebuild the source code , then add the new dll to your website ,

    to modify that file , download the Css adapters source code , and open "GridViewAdapter.cs" , go to line 166 and uncomment this code block :

     

    //Uncomment the following block of code if you want to add arbitrary attributes.
    /*
    foreach (string key in row.Attributes.Keys)
    {
        writer.WriteAttribute(key, row.Attributes[key]);
    }
    */

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 14, 2008 4:11 PM
  • User-1912369449 posted

    Thanks, Anas! That's just what I was looking for and it worked great [:)]

    Friday, October 17, 2008 6:55 AM