Add JavaScript to Asp:GridView example

protected void dgvCompanies_RowDataBound(object sender, GridViewRowEventArgs e)        
{                  
     if (e.Row.RowType == DataControlRowType.DataRow)                 
    {
            //Each row will then behave like a link, and when you select one it
            //can drive the behavior of another control(s) on your page
            //other way to select
            //e.Row.Attributes["ondblclick"] =
                 ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);                                       
            e.Row.Attributes["onmouseover"] =   
                     "this.style.cursor='hand';this.style.textDecoration='underline';this.style.color='red'";
            e.Row.Attributes["onmouseout"] =
                         "this.style.textDecoration='none';this.style.color='black';";

            string scriptFORonclick =
                        "Javascript:document.getElementById('hChosenCompany').value=" +
                        e.Row.Cells[0].Text + ";";
            string scriptFORondblclick = scriptFORonclick +
                        ";Javascript:document.getElementById('btnOpenCompany').click();";
             e.Row.Attributes.Add("ondblclick", scriptFORondblclick);
             e.Row.Attributes["onclick"] = scriptFORonclick;
    }//end if
}

u can also do specific cells like this:
e.Row.Cells[0].Attributes["onmouseover"] = "Javascript...";

to find a specific col by name:
int i = 0;
GridView gv = sender as GridView;
for (i = 0; i < gv.HeaderRow.Cells.Count; i++)                
{
       if (gv.HeaderRow.Cells[i].Text == "company_id")
           break;
}
string compId = e.Row.Cells[i].Text;
e.Row.Cells[0].Attributes["onclick"] = "OpenAttachDialog('" + compId + "');";



2 general tips:
i u use such a case u might get a heavy page, for 2k rows i got 2MB page and for 53 i got 67k
moving all the onmouse to 2 JS functions as OnMouseOver(this)/OnMouseOut(this) got me to 1.4MB/50K. then i did ViewStateMode="Disabled" on the grid and that got me to 650K/30K

Comments

Post a Comment

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem