Problems with Databinding during PreRender

M

Musravus

Hi all,

I have a piece of XML that looks like this, which I want to DataBind
to a Repeater (the Repeater is inside a UserControl):

<SECTION id="MAIN" >
<SUBSECTION id="SUB1" />
<SUBSECTION id="SUB2" />
<SUBSECTION id="SUB3" />
</SECTION>

The Repeater definition looks like this:

<asp:Repeater id="rpt1" runat="server">
<ItemTemplate>
<asp:LinkButton id="lbtnSubSection" runat="server"
EnableViewState=False CommandName=<%#
((System.Xml.XmlNode)Container.DataItem).Attributes["id"].Value %>
OnCommand="LinkButton_SubSectionCommand"><%#
((System.Xml.XmlNode)Container.DataItem).Attributes["id"].Value
%></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>

I bind the Repeater to the XmlNodeList using this code in
Page_PreRender:

XmlNodeList dataSourceNodeList =
myPieceOfXML.SelectNodes("SUBSECTION");
rpt1.DataSource = dataSourceNodeList;
rpt1.DataBind();

I have to do this in PreRender because the piece of XML may change
depending on a button I have clicked earlier, so I have to evaluate
this Click event before I can do the DataBind.

Once rendered the HTML looks like this:

<a id="uc1_rpt1__ctl1_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl1$lbtnSubSection','')">SUB1</a>
<a id="uc1_rpt1__ctl2_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl2$lbtnSubSection','')">SUB2</a>
<a id="uc1_rpt1__ctl3_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl3$lbtnSubSection','')">SUB3</a>

Upon postback it becomes clear that the CommandName property has not
been properly bound (e.CommandName evaluates to "") but the Text
property of the LinkButton has, even if they point to the same XML
attribute (id)! This does not happen if I put the code in Page_Load.
Why is this? Can anyone suggest a workaround?

Cheers, Eva
 
J

John Saunders

Musravus said:
Hi all,

I have a piece of XML that looks like this, which I want to DataBind
to a Repeater (the Repeater is inside a UserControl):

<SECTION id="MAIN" >
<SUBSECTION id="SUB1" />
<SUBSECTION id="SUB2" />
<SUBSECTION id="SUB3" />
</SECTION>

The Repeater definition looks like this:

<asp:Repeater id="rpt1" runat="server">
<ItemTemplate>
<asp:LinkButton id="lbtnSubSection" runat="server"
EnableViewState=False CommandName=<%#
((System.Xml.XmlNode)Container.DataItem).Attributes["id"].Value %>
OnCommand="LinkButton_SubSectionCommand"><%#
((System.Xml.XmlNode)Container.DataItem).Attributes["id"].Value
%></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>

I bind the Repeater to the XmlNodeList using this code in
Page_PreRender:

XmlNodeList dataSourceNodeList =
myPieceOfXML.SelectNodes("SUBSECTION");
rpt1.DataSource = dataSourceNodeList;
rpt1.DataBind();

I have to do this in PreRender because the piece of XML may change
depending on a button I have clicked earlier, so I have to evaluate
this Click event before I can do the DataBind.

Once rendered the HTML looks like this:

<a id="uc1_rpt1__ctl1_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl1$lbtnSubSection' said:
<a id="uc1_rpt1__ctl2_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl2$lbtnSubSection' said:
<a id="uc1_rpt1__ctl3_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl3$lbtnSubSection' said:
Upon postback it becomes clear that the CommandName property has not
been properly bound (e.CommandName evaluates to "") but the Text
property of the LinkButton has, even if they point to the same XML
attribute (id)! This does not happen if I put the code in Page_Load.
Why is this? Can anyone suggest a workaround?

Eva, does the same thing happen if you use quotes around the CommandName
value? I believe you'll have to use single quotes around 'id'.

Also, have you tried doing the DataBind within the click event?
 
C

Cosmin Marin

Hi,

You should enable the viewstate for your link buttons; since command name
property is being 'programmatically' modified, it must be persisted on the
client.

Cosmin

Musravus said:
Hi all,

I have a piece of XML that looks like this, which I want to DataBind
to a Repeater (the Repeater is inside a UserControl):

<SECTION id="MAIN" >
<SUBSECTION id="SUB1" />
<SUBSECTION id="SUB2" />
<SUBSECTION id="SUB3" />
</SECTION>

The Repeater definition looks like this:

<asp:Repeater id="rpt1" runat="server">
<ItemTemplate>
<asp:LinkButton id="lbtnSubSection" runat="server"
EnableViewState=False CommandName=<%#
((System.Xml.XmlNode)Container.DataItem).Attributes["id"].Value %>
OnCommand="LinkButton_SubSectionCommand"><%#
((System.Xml.XmlNode)Container.DataItem).Attributes["id"].Value
%></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>

I bind the Repeater to the XmlNodeList using this code in
Page_PreRender:

XmlNodeList dataSourceNodeList =
myPieceOfXML.SelectNodes("SUBSECTION");
rpt1.DataSource = dataSourceNodeList;
rpt1.DataBind();

I have to do this in PreRender because the piece of XML may change
depending on a button I have clicked earlier, so I have to evaluate
this Click event before I can do the DataBind.

Once rendered the HTML looks like this:

<a id="uc1_rpt1__ctl1_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl1$lbtnSubSection' said:
<a id="uc1_rpt1__ctl2_lbtnSubSection"
 
M

Musravus

Got it... It's actually really simple - I had EnableViewState set to
false for those controls that did not bind properly. Apparently this
influences the way the databinding process works. It kind of makes
sense when you think of it.

Thanks all for your suggestions. You were correct. FYI I did find a
workaround, by placing a hidden input variable inside the
<itemtemplate> tag, like this:

<asp:Repeater id="rpt1" runat="server">
<ItemTemplate>
<asp:LinkButton id="lbtnSubSection" runat="server"
EnableViewState=False
OnCommand="LinkButton_SubSectionCommand"><%#((System.Xml.XmlNode)Container.DataItem).Attributes["name"].Value%></asp:LinkButton>
<input type=hidden runat=server
value=<%#((System.Xml.XmlNode)Container.DataItem).Attributes["id"].Value%>
id=hdSubSection>
</ItemTemplate>
</asp:Repeater>

This renders correctly - I reckon the "Value" attribute is a render
property so it binds correctly during Page_PreRender, whereas the
"CommandName" attribute is something that needs to be bound earlier,
like in Page_Init or Page_Load.
Upon postback, in my Command event code after a click on one of the
Repeater's LinkButtons, I can then get hold of the "id" value by
accessing the hidden variable's Value:

LinkButton controlClicked = (LinkButton) sender;
HtmlInputHidden hiddenVar =
(HtmlInputHidden)controlClicked.Parent.FindControl("hdSubSection");
string id = hiddenVar.Value;

This I can then use in Page_PreRender to create the XML to re-bind to
the Repeater. Sweet music.
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top