Problem with DataBinding and CheckBoxList

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Re: Problem with DataBinding and CheckBoxList

    What does your Page_Load look like?
    --
    John Saunders
    Internet Engineer
    [email]john.saunders@surfcontrol.com[/email]

    "Eric Johannsen" <funmx@hotmail.com> wrote in message
    news:uPlZa.2$%p6.739429@newssvr14.news.prodigy.com ...
    > Hi all,
    >
    > I have a business object with some boolean fields. They need to be
    > represented as a CheckBoxList on an aspx page. I do the data binding just
    > like in the examples and everything displays just fine. However, when I
    go
    > to read out the new values from chklAvoid (my CheckBoxList), accessing
    > chklAvoid[0] produces an error.
    >
    > When I do a Quick Watch on that variable, I note that the embedded
    > ListControl's items.listItems property has a count of 0. Normally this
    > would contain my bound items as far as I understand.
    >
    > HELP!
    >
    > Thanks,
    >
    > Eric
    >
    > Code follows:
    >
    > test.aspx:
    > =========
    > <asp:CheckBoxList id="chklAvoid" runat="server" DataSource="<%#
    > this.createDataSourceAvoid() %>" DataValueField="BoolValue"
    > DataTextField="Name">
    > </asp:CheckBoxList>
    >
    > test.aspx.cs:
    > ============
    > private DataRow getCheckboxRow(DataTable dt, bool isChecked, string name)
    > {
    > DataRow dr = dt.NewRow();
    > dr[0] = isChecked;
    > dr[1] = name;
    > return dr;
    > }
    >
    > protected ICollection createDataSourceAvoid()
    > {
    > DataTable dt = new DataTable();
    > dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
    > dt.Columns.Add(new DataColumn("Name", typeof(string)));
    > dt.Rows.Add(getCheckboxRow(dt, myObject.Boolean1, "Text for boolean
    > 1"));
    > dt.Rows.Add(getCheckboxRow(dt, myObject.Boolean2, "Text for boolean
    > 2"));
    > DataView dv = new DataView(dt);
    > return dv;
    > }
    >
    > private void btnUpdate_Click(object sender, System.EventArgs e)
    > {
    > mCustomer.AvoidBreads = chklAvoid.Items[0].Selected;
    > mCustomer.AvoidDairy = chklAvoid.Items[1].Selected;
    > }
    >
    >

    John Saunders Guest

  2. Similar Questions and Discussions

    1. compositedataboundcontrol with template databinding problem
      Hi everyone, I am new to this ASP.NET 2.0 feature. I am having a hard time getting the databinding syntax evaluated in my template for a control...
    2. Controling the 'selected' property on CheckBoxList while DataBinding()
      Hi Group! When binding the CheckBoxList control, you can only control the 'text' and 'value' properties of each CheckBox created. Suppose I...
    3. Problem handling DataGrid TemplateColumn Databinding Event
      I have spent several hours trying to research and/or fix the following problem: Environment: VB.Net 2003 Scenario: I have a dynamically...
    4. DataGrid Custom Column Error when DataBinding "does not contain a definition for 'DataBinding'"
      I am creating a custom column that inherits from DataColumnGrid. When I attempt Databind to a property of the custom column, I get the the error: ...
    5. Control derived from datagrid, problem with adding other control and databinding (VB)
      I all, my employer required me to add a bunch of control to a datagrid (such as a drop down list on top corner to select page size and prev, bunch...
  3. #2

    Default Re: Problem with DataBinding and CheckBoxList

    Don't have the code in front of me right now, but it's very much like
    this:

    Page_Load(... standard parms ...)
    {
    if (!IsPostBack) DataBind();
    }

    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Eric Johannsen Guest

  4. #3

    Default Re: Problem with DataBinding and CheckBoxList


    Is ViewState enabled on the form and on the control? Without you'd have to
    rebind each time and if you do you loose the state (selection) of the
    control.

    +++ Rick ---

    --

    Rick Strahl
    West Wind Technologies
    [url]http://www.west-wind.com/[/url]
    [url]http://www.west-wind.com/wwHelp[/url]
    ----------------------------------
    Making waves on the Web



    "Eric Johannsen" <funmx@hotmail.com> wrote in message
    news:uPlZa.2$%p6.739429@newssvr14.news.prodigy.com ...
    > Hi all,
    >
    > I have a business object with some boolean fields. They need to be
    > represented as a CheckBoxList on an aspx page. I do the data binding just
    > like in the examples and everything displays just fine. However, when I
    go
    > to read out the new values from chklAvoid (my CheckBoxList), accessing
    > chklAvoid[0] produces an error.
    >
    > When I do a Quick Watch on that variable, I note that the embedded
    > ListControl's items.listItems property has a count of 0. Normally this
    > would contain my bound items as far as I understand.
    >
    > HELP!
    >
    > Thanks,
    >
    > Eric
    >
    > Code follows:
    >
    > test.aspx:
    > =========
    > <asp:CheckBoxList id="chklAvoid" runat="server" DataSource="<%#
    > this.createDataSourceAvoid() %>" DataValueField="BoolValue"
    > DataTextField="Name">
    > </asp:CheckBoxList>
    >
    > test.aspx.cs:
    > ============
    > private DataRow getCheckboxRow(DataTable dt, bool isChecked, string name)
    > {
    > DataRow dr = dt.NewRow();
    > dr[0] = isChecked;
    > dr[1] = name;
    > return dr;
    > }
    >
    > protected ICollection createDataSourceAvoid()
    > {
    > DataTable dt = new DataTable();
    > dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
    > dt.Columns.Add(new DataColumn("Name", typeof(string)));
    > dt.Rows.Add(getCheckboxRow(dt, myObject.Boolean1, "Text for boolean
    > 1"));
    > dt.Rows.Add(getCheckboxRow(dt, myObject.Boolean2, "Text for boolean
    > 2"));
    > DataView dv = new DataView(dt);
    > return dv;
    > }
    >
    > private void btnUpdate_Click(object sender, System.EventArgs e)
    > {
    > mCustomer.AvoidBreads = chklAvoid.Items[0].Selected;
    > mCustomer.AvoidDairy = chklAvoid.Items[1].Selected;
    > }
    >
    >

    Rick Strahl [MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139