checkboxes dynamically

G

Guest

I create 2 groupes of checkboxes dynamically (I don't use checkboxlist) for 2
differnet zones in my aspx web page and I assign a "onClick" javascript
handler by group., lets say the first 4 chboxes have evHandler1 and the other
4 chboxes have evHandler2
Of course each checkbox has an id and name which comes from DB lookup tables.
At the top of each group, there is a special checkbox, if checked by the
user, I need to write a javascript that loop through checkboxes in the group
and uncheck any
of the checked checkboxes in that group.
I can do it easily for radio buttions because they have the groupname
property, checkboxes don't have this property.
I amnot expert in javascript so I would appreciate any idea or tip to
achieve this

Thanks in advance
 
W

Walter Wang [MSFT]

Hi,

Based on my understanding, your question is how to treat several checkbox
as a group in javascript/dhtml. Please correct me if I've misunderstood
anything.

I think you can use following javascript:

#JavaScript Toolbox - CheckBox / Check Box Group
http://www.mattkruse.com/javascript/checkboxgroup/

Let me know if you need further information on how to achieve your specific
objective. Thanks.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Exactly, example 2 is what I need to do. However, as I said in a second post
, in the code behind, I can not assign the value "name" to the control. I can
do it for the id, when browsing, aparently .net assigns trhe ID value as the
name value for the control.
So according to the link you provided, I need to dosomething like this
id="1" name = "checkbox1"
id="2" name = "checkbox2"
...
....
so how this can be achieved in the code behind. last question, what is the
best way to use this library? should I write it as a big string in the code
behind or write it in a separate js file? can please give me some hints.
So many thanks
 
G

Guest

I forgot to say that I didn'tfind how to set the attribute "Value" for the
checkbox in the code behind as well as the "name" attribute

Thanks
 
G

Guest

Again so many thanks. I switched to to outloook express, downloaded the zip
file, I can extract all files except the javascript one. windows doesn't
allow. I have Windows firewall off.

One thing to mention, and sorry for that , In fact my needs is when first
item is checked, I need to uncheck all other check boxes in the group and not
check the boxes. So I think the code should do the the inverse, where should
I do that in your JS file of the C sharpe.

One last thing, how did you test your control? what I mean is I haev already
an aspx page which I said create dynamically the checkboxes and they are
bound to different code. So, correct me If i AM MISTAKEN, inyour Csharpe
code, I dont see how to add the check boxes
Is it possible to send me the js file and a test file on my email directly
Walter Wang said:
Hi,

I've created a customized CheckBoxList control for you which has the
behavior: the first item will be the control item, when it's checked, all
other items are checked; when it's unchecked, all other items are unchecked.

using System;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

[assembly: WebResource("CheckBoxListExLib.CheckBoxGroup.js",
"text/javascript")]

namespace CheckBoxListExLib
{
public class CheckBoxListEx : CheckBoxList
{
protected override void OnPreRender(EventArgs e)
{
Type type = typeof(CheckBoxListEx);
string url = Page.ClientScript.GetWebResourceUrl(type,
"CheckBoxListExLib.CheckBoxGroup.js");
if (!Page.ClientScript.IsClientScriptIncludeRegistered(type,
type.FullName))
Page.ClientScript.RegisterClientScriptInclude(type,
type.FullName, url);

if (!Page.ClientScript.IsClientScriptBlockRegistered(GetType(),
ClientID))
{
StringBuilder sb = new StringBuilder();
sb.Append(string.Format("var {0} = new CheckBoxGroup();",
GroupID));

for (int i = 0; i < Items.Count; i++)
{
string id = ClientID + "$" + i.ToString();
sb.Append(string.Format("{0}.addToGroup('{1}');",
GroupID, id));
if (i == 0)
{

sb.Append(string.Format("{0}.setControlBox('{1}');", GroupID, id));

sb.Append(string.Format("{0}.setMasterBehavior('all');", GroupID));
}
}

Page.ClientScript.RegisterClientScriptBlock(GetType(),
ClientID, sb.ToString(), true);
}

base.OnPreRender(e);
}

protected virtual string GroupID
{
get { return ClientID + "_CG"; }
}

protected override void RenderItem(ListItemType itemType, int
repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
Items[repeatIndex].Attributes.Add("onclick",
string.Format("{0}.check(this)", GroupID));
base.RenderItem(itemType, repeatIndex, repeatInfo, writer);
}
}
}


I've attached a complete class library project for your reference. The
javascript will be embedded in the assembly. You can use this control as
where you need to use CheckBoxList. (You may have to use Outlook Express to
download the attachment)
 
G

Guest

Also, maybe I was not very clear about the fact that as I said I have 2
different groups and when the master control in checked, there is a
javascript that would execute that update some different stuff in an xml
database. If I follow your logic, I should create 2 controls with each one
has its check method unchecks the other checboxes and execute the custom code
regarding updating the database., NON?
 
W

Walter Wang [MSFT]

Hi,

Sorry for my misunderstanding that you're using .NET 2.0.

I understand that you've already have code that dynamically creates the
checkboxes, so I think we will continue the work from your existing code.

I think you need to do following changes:
1) Get the javascript from the website I mentioned at my first reply and
save it as a .js file and add it to your web app
2) In your web form, include the javascript
3) Since your desired behavior is to clear other checkboxes when the
controlling checkbox is checked, you need to change following javascript
code:

function CBG_check(obj) {
....

if (this.masterBehavior=="all") {
for (i=0;i<this.checkboxNames.length;i++) {
this.checkboxNames.checked=checked; }


just change to "....checked=!checked"

4) Construct a javascript code block dynamically in your code-behind to use
the javascript library. Just remember that the client-side name attribute
is from server-side UniqueID property. Then register this javascript code
block to your Page class.

Please let me know if you need further information. If you need complete
code example, please send me your webform first so that I can directly make
the modifications based on your code, that will make more sense. Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top