开发者

ASP.NET problem when databinding

I'm trying to do a Loop with a databind in it but it isn't working as planned and I've no idea why.

Session("mysession") = "1234-5678-"

Dim delimiters As Char() = New Char() 开发者_JS百科{"-"C}

Dim nodes As XmlNodeList

Dim mystring As String() = Trim(Session("mysession")).Split(delimiters, StringSplitOptions.RemoveEmptyEntries)

For x = 0 to mystring.Length - 1 

nodes = doc.SelectNodes("this/that[@number='" & mystring(x) & "']/something/blah")

Response.write(mystring(x))

repeater.DataSource = nodes
repeater.DataBind()

Next

I know that it is getting through the loop properly because I put the response.write in but it is only databinding the last entry in the string

Any ideas how I can make it databind each string rather than just the last one?

Thanks


You are calling DataBind() in a loop. Each time through it overwrites the previous binding values.


You're re-binding with each iteration of the loop, so after the loop finishes it will be bound to the last thing you told it to bind to, which is the last iteration of the loop.

(Note: Data binding doesn't mean "append this data to the control's existing data" but rather "use this data for the control." It's a destructive operation, replacing what was previously there.)

You should build your data source first (presumably with the loop) and then bind to the fully-built data source after the loop is completed. Presumably this means you'll want to append to nodes with each iteration, rather than assign to it (which overwrites what's already there). However, you'll want to debug a little and make sure the appended version still makes sense data-wise and can still be bound to. It may need some delineation between loop iterations, etc.


Every DataBind() you do overwrite the previous ones. And same with the DataSource. Move those outside of the loop, and instead of
nodes = doc.SelectNodes("this/that[@number='" & mystring(x) & "']/something/blah") , append the new node to the XmlNodeList.

nodes += doc.SelectNodes("this/that[@number='" & mystring(x) & "']/something/blah") or whatever the equivalent VB code is.


it sounds like you need to put your repeater into another repeater. Something like

<Repeater ID="outerrepeater">
<Repeater ID="innerrepeater" />
</Repeater>

Then in your code behind

PageLoad or whatever
{
    outerrepeater.OnDataBound += new RepeaterDataBoundEvent(databind); //or is it ondatabinding
    outerrepeater.DataSource = mystring;
    outerrepeater.DataBind();
}

void databind(object sender, EventArgs e)
{
    Repeater inner = ((Repeater)((Repeater)sender).FindControl("innerrepeater"));
    inner.DataSource = doc.SelectNodes("this/that[@number='" & mystring(x) & "']/something/blah");
    inner.DataBind();
}

Sorry it's in C# but that is the general jist of what I think you are trying to do.


I fixed this by using an asp:XmlDataSource inside a repeater and then did my Databind on the repeater OnItemDataBound event

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜