This is a migrated thread and some comments may be shown as answers.

how to read chechbox

5 Answers 41 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jav
Top achievements
Rank 1
jav asked on 07 Nov 2008, 06:36 PM

Hi, all i want to do, is to know if  a checkbox is checked or not in every row of my grid, with the grid of visualstudio 2005 i ran this code when i clicked a button

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        For index As Integer = 0 To Me.GridView1.Rows.Count - 1
            Dim Valida As CheckBox = CType(Me.GridView1.Rows(index).FindControl("ColSel"), CheckBox)
            If Valida.Checked Then
               'do something
            End If
        Next
    End Sub

in aspx i define the checkbox column like this

<telerik:GridTemplateColumn UniqueName="RevCol">

 

 

<ItemTemplate>

 

 

<asp:CheckBox ID="CheckBox1" runat="server" />

 

 

</ItemTemplate>

 

 

<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />

 

 

 

</telerik:GridTemplateColumn>

then i convert to vb this code, than you posted in some thread

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
                foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
                { 
                    CheckBox chk = (CheckBox)item.FindControl("CheckBox1"); 
                    if (chk.Checked) 
                    { 
                        Label lbl = (Label)item["Cool"].FindControl("Label1"); 
                        lbl.Text = DropDownList1.Text; 
                    } 
                }          
    } 

this is the result

 

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    For

 

Each item As Telerik.Web.UI.GridDataItem In RadGrid1.MasterTableView.Items

 

 

        Dim chk As CheckBox = CType(item.FindControl("CheckBox1"), CheckBox)

 

 

        If chk.Checked  Then

 

 

 

            'do something

 

 

        End If

 

 

    Next

 

End Sub

 

this code is running, but even with all rows checked in the command IF allways the result is false, can you help me please, what i doing wrong.

sorry for my english, i still working on it

Thanks in advanced...

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Nov 2008, 05:42 AM
Hi Jav,

Try with the following code snippet and see whether it helps.

VB:
 
 
     Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
         
         For Each item As GridDataItem In RadGrid1.MasterTableView.Items 
             Dim chkbx As CheckBox = DirectCast(item("RevCol").FindControl("CheckBox1"), CheckBox) 
             If chkbx.Checked Then 
                 
             End If 
         Next 
         
         
     End Sub 
 
 



Thanks
Shinu.
0
jav
Top achievements
Rank 1
answered on 10 Nov 2008, 03:56 PM
Hi, Shinu, Thanks for your answer, i test the code but i still have the same result, all rows with checkbox checked the result in IF command is false, do you have another idea?, in the meantime i will keep research into sth

thanks again
0
jav
Top achievements
Rank 1
answered on 10 Nov 2008, 06:59 PM
Hi again, Shinu, I fix up my problem, i follow this code than you posted in some thread, maybe isn't the best way because every time i checked a row, i have to run it, in the meantime i find a solution. thaks again

aspx

 

<telerik:GridTemplateColumn UniqueName="TempCol1" HeaderText="TempCol1">

 

 

<ItemTemplate>

 

 

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />

 

 

</ItemTemplate>

 

 

</telerik:GridTemplateColumn>

 


VB
i convert this code that you posted in some thread to vb
protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
    { 
        CheckBox chkbx = (CheckBox)sender;  
        //to access the row containg the Corresponding CheckBox 
        GridDataItem item = (GridDataItem)chkbx.NamingContainer; 
        // to get the CheckBox value 
        bool checkValue = chkbx.Checked; 
    } 
0
Zafarul
Top achievements
Rank 1
answered on 19 Nov 2008, 12:32 PM
But how can i read bound column in button click.

please suggest
0
Princy
Top achievements
Rank 2
answered on 19 Nov 2008, 01:39 PM
Hello Zafarul,

You can access the text in a GridBoundColumn on clicking a button outside the grid using the following code:
cs:
 protected void Button_Click(object sender, EventArgs e) 
    { 
        foreach (GridDataItem data in RadGrid1.Items) 
        { 
           string strtxt = data["ProductName"].Text; 
        } 
    } 

And to access the text of the gridboundcolumn on clicking a button in the same row in the grid.
aspx:
<telerik:GridTemplateColumn UniqueName="TemplateColumn2"
                <ItemTemplate> 
                    <asp:Button ID="Button" runat="server" Text="Button" OnClick="Button_Click" /> 
                 </ItemTemplate> 
</telerik:GridTemplateColumn> 

cs:
 protected void Button_Click(object sender, EventArgs e) 
    { 
        Button btn = (Button)sender; 
        GridDataItem dataItem = (GridDataItem)btn.NamingContainer; 
        string strtxt = dataItem["ProductName"].Text; 
    } 

Thanks
Princy.
Tags
Grid
Asked by
jav
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
jav
Top achievements
Rank 1
Zafarul
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or