[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Web Custom Control - Checkbox list (Very urgent)

Posted on 2005-04-19
6
Medium Priority
?
249 Views
Last Modified: 2010-04-16
Hi,

I'm creating a custom checkbox list control which will take 2 arraylists as input. (One will contain Names and other will contain 0s and 1s. 0 - uncheck 1- check).
I'm able to create the checkbox list. The problem is I'm unable to raise an event when I select any of the checkboxes. Following is the code:

Could you guys please help me on this issue.

Custom Control Code:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;

namespace CheckBoxControl
{
      /// <summary>
      /// Summary description for WebCustomControl1.
      /// </summary>
      public class WebCheckBoxControl : WebControl, IPostBackDataHandler
      {
            ArrayList GBNameList = new ArrayList();
            ArrayList GBCodeFlagList = new ArrayList();
              public event EventHandler Changed;

            public String Text
            {
                  get
                  {
                        return (String) ViewState["Text"];
                  }

                  set
                  {
                        ViewState["Text"] = value;
                  }
            }    
 
            public virtual bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
            {
                  String presentValue = Text;
                  String postedValue = postCollection[postDataKey];

                  if (presentValue == null || !presentValue.Equals(postedValue))
                  {
                        Text = postedValue;
                        return true;
                  }
                  return false;
            }


            public virtual void RaisePostDataChangedEvent()
            {
                  OnChanged(EventArgs.Empty);
            }


            protected virtual void OnChanged(EventArgs e)
            {    
                  if (Changed != null)
                  {
                        Changed(this, e);
                  }  
            }


            public void PerformHandShake(ArrayList NameList, ArrayList FlagList)
            {
                  if (NameList.Count == CodeFlagList.Count)
                  {
                        this.GBNameList = NameList;
                        this.GBCodeFlagList = CodeFlagList;
                  }
                  else // Show error
                  {
                        
                  }
            }

            protected override void Render(HtmlTextWriter output)
            {
                  try
                  {
                        int CheckBoxValue = 1;
                        string OutPut = "";
                                    

                        string st1 = "<Table>";

                        if (this.GBNameList.Count > 0)
                        {
                                        
                              for(int name = 0; name<this.GBNameList.Count; name++)
                              {
                              string Name = this.GBNameList[name].ToString();
                              string CodeFlag = this.GBCodeFlagList[name].ToString();
                                                            
                              
                                    if (CodeFlag == "0") // Checked = False
                                    {
                                          OutPut = OutPut + this.GetOneString(Name, CheckBoxValue, "0");
                                                                              }
                                    if (CodeFlag == "1") // Checked = True
                                    {
                                          OutPut = OutPut + this.GetOneString(Name, CheckBoxValue, "1");
                                    }

                                    CheckBoxValue = CheckBoxValue + 1 ;
                              }      
                  
                              string st2 = "</Table>";
                              string CheckBoxeswithTable = st1 + OutPut + st2;
                              output.Write(CheckBoxeswithTable);                                    
                              output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
                        }            
                        else
                        {
                              output.Write("CheckBox List Control");
                        }
                  }

                  catch(Exception ex)
                  {
                        output.Write("Errror building CheckBox List:<br>");
                        output.Write(ex.InnerException);
                  }
            }


            public string GetOneString(string Name, int CheckBoxValue, string CodeFlag)
            {
                  string ToGo = "";
                  try
                  {
                        // Construct CheckBox in the First Column
                        string st1 = "<TR>";
                        string st2 = "<TD>";
                        string st3 = "<input type=\"checkbox\" name=\"";
                        string st4 = Name;
                        string st5 = "\" value=\"";
                        string st6 = CheckBoxValue.ToString();
                        string st7 = "\"";
                        string st8 = "CHECKED";
                        string test = "";
                                                                string st9 = ">";
                        string st10 = "</TD>";
                        
                        // Construct Name of CheckBox in the Adjacent Cell
                        string st11 = "<TD>";
                        string st13 = "<font size=\"2\" face=\"Arial\">" ;
                        string st14 = Name;
                        string st15 = "</font>";
                        string st16 = "</TD>";
                        string st17= "</TR>";

                        if (CodeFlag == "0")// Checked = False
                        {
                              ToGo = st1 + st2 + st3 + st4 + st5 + st6 + st7 + test + st9 + st10 + st11 + st13 + st14 + st15 + st16 + st17 ;
                        }
                        if (CodeFlag == "1") // Checked = True
                        {
                              ToGo = st1 +  st2 + st3 + st4 + st5 + st6 + st7 + st8 + test + st9 + st10 + st11 + st13 + st14 + st15 + st16 + st17 ;
                        }
                  }
                  catch(Exception ex) // Show Error
                  {
                        
                  }

                  return ToGo;
            }


      } // Class
} // NameSpace




USAGE ON THE PAGE:

HTML Section:

<%@ Register TagPrefix="cc1" Namespace="BranchCheckBoxControl" Assembly="BranchCheckBoxControl" %>
<cc1:webbranchcheckboxcontrol id="WebBranchCheckBoxControl1" runat="server"></cc1:webbranchcheckboxcontrol>

Code-Behind Section:
            public void Changed(object sender, System.EventArgs e)
            {
                  TextBox1.BackColor = System.Drawing.Color.Green;
                  TextBox1.Text = "You clicked the button";
            }

Thanks
Praveen
0
Comment
Question by:praveenuni
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13821047
Did you add function Changed to the Changed property of this custom control?
0
 

Author Comment

by:praveenuni
ID: 13821300
yes, I did this :

<cc1:webbranchcheckboxcontrol id="WebBranchCheckBoxControl1" OnChanged = "Changed" runat="server"></cc1:webbranchcheckboxcontrol>
Still I'm unable to raise the event

Thanks
Praveen
0
 
LVL 12

Accepted Solution

by:
laotzi2000 earned 2000 total points
ID: 13823516
But your event name is not OnChanged, it's Changed.
Shouldn't it be
<cc1:webbranchcheckboxcontrol id="WebBranchCheckBoxControl1" Changed = "Changed" runat="server"></cc1:webbranchcheckboxcontrol>
?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:praveenuni
ID: 13825555
Look at this:
            public virtual void RaisePostDataChangedEvent()
            {
                  OnChanged(EventArgs.Empty);
            }


            protected virtual void OnChanged(EventArgs e)
            {    
                  if (Changed != null)
                  {
                        Changed(this, e);
                  }  
            }

So my event name is OnChanged right. Anyways I changed it to ..
<cc1:webbranchcheckboxcontrol id="WebBranchCheckBoxControl1" Changed = "Changed" runat="server"></cc1:webbranchcheckboxcontrol>. Still I'm unable to raise the event.

Thanks
Praveen
0
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13825618
No, OnChanged is not the event name, it's not even public.
public event EventHandler Changed;
This is the event.

I think the problem is with the hookup.
I know that in code you can do:
WebBranchCheckBoxControl1.Changed += new EventHandler(Changed);

I'm not sure how it's done in html.
I'll do some research on that.
0
 

Author Comment

by:praveenuni
ID: 13825680
I did this on Initialize component :

            private void InitializeComponent()
            {    
                  this.Load += new System.EventHandler(this.Page_Load);
                  this.WebBranchCheckBoxControl1.Changed +=new EventHandler(Changed);
            }

still of no use.  I think you are right. The problem should be in the hook up.

Thanks
Praveen
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question