简体   繁体   English

数据绑定时的ASP.NET问题

[英]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() {"-"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 我知道它正确地通过了循环,因为我放入了response.write ,但是它只是数据绑定字符串中的最后一个条目

Any ideas how I can make it databind each string rather than just the last one? 有什么想法可以使它对每个字符串而不是最后一个字符串进行数据绑定吗?

Thanks 谢谢

You are calling DataBind() in a loop. 您正在循环调用DataBind()。 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). 大概这意味着您将希望在每次迭代时都将其追加nodes上,而不是分配给它(这将覆盖已经存在的内容)。 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. 您所做的每个DataBind()都会覆盖以前的数据。 And same with the DataSource . 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") ,将新节点附加到XmlNodeList

nodes += doc.SelectNodes("this/that[@number='" & mystring(x) & "']/something/blah") or whatever the equivalent VB code is. nodes += doc.SelectNodes("this/that[@number='" & mystring(x) & "']/something/blah")或任何等效的VB代码。

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. 抱歉,它是用C#语言编写的,但这是我认为您要尝试执行的一般操作。

我通过在转发器内使用asp:XmlDataSource修复了此问题,然后对转发器OnItemDataBound事件进行了数据绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM