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

How to disable a checkbox in radgrid using javascript

5 Answers 519 Views
Input
This is a migrated thread and some comments may be shown as answers.
BRK
Top achievements
Rank 1
BRK asked on 25 May 2011, 07:06 AM
Hi,

I have a Radgrid which contains one text box and one checkbox.
depending upon the value in textbox i want to disable the checkbox in the same row in RadGrid.
Please tell me how can I do it.

I tried but i cant access the correct property to disable the checkbox.
Please let me know the solutions

Bye
Thanks

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 May 2011, 08:40 AM
Hello Brk,
You can achieve this by calling the javascript function from itemDataBound event. Here is a sample code.
C#:
protected void grdEmail_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item=(GridDataItem)e.Item;
           TextBox txt = (TextBox)item.FindControl("TxtBox");
           if (txt.Text == "1")//Checking for the textBox value
           {
               string executeScript = "<script language='javascript'>function f(){setCheckBox('" + e.Item.ItemIndex + "');             Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
               Page.ClientScript.RegisterStartupScript(this.GetType(), "setCheckBox", executeScript );
           }
     }

Javascript:
function setCheckBox(index)
    {
       var grid = $find('<%=RadGrid1.ClientID %>');
       var masterTable = grid.get_masterTableView();
       var row = masterTable.get_dataItems()[index];
       var chkBox = row.findElement("CheckBox1");
       chkBox.disabled = true;
   }

corresponding aspx is:
<telerik:GridTemplateColumn >
  <ItemTemplate>
   <asp:CheckBox ID="CheckBox1" runat="server" Checked="true" />
   <asp:TextBox ID="TxtBox" runat="server" Text='<%#Eval("EmployeeID")%>'></asp:TextBox>
  </ItemTemplate>
</telerik:GridTemplateColumn>

Thanks,
Shinu.
0
BRK
Top achievements
Rank 1
answered on 25 May 2011, 11:16 AM
Hi Shinu,

Its not working it is not even going into that javascript function.
Please tell me where I went wrong
0
Shinu
Top achievements
Rank 2
answered on 26 May 2011, 06:06 AM
Hello Brk,

The code worked as expected at my end. I suppose you may not checking for the TextBox value correctly. Please make sure that you are checking for the value correctly and getting into the loop.
C#:
if (give the condition here)//Checking for the textBox value
           {
               string executeScript = "<script language='javascript'>function f(){setCheckBox('" + e.Item.ItemIndex + "');             Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
               Page.ClientScript.RegisterStartupScript(this.GetType(), "setCheckBox", executeScript );
           }
Hope it helps.

Please elaborate your scenario if it doesn't help.

Thanks,
Shinu.
0
BRK
Top achievements
Rank 1
answered on 26 May 2011, 06:28 AM
Hi Shinu,

Yes I am not taking any condition here.

My scenario is In a grid there will two check boxes if one check-box is checked the another must be disabled or Invisible,
When I am implementing the code you sent it is telling that there is conversion problem cant convert string to double.
may be due to the index we gave so there I kept .ToString() to resolve.
After that too the it is not even going to the Javascript function it is telling that setCheckBox is undefined.
These are the problems I faced.

Please let me know if you have any solutions.
0
Shinu
Top achievements
Rank 2
answered on 27 May 2011, 08:39 AM
Hello,

Try the following code to achieve your scenario.

aspx:
<telerik:GridTemplateColumn UniqueName="ToggleButton" DataField="bool">
   <ItemTemplate>
            <asp:CheckBox ID="ChkBx1" runat="server" />
            <asp:CheckBox ID="CheckBox1" runat="server" />
   </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void grdEmail_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           CheckBox chk = (CheckBox)item.FindControl("ChkBx1");
           chk.Attributes.Add("onclick","disable('"+e.Item.ItemIndex+"')");//ataching the click event to the checkBox and passing the index
       }
   }

JavaScript:
function disable(index)
 {
       var masterTable = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
       var row = masterTable.get_dataItems()[index];
       var chk1 = row.findElement("ChkBx1");
       var chk2 = row.findElement("CheckBox1");
       if (chk1.checked)
       {
           chk2.disabled = true;
       }
       else
       {
           chk2.disabled = false;
       }
   }

Thanks,
Shinu.
Tags
Input
Asked by
BRK
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
BRK
Top achievements
Rank 1
Share this question
or