I have a radgrid with a checkbox grid template column to select the default address. I would like to have user check only one checkbox . If user checks one, then all others should be unchecked. If none are checked then give them error saying atleast one needs to be checked. They can change the checkbox selection until they submit the form. How can this be done?
12 Answers, 1 is accepted
0

Tan
Top achievements
Rank 1
answered on 17 Apr 2012, 03:37 AM
To Janaki
U can use RadioButton, Please refer http://www.telerik.com/community/code-library/aspnet-ajax/grid/single-radiobutton-check-at-a-time-with-row-selection.aspx

From louisth
U can use RadioButton, Please refer http://www.telerik.com/community/code-library/aspnet-ajax/grid/single-radiobutton-check-at-a-time-with-row-selection.aspx
From louisth
0

Janaki
Top achievements
Rank 1
answered on 17 Apr 2012, 04:25 AM
My users would like the checkbox functionality. Can we do the same for a checkbox?
0

Shinu
Top achievements
Rank 2
answered on 17 Apr 2012, 06:01 AM
Hi Janaki,
Here is the code I tried to check only one CheckBox at a time and alert if none is checked on a Button click.
ASPX:
Javascript:
Thanks,
Shinu.
Here is the code I tried to check only one CheckBox at a time and alert if none is checked on a Button click.
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridTemplateColumn
HeaderText
=
"Checking"
UniqueName
=
"check"
>
<
ItemTemplate
>
<
asp:CheckBox
ID
=
"CheckBox1"
runat
=
"server"
onclick
=
"checkboxClicked(event, 'CheckBox1')"
/>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
<
asp:Button
ID
=
"button1"
runat
=
"server"
OnClientClick
=
"OnClientClick()"
Text
=
"submit"
/>
Javascript:
<script type=
"text/javascript"
>
function
checkboxClicked(e, idFragment)
{
var
currentCheckBox = e.srcElement || e.target;
var
inputs = document.getElementsByTagName(
"input"
);
for
(
var
i = 0; i < inputs.length; i++) {
var
input = inputs[i];
if
(input.id == currentCheckBox.id)
continue
;
if
(input.id.indexOf(idFragment) < 0)
continue
;
//clear out the rest of the checkboxes
if
(input.type && input.type ==
"checkbox"
) {
input.checked =
false
;
}
}
}
function
OnClientClick()
{
var
checking =
false
;
debugger;
var
inputs = document.getElementsByTagName(
"input"
);
for
(
var
i = 0; i < inputs.length; i++) {
if
(inputs[i].checked ==
true
) {
checking =
true
;
break
;
}
}
if
(!checking) {
alert(
"Atleast one need to be selected"
);
}
}
</script>
Thanks,
Shinu.
0

Janaki
Top achievements
Rank 1
answered on 17 Apr 2012, 01:57 PM
Hi Shinu,
Thanks for your code. I will try it out. Is there a way this can be done on the server side code?
Thanks, Janaki
Thanks for your code. I will try it out. Is there a way this can be done on the server side code?
Thanks, Janaki
0

Janaki
Top achievements
Rank 1
answered on 17 Apr 2012, 04:54 PM
Is there a way to handle this in the checked changed event?
0

Janaki
Top achievements
Rank 1
answered on 17 Apr 2012, 05:02 PM
Is there a way to handle this in the checked changed event?
0

Shinu
Top achievements
Rank 2
answered on 18 Apr 2012, 07:13 AM
Hello Janaki,
Please try the following approach.
C#:
Thanks,
Shinu.
Please try the following approach.
C#:
protected
void
CheckBox1_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridDataItem itm = (GridDataItem)chk.NamingContainer;
int
crrntindex = itm.ItemIndex;
if
(chk.Checked ==
true
)
{
foreach
(GridDataItem row
in
grid1.MasterTableView.Items)
{
CheckBox chk1 = (CheckBox)row.FindControl(
"CheckBox1"
);
int
index = row.ItemIndex;
if
(crrntindex == index)
continue
;
if
(chk1.Checked ==
true
)
{
chk1.Checked =
false
;
}
}
}
}
protected
void
Submit_Click(
object
sender, EventArgs e)
{
bool
check =
false
;
foreach
(GridDataItem item
in
grid1.MasterTableView.Items)
{
CheckBox chk = (CheckBox)item.FindControl(
"CheckBox1"
);
if
(chk.Checked ==
true
)
{
check =
true
;
break
;
}
if
(!check)
{
Response.Write(
"<script>alert('Please select atleast one item');</script>"
);
}
}
}
Thanks,
Shinu.
0

Janaki
Top achievements
Rank 1
answered on 20 Apr 2012, 04:01 AM
This worked except for one problem. The checkbox does not keep it checked state after the form is submitted with the save button. Can you tell me how I can keep the checkbox checked status after the save.
Thanks, Janaki
Thanks, Janaki
0

Shinu
Top achievements
Rank 2
answered on 20 Apr 2012, 06:08 AM
Hi Janaki,
Please try the following code snippet to persist the checkbox check after submitting.
ASPX:
C#:
Here is the documentation that I referred.
Thanks,
Shinu.
Please try the following code snippet to persist the checkbox check after submitting.
ASPX:
<
MasterTableView
DataKeyNames
=
"OrderID"
Name
=
"Master"
></
MasterTableView
>
C#:
protected
void
CheckBox1_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridDataItem itm = (GridDataItem)chk.NamingContainer;
int
crrntindex = itm.ItemIndex;
if
(chk.Checked ==
true
)
{
foreach
(GridDataItem row
in
grid1.MasterTableView.Items)
{
CheckBox chk1 = (CheckBox)row.FindControl(
"CheckBox1"
);
int
index = row.ItemIndex;
if
(crrntindex == index)
continue
;
if
(chk1.Checked ==
true
)
{
chk1.Checked =
false
;
}
}
}
Hashtable target =
new
Hashtable();
if
(itm.OwnerTableView.Name ==
"Master"
)
{
target = CustomersChecked;
}
if
(chk.Checked)
{
target[itm[
"OrderID"
].Text] =
true
;
}
else
{
target[itm[
"OrderID"
].Text] =
null
;
}
}
protected
void
Submit_Click(
object
sender, EventArgs e)
{
bool
check =
false
;
foreach
(GridDataItem item
in
grid1.MasterTableView.Items)
{
CheckBox chk = (CheckBox)item.FindControl(
"CheckBox1"
);
if
(chk.Checked ==
true
)
{
check =
true
;
break
;
}
if
(!check)
{
Response.Write(
"<script>alert('Please select atleast one item');</script>"
);
}
}
}
protected
void
rdgdr1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = e.Item
as
GridDataItem;
CheckBox box = (CheckBox)item.FindControl(
"CheckBox1"
);
object
isChecked =
null
;
if
(item.OwnerTableView.Name==
"Master"
)
{
isChecked = CustomersChecked[item[
"OrderID"
].Text];
}
if
(isChecked !=
null
)
{
box.Checked = (
bool
)isChecked ==
true
;
}
}
}
Here is the documentation that I referred.
Thanks,
Shinu.
0

Janaki
Top achievements
Rank 1
answered on 20 Apr 2012, 10:17 PM
I tried this and it still does not keep the checked state. Not sure what is wrong.
0

Shinu
Top achievements
Rank 2
answered on 21 Apr 2012, 05:25 AM
Hi Janaki,
Unfortunately I could not replicate the issue you are facing. Could you please provide your complete code for further assistance.
Thanks,
Shinu.
Unfortunately I could not replicate the issue you are facing. Could you please provide your complete code for further assistance.
Thanks,
Shinu.
0

Wbc
Top achievements
Rank 1
answered on 29 Jan 2013, 06:22 PM
Thank You Very Much I works fine.