User Control settings not propagated to Page

Z

Zark3

Hi all,

I've got a small difference of opinion with a (self-designed) user
control. The control should display a (variable) number of checkboxes
(the different options available to the page) that the user can
check/uncheck to tell the page to use this data or not. With me so far?

Example: page is to show a text in a variety of versions; there are
checkboxes for every different version of this text and unchecking an
option tells the page not to display this version.

My problem is that even though the page shows a checkbox unchecked
after the user clicks (and such checkboxes are kept unchecked also
after other checkboxes are unchecked) the text versions are still
--all-- displayed. A debug label confirms that it finds --every-- box
checked. My personal guess is that the event handler does not fire
correctly and/or that i'm trying to work in the wrong phase of the page
life cycle. Unfortunately, my attempts have been to no avail. Can any
of you help me out here? Please?

[Code somewhat abbreviated/snipped for brevity]

--- Default.aspx
<%@ Register TagPrefix="Zark3" TagName="FilterBox"
Src="~/FilterBox.ascx" %>
<Zark3:FilterBox runat="server" ID="LanguageFilter" />

--- Default.aspx.cs
protected void Page_Load(object sender, EventArgs e) {
LanguageFilter.OptionsFile =
Server.MapPath("~/App_Data/Options.xml");
foreach (Option o in LanguageFilter.CheckedOptions)
DebugLabel.Text += o.ID; // --> Outputs 1234 without change
}

--- FilterBox.ascx
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="FilterBox.ascx.cs" Inherits="Zark3.SideBySide.FilterBox" %>
<asp:Table runat="server" ID="OptionsTable" CssClass="Filter">
<asp:TableRow runat="server"><asp:TableCell runat="server"
ColumnSpan="2"><b>Language filter:</b></asp:TableCell></asp:TableRow>
</asp:Table>

--- FilterBox.ascx.cs
public partial class FilterBox : System.Web.UI.UserControl {
private Options _options;
private string _filename;
private bool[] _checked;

public string OptionsFile {
get { return _filename; }
set {
_filename = value;
_options = new Options(_filename); // -->
Deserializes a list from an XML file
_checked = new bool[_options.Option.Length]; // -->
Keep track of checked options
for (int i = 0; i < _options.Option.Length; i++) {
CheckBox c = new CheckBox();
c.ID = _options.Option.ID.ToString();
c.AutoPostBack = true;
c.Checked = true;
c.CheckedChanged += new
EventHandler(c_CheckedChanged);
Label l = new Label();
l.Text = _options.Option.Name;
TableCell chk = new TableCell();
chk.Controls.Add(c);
TableCell val = new TableCell();
val.Controls.Add(l);
TableRow tr = new TableRow();
tr.Cells.Add(chk);
tr.Cells.Add(val);
OptionsTable.Rows.Add(tr);
_checked = true;
}
}
}

public Option[] CheckedOptions {
get {
List<Option> list = new List<Option>();
for (int i = 0; i < _options.Option.Length; i++)
if (_checked)
list.Add(_options.Option);
return list.ToArray();
}
}

void c_CheckedChanged(object sender, EventArgs e) {
Toggle(getIndex(int.Parse(((CheckBox)sender).ID)));
}
private int getIndex(int ID) {
for (int i = 0; i < _options.Option.Length; i++)
if (_options.Option.ID == ID)
return i;
return -1;
}
private void Toggle(int index) {
if (_checked[index]) {
_checked[index] = false;
((CheckBox)OptionsTable.Rows[index +
1].Cells[0].Controls[0]).Checked = false;
}
else {
_checked[index] = true;
((CheckBox)OptionsTable.Rows[index +
1].Cells[0].Controls[0]).Checked = true;
}
}
}
 
Z

Zark3

Added info: I've tried changing in Default.aspx.cs the line

LanguageFilter.OptionsFile = Server.MapPath("~/App_Data/Options.xml");

to

if (!Page.IsPostBack)
LanguageFilter.OptionsFile =
Server.MapPath("~/App_Data/Options.xml");

but this only results in a NullReferenceException in
Zark3.SideBySide.FilterBox.get_CheckedOptions(), stating that the
_options Object reference is not set to an instance of an object.

Thanks in advance for any pointers!

Chris
 

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