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" Changed =
"Changed" 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
Steven