By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,600 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,600 IT Pros & Developers. It's quick & easy.

Problems with Databinding during PreRender

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
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

P: n/a
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.