Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Data binding glitch

2 views
Skip to first unread message

Kevin Donn

unread,
Jul 13, 2006, 4:10:59 PM7/13/06
to
I'm not at all sure which group to post this in, but since it involves a
component I'm writing, I'll post it here. I'm going to try to strike the
balance between giving too much detail and leaving out too much detail, but
it'll be tough. I've written a component that inherits CheckBox called
TLWRemoveCheckBox. I've added a property to this component called Seq which
is a string. I'm using this component in the ItemTemplate of a Repeater.
The following snippet works:

<ASP:Repeater id="PickListRepeater" runat="server" datasource="<%#
PickListQuery %>">
<ItemTemplate>
<input id="RES_SEQ" type="hidden" value='<%#
DataBinder.Eval(Container.DataItem, "RES_SEQ") %>' />
<lw:TLWRemoveCheckBox id="RemoveCheckBox" runat="server"
Seq='5'
oncheckedchanged="RemoveCheckBoxCheckChanged">
</ItemTemplate>
</ASP:Repeater>

When my page gets posted back, if I've clicked on one of my
RemoveCheckBoxes, I can reference the Seq property and see the 5 in there.
Plus, if I look at the page source at the browser, I can see that my RES_SEQ
hidden input has evaluated properly for each line (RES_SEQ is a database
key).

Now, what doesn't work is this:

<ASP:Repeater id="PickListRepeater" runat="server" datasource="<%#
PickListQuery %>">
<ItemTemplate>
<input id="RES_SEQ" type="hidden" value='<%#
DataBinder.Eval(Container.DataItem, "RES_SEQ") %>' />
<lw:TLWRemoveCheckBox id="RemoveCheckBox" runat="server"
Seq='<%# DataBinder.Eval(Container.DataItem, "RES_SEQ") %>'
oncheckedchanged="RemoveCheckBoxCheckChanged">
</ItemTemplate>
</ASP:Repeater>

The only thing different is that I've replaced the 5 with <%#
DataBinder.Eval(Container.DataItem, "RES_SEQ") %> so that Seq will have a
generated value for each instance of RemoveCheckBox. I don't get an error,
I just find that Seq is an empty string on post back. What gives? Even
using the Eval with asp:TextBox works as expected:

<ASP:Repeater id="PickListRepeater" runat="server" datasource="<%#
PickListQuery %>">
<ItemTemplate>
<input id="RES_SEQ" type="hidden" value='<%#
DataBinder.Eval(Container.DataItem, "RES_SEQ") %>' />
<asp:TextBox runat="server" id="fubar" text='<%#
DataBinder.Eval(Container.DataItem, "RES_SEQ") %>'></asp:textbox>
<lw:TLWRemoveCheckBox id="RemoveCheckBox" runat="server"
Seq='<%# DataBinder.Eval(Container.DataItem, "RES_SEQ") %>'
oncheckedchanged="RemoveCheckBoxCheckChanged">
</lw:TLWRemoveCheckBox>
</ItemTemplate>
</ASP:Repeater>

Any thoughts much appreciated.

Thanks,
Kevin Donn


Kevin Donn

unread,
Jul 13, 2006, 5:59:18 PM7/13/06
to
It struck me that one thing that's different about this:

<lw:TLWRemoveCheckBox id="RemoveCheckBox" runat="server"
Seq='5'
oncheckedchanged="RemoveCheckBoxCheckChanged">

and this:

<lw:TLWRemoveCheckBox id="RemoveCheckBox" runat="server"
Seq='<%# DataBinder.Eval(Container.DataItem, "RES_SEQ") %>'
oncheckedchanged="RemoveCheckBoxCheckChanged">

is that the first being a literal wouldn't be dependent upon ViewState. The
5 would be there after page load whether it was in ViewState or not. Also I
added this handler:

procedure TWebForm1.PickListRepeater_ItemDataBound(sender: System.Object; e:
System.Web.UI.WebControls.RepeaterItemEventArgs);
begin
if e.Item.ItemType=ListItemType.Item then
Response.Write(TLWRemoveCheckBox(e.Item.FindControl('RemoveCheckBox')).Seq+'<BR>'#10)
end;

And was able to confirm that each instance of RemoveCheckBox is definitely
getting a value. They just don't appear to be making the round trip back to
the server. I tried "EnableViewState:=true" in my constructor but that
didn't work. Is there something special I need to do to get this property
into the ViewState?

Kevin Donn


Kevin Donn

unread,
Jul 19, 2006, 1:33:55 PM7/19/06
to
Well, since there is a grand total of seven threads in this group, I'll take
a moment to relate the resolution of my problem. It all boils down to the
fact that I wasn't storing my property in the ViewState. I had made a
custom CheckBox as follows:

TLWRemoveCheckBox = class(System.Web.UI.WebControls.CheckBox)
private
FSeq: string;
published
[Bindable(true), Category('Appearance'), DefaultValue('')]
property Seq: string read FSeq write FSeq;
end;

What I needed was:
property Seq: string read GetSeq write SetSeq;

function TLWRemoveCheckBox.GetSeq: string;
begin
result:=string(ViewState['Seq'])
end;

procedure TLWRemoveCheckBox.SetString(const value: string);
begin
ViewState['Seq']:=value
end;

I was imagining that properties, particularly published properties, would be
persisted in the ViewState automatically, but not so.

Kevin Donn


Kevin Donn

unread,
Jul 19, 2006, 2:11:18 PM7/19/06
to
Is explicitly writing values to the ViewState the only way to get properties
to persist? Is there a way to have a property stored as an object field and
make it persist to the ViewState?

Thanks,
Kevin Donn


0 new messages