Monday, 23 April 2012

Find Control In Grid View

Find Control In Grid View

Here We Will Find Control In Grid View From Two Well Know Events RowCommand,DataBound And Delete,Update.



1)In Normal Grid View DataBound


 if (e.Row.RowType == DataControlRowType.DataRow)
        {
                     string NormalValue=     ((Label)e.Row.FindControl(" Label 2")).Text ;
        }


   



2)In Normal GridView RowCommand


for (int i = 0; i <  GridView1.Rows.Count; i++)
            {
           
         ((Label)GridView1.Rows[i].FindControl("Label1")).Text);
         ((Image) GridView1 .Rows[i].FindControl("Image1")).ImageUrl);

             }





3)In Normal GridView Delete,Update Command

((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text);


If You Have Any Data Key Then...


GridView1.DataKeys(e.Row.RowIndex).Value.ToString()





4)When its Nested  GridView  than in row command you can find any (lable,linkbutton,button,textbox)value by putting this..


            LinkButton btn = e.CommandSource as LinkButton;//the button in which this event fired
            GridViewRow row= btn.NamingContainer as GridViewRow;
            Label POST = row.FindControl("Label1") as Label;
            Label COMMENTID = row.FindControl("Label2") as Label;
            LinkButton Likelinkbutton = row.FindControl(" LinkButton1") as LinkButton;


or direct

string valuefromdatabase= e.CommandArgument.ToString();

if you have binded control with CommandArgument!



5)Nested Grid View DataBound


 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridViewRow row = e.Row;
           string commentingid= ((Label)row.FindControl( Label 2")).Text;
         }


















No comments:

Post a Comment

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...