vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Thursday, January 19, 2012

Find a control in gridview or Datalist control asp.net


Hi..
I share the bit of code to find the control present in the grid view..
For many instance we need to get the value or check the condition of the conrol..that time first we need to find the control in grid view or data list..
They are different ways to find the control..
C#
for (int i = 0; i < GridView1.Rows.Count; i++)
{
    if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
    {
        CheckBox chk =
         (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
        if (chk.Checked)
        {
}
}}

 VB
For i As Integer = 0 To GridView1.Rows.Count - 1
   If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
     Dim chk As CheckBox = _
     DirectCast(GridView1.Rows(i).Cells(0) _
     .FindControl("CheckBox1"), CheckBox)
     
     If chk.Checked Then
End if
End if
Next

C#
foreach(GridViewRow row in GridView1.Rows)
            {
                var chkBox = row.FindControl("chkSelect") as CheckBox;

                if(chkBox.Checked)
                {
}}

Finding textbox in gridview..
TextBox txt1;
String str;
 txt1 = GridView1.Rows[GridView1.EditIndex].FindControl("textbox1") asTextBox;
str=txt1.text;

i hope this one be useful

0 comments:

Post a Comment