470,586 Members | 1,358 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,586 developers. It's quick & easy.

Problems with Databinding during PreRender

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).Attribute s["id"].Value %>
OnCommand="LinkButton_SubSectionCommand"><%#
((System.Xml.XmlNode)Container.DataItem).Attribute s["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$lbtn SubSection','')">SUB1</a>
<a id="uc1_rpt1__ctl2_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl2$lbtn SubSection','')">SUB2</a>
<a id="uc1_rpt1__ctl3_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl3$lbtn SubSection','')">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
Nov 18 '05 #1
3 2462
"Musravus" <ev********@hotmail.com> wrote in message
news:be************************@posting.google.com ...
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).Attribute s["id"].Value %>
OnCommand="LinkButton_SubSectionCommand"><%#
((System.Xml.XmlNode)Container.DataItem).Attribute s["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$lbtn SubSection','')">SUB1</a> <a id="uc1_rpt1__ctl2_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl2$lbtn SubSection','')">SUB2</a> <a id="uc1_rpt1__ctl3_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl3$lbtn SubSection','')">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?


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?
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #2
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" <ev********@hotmail.com> wrote in message
news:be************************@posting.google.com ...
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).Attribute s["id"].Value %>
OnCommand="LinkButton_SubSectionCommand"><%#
((System.Xml.XmlNode)Container.DataItem).Attribute s["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$lbtn SubSection','')">SUB1</a> <a id="uc1_rpt1__ctl2_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl2$lbtn SubSection','')">SUB2</a> <a id="uc1_rpt1__ctl3_lbtnSubSection"
href="javascript:__doPostBack('uc1$rpt1$_ctl3$lbtn SubSection','')">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

Nov 18 '05 #3
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"><%#((Syst em.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.
Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Thomas | last post: by
2 posts views Thread by BluDog | last post: by
5 posts views Thread by Peter Rilling | last post: by
2 posts views Thread by Al Smith | last post: by
4 posts views Thread by Nathan Sokalski | last post: by
8 posts views Thread by GaryDean | last post: by
9 posts views Thread by J055 | last post: by
9 posts views Thread by Nathan Sokalski | last post: by
2 posts views Thread by Sebastian | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.