Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 22nd, 2007, 05:29 AM
Authorized User
 
Join Date: Jun 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vishwjeet
Default Custom Radio Button

Hi all, I got the following code on a forum. But I dont know how to use it in my code. Help!!!

public class MyRadioButtonList : RadioButtonList
    {

        private string HiddenValueFieldName
        {
            get
            {
                return this.ClientID + "_theValue";
            }
        }

        private string DivArrayName
        {
            get
            {
                return this.ClientID + "_theDivs";
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            string script = "";
            script += "function setValue(theDiv){";
            script += "document.getElementById('" + HiddenValueFieldName + "').value = theDiv.getAttribute('value');";

            if(this.AutoPostBack)
                script += Page.ClientScript.GetPostBackEventReference(this, "", false);
            else{
                script += "theDiv.style.backgroundColor='gray';";
                script += "for(i=0;i<" + DivArrayName + ".length;i++){";
                script += "var theVal=" + DivArrayName + "[i];";
                script += "if(theDiv.getAttribute('id') != theVal)";
                script += "document.getElementById(theVal).style.backgroundC olor='white';";
                script += "}";
            }

            script += "}";
            Page.ClientScript.RegisterStartupScript(this.GetTy pe(),"setValue",script,true);

            //register the hidden field
            Page.ClientScript.RegisterHiddenField(HiddenValueF ieldName, "");

            //register that LoadPostdata will be called even if control's UniqueID is not on post data collection
            Page.RegisterRequiresPostBack(this);

            //this is playing for the client-side color selection :-)
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach(ListItem litem in this.Items)
            {
                sb.Append("'");
                sb.Append(this.ClientID);
                sb.Append(this.IdSeparator);
                sb.Append(litem.Value);
                sb.Append("'");
                sb.Append(",");
            }

            string s = sb.ToString().TrimEnd(',');
            Page.ClientScript.RegisterArrayDeclaration(this.Cl ientID + "_theDivs", s);

        }

        protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            //wrap the existing loading logic to get the LiostItem value from our hidden field
            return base.LoadPostData(HiddenValueFieldName, postCollection);
        }

        protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
        {
            //render the thing out as a DIV
            ListItem item=this.Items[repeatIndex];
            writer.AddStyleAttribute(HtmlTextWriterStyle.Borde rWidth, "1px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Borde rColor, "black");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Borde rStyle, "solid");

            writer.AddAttribute(HtmlTextWriterAttribute.Value, item.Value);
            writer.AddAttribute(HtmlTextWriterAttribute.Onclic k, "setValue(this)");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID + this.IdSeparator + item.Value);

            if (item.Selected)
                writer.AddStyleAttribute(HtmlTextWriterStyle.Backg roundColor, "gray");

            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.Write(item.Text);
            writer.RenderEndTag();

        }
    }

- Vishu
__________________
- Vishu
 
Old June 26th, 2007, 01:46 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Well, I don't "think" it's an issue, but this is in C# which I'm less familiar with.

In principle though, what you have is the code for a custom class description. Just make the code available to your application (the part I'm a little fuzzy on), and in your VB code behind...

Code:
Dim Whatever As New MyRadioButtonList
That will create an instance of the list that you can program against. Alternatively, if you're working in C# yourself, you may be able to include the class description code directly in your code behind file. But I don't know the syntax for C#.


Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Radio button gaurisharya C# 2005 2 March 16th, 2008 06:43 AM
radio-button grstad HTML Code Clinic 3 February 28th, 2006 08:32 AM
Radio Button hoailing22 ASP.NET 1.0 and 1.1 Basics 1 June 2nd, 2005 12:08 AM
Button acts depending on radio button values janise Access 4 March 10th, 2004 12:53 AM
radio button damnnono_86 Access 5 October 28th, 2003 02:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.