gridview with checkbox option

Posted by Prince5 under ASP.NET on 2/19/2009 | Views : 7032 | Status : [Member] | Replies : 3
hi
i have gridview with checkbox option. think there 10 rows with 5 columns
now user selects a check box. but now i should make just a option user can check only one checkbox at a time . not more than one.
and i have button. called delete button. so that once the user press this button he need to get the message telling select atleast on checkbox before deleting.

i want the how to make user selects only one checkbox at a time before delete
thank you




Responses

Posted by: Neeks on: 2/19/2009 [Member] Bronze

Up
0
Down
Required things on your page.
1) GridView [There must be a Checkbox template field in your Gridview.]
2) Button--%>
You have to write this script in your <head></head> section of your code.

<script type="text/javascript" language="javascript">

//You have to call this script in OnClientClick event of a button
function fnTest()
{
var frm = document.forms[0];
alert(frm);
var test = 0;
for (i=0; i<frm.length; i++)
{
if (frm.elements[i].id.indexOf('checkBox') != -1)
{
//To check whether the checkbox is checked or not.
if(frm.elements[i].checked == true)
{
//If checkbox is selectd then the value test variable is increnented by one.
test ++;
if(test > 1)
break; //We are breaking the extra Loop if more than one checkbox is checked.
}
}
}
var retVal = false;
//If more than one checkbox is selected
if(test > 1)
{
alert('You cannot select more than one check box');
retVal = false;
}
//If none of the checkbox is selected
else if( test == 0)
{
alert('You have to select atleast one checkbox');
retVal = false;
}
//If one checkbox is selected
else
{
retVal = confirm('Are you sure you want to delete');
}
return retVal;
}
</script>


Calling this function from the Button Event
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:return fnTest();" />



Prince5, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Majith on: 2/19/2009 [Member] Starter

Up
0
Down
Greetings,

you can try this in simple way using AJAX MutuallyExclusiveCheckBoxExtender .

<asp:GridView ID="GrdViewAJAX" runat="server" AutoGenerateColumns="False" Width="290px"

AllowPaging="true" PageSize="5">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
<Ajax:MutuallyExclusiveCheckBoxExtender ID="MutuallyExclusiveCheckBoxExtender1" runat="server"
TargetControlID="chk1" Key="chk1">
</Ajax:MutuallyExclusiveCheckBoxExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns> </asp:GridView>

No need to write any javascript .

Prince5, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 2/20/2009 [Member] Bronze

Up
0
Down
If we are using AJAX, will it satisfy the second Condition that at least one checkbox has to be checked before deleting?

Prince5, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response