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.W ebControls;
using System.Componen tModel;
using System.Collecti ons;
namespace CheckBoxControl
{
/// <summary>
/// Summary description for WebCustomContro l1.
/// </summary>
public class WebCheckBoxCont rol : WebControl, IPostBackDataHa ndler
{
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(st ring postDataKey,
System.Collecti ons.Specialized .NameValueColle ction postCollection)
{
String presentValue = Text;
String postedValue = postCollection[postDataKey];
if (presentValue == null ||
!presentValue.E quals(postedVal ue))
{
Text = postedValue;
return true;
}
return false;
}
public virtual void RaisePostDataCh angedEvent()
{
OnChanged(Event Args.Empty);
}
protected virtual void OnChanged(Event Args e)
{
if (Changed != null)
{
Changed(this, e);
}
}
public void PerformHandShak e(ArrayList NameList, ArrayList
FlagList)
{
if (NameList.Count == CodeFlagList.Co unt)
{
this.GBNameList = NameList;
this.GBCodeFlag List = CodeFlagList;
}
else // Show error
{
}
}
protected override void Render(HtmlText Writer output)
{
try
{
int CheckBoxValue = 1;
string OutPut = "";
string st1 = "<Table>";
if (this.GBNameLis t.Count > 0)
{
for(int name = 0; name<this.GBNam eList.Count;
name++)
{
string Name = this.GBNameList[name].ToString();
string CodeFlag =
this.GBCodeFlag List[name].ToString();
if (CodeFlag == "0") // Checked = False
{
OutPut = OutPut + this.GetOneStri ng(Name,
CheckBoxValue, "0");
}
if (CodeFlag == "1") // Checked = True
{
OutPut = OutPut + this.GetOneStri ng(Name,
CheckBoxValue, "1");
}
CheckBoxValue = CheckBoxValue + 1 ;
}
string st2 = "</Table>";
string CheckBoxeswithT able = st1 + OutPut + st2;
output.Write(Ch eckBoxeswithTab le);
output.AddAttri bute(HtmlTextWr iterAttribute.N ame,
this.UniqueID);
}
else
{
output.Write("C heckBox List Control");
}
}
catch(Exception ex)
{
output.Write("E rrror building CheckBox List:<br>");
output.Write(ex .InnerException );
}
}
public string GetOneString(st ring 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.T oString();
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="Bran chCheckBoxContr ol"
Assembly="Branc hCheckBoxContro l" %>
<cc1:webbranchc heckboxcontrol id="WebBranchCh eckBoxControl1" Changed =
"Changed" runat="server"> </cc1:webbranchch eckboxcontrol>
Code-Behind Section:
public void Changed(object sender, System.EventArg s e)
{
TextBox1.BackCo lor = System.Drawing. Color.Green;
TextBox1.Text = "You clicked the button";
}
Thanks
Steven