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"] =   
            e.Row.Attributes["onmouseout"] =

            string scriptFORonclick =
                        "Javascript:document.getElementById('hChosenCompany').value=" +
                        e.Row.Cells[0].Text + ";";
            string scriptFORondblclick = scriptFORonclick +
             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")
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


Popular posts from this blog

c# Service Play Sound with NAudio example by Moshe

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

JS/JQ simulate Enter event