Listview:所以你认为你了解 itemDataBound?

发布于 2024-09-02 22:36:02 字数 2428 浏览 5 评论 0原文

在我的网页中,我使用以下内容来填充列表视图控件

<asp:ListView ID="ListView1" runat="server">
<layouttemplate>
<asp:PlaceHolder id="itemPlaceholder" runat="server" /></layouttemplate>
<ItemTemplate>
<tr>
            <td><asp:Label ID="Label1" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans1") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans1Visible") %>'></asp:Label>
                <br />
                <asp:Label ID="Label2" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans2") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans2Visible") %>'></asp:Label>
                <br />
                <asp:Label ID="Label3" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans3") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans3Visible") %>'></asp:Label>
                <br />
                <asp:Label ID="Label4" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans4") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans4Visible") %>'></asp:Label>
                <br />
                <asp:Label ID="Label5" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans5") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans5Visible") %>'></asp:Label>
                <br />
                <asp:Label ID="Label6" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans6") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans6Visible") %>'></asp:Label>
           </td>
        </tr>
</ItemTemplate>
</asp:ListView>

现在我想在渲染标签之前将数字添加到标签中。

例如,当前显示的数据如下

Tennis
Football
Basketball
Nfl
Nba
Polo

,我想要的输出是

1. Tennis
2. Football
3. Basketball
4. Nfl
5. Nba
6. Polo

我可以使用 ListView1_ItemCreated 或 ListView1_ItemDataBound 事件来实现此目的吗? 如果这是真的,你能给我指出一个起点吗?

PS列表视图充满了

Dt = GetDataTable("SELECT Ans1, Ans2,Ans3,Ans4,Ans5,Ans6, Ans1Visible,Ans2Visible,Ans3Visible,Ans4Visible,Ans5Visible,Ans6Visible, From myTable WHERE CatID ='" & cat & "'")
        ListView1.DataSource = Dt
        ListView1.DataBind()

In my webpage i use the following in order filling the listview control

<asp:ListView ID="ListView1" runat="server">
<layouttemplate>
<asp:PlaceHolder id="itemPlaceholder" runat="server" /></layouttemplate>
<ItemTemplate>
<tr>
            <td><asp:Label ID="Label1" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans1") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans1Visible") %>'></asp:Label>
                <br />
                <asp:Label ID="Label2" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans2") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans2Visible") %>'></asp:Label>
                <br />
                <asp:Label ID="Label3" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans3") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans3Visible") %>'></asp:Label>
                <br />
                <asp:Label ID="Label4" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans4") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans4Visible") %>'></asp:Label>
                <br />
                <asp:Label ID="Label5" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans5") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans5Visible") %>'></asp:Label>
                <br />
                <asp:Label ID="Label6" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans6") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans6Visible") %>'></asp:Label>
           </td>
        </tr>
</ItemTemplate>
</asp:ListView>

Now i would like to add numbers to the labels before they are rendered.

For example currently the data displayed are like

Tennis
Football
Basketball
Nfl
Nba
Polo

and the output i would like to have is

1. Tennis
2. Football
3. Basketball
4. Nfl
5. Nba
6. Polo

Could i use ListView1_ItemCreated or the ListView1_ItemDataBound event to achieve this?
If that is true, could you point me a place to start?

P.S. the list view is filled with

Dt = GetDataTable("SELECT Ans1, Ans2,Ans3,Ans4,Ans5,Ans6, Ans1Visible,Ans2Visible,Ans3Visible,Ans4Visible,Ans5Visible,Ans6Visible, From myTable WHERE CatID ='" & cat & "'")
        ListView1.DataSource = Dt
        ListView1.DataBind()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不弃不离 2024-09-09 22:36:02
    <asp:ListView ID="ListView1" runat="server">
    <layouttemplate>
    <asp:PlaceHolder id="itemPlaceholder" runat="server" /></layouttemplate>
    <ItemTemplate>
    <ol>

<asp:Literal ID="Label1" runat="server" Text = '<%# Eval("Ans1","<li>{0}</li>") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans1Visible") %>'></asp:Label>

 <asp:Literal ID="Label2" runat="server" Text = '<%# Eval( "Ans2","<li>{0}</li>") %>' Visible = '<%# Eval(Container.DataItem, "Ans2Visible") %>'></asp:Label>

<asp:Literal ID="Label3" runat="server" Text = '<%# DataBinder.Eval("Ans3","<li>{0}</li>") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans3Visible") %>'></asp:Label>

 <asp:Label ID="Label4" runat="server" Text = '<%# Eval("Ans4","<li>{0}</li>) %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans4Visible") %>'></asp:Label>
</ol>
    </ItemTemplate>
    </asp:ListView>

最好使用 OL(有序列表)。然后生成列表元素作为
Eval 格式字符串。如果您对数字进行硬编码,则当项目不可见时(visible=false),它们可能会显示不正确。您可以使用 CSS 更改 li 的显示样式

    <asp:ListView ID="ListView1" runat="server">
    <layouttemplate>
    <asp:PlaceHolder id="itemPlaceholder" runat="server" /></layouttemplate>
    <ItemTemplate>
    <ol>

<asp:Literal ID="Label1" runat="server" Text = '<%# Eval("Ans1","<li>{0}</li>") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans1Visible") %>'></asp:Label>

 <asp:Literal ID="Label2" runat="server" Text = '<%# Eval( "Ans2","<li>{0}</li>") %>' Visible = '<%# Eval(Container.DataItem, "Ans2Visible") %>'></asp:Label>

<asp:Literal ID="Label3" runat="server" Text = '<%# DataBinder.Eval("Ans3","<li>{0}</li>") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans3Visible") %>'></asp:Label>

 <asp:Label ID="Label4" runat="server" Text = '<%# Eval("Ans4","<li>{0}</li>) %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans4Visible") %>'></asp:Label>
</ol>
    </ItemTemplate>
    </asp:ListView>

You are better off using OL (Ordered List).Then you generate the list elements as part of the
Eval format string. If you hardcode your numbers they may display incorrectly when items are not visible(visible=false). You can change the style of display of li using CSS

地狱即天堂 2024-09-09 22:36:02

我认为 josephj1989 位于正确的路径上,但缺少您在评论中所述的必需的表行代码。

完整的解决方案如下所示:

<asp:ListView ID="ListView1" runat="server">
  <LayoutTemplate>
    <asp:PlaceHolder id="itemPlaceholder" runat="server" />
  </LayoutTemplate>
  <ItemTemplate>
    <tr>
      <td>
        <ol>
          <asp:Literal ID="Literal1" runat="server" Text = '<%# Eval("Ans1","<li>{0}</li>") %>' Visible = '<%# Eval("Ans1Visible") %>' />
          <asp:Literal ID="Literal2" runat="server" Text = '<%# Eval("Ans2","<li>{0}</li>") %>' Visible = '<%# Eval("Ans2Visible") %>' />
          <asp:Literal ID="Literal3" runat="server" Text = '<%# Eval("Ans3","<li>{0}</li>") %>' Visible = '<%# Eval("Ans3Visible") %>' />
          <asp:Literal ID="Literal4" runat="server" Text = '<%# Eval("Ans4","<li>{0}</li>) %>' Visible = '<%# Eval("Ans4Visible") %>' />
        </ol>
      </td>
    </tr>
  </ItemTemplate>
</asp:ListView>

如果您想使用 OnItemDataBound 方法,以下事件处理程序将适合您:

protected void lvData_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
    int count = 1;

    foreach (Literal lit in e.Item.Controls.OfType<Literal>())
    {
        if (lit.Visible)
        {
            lit.Text = String.Format("{0}. {1}", count.ToString(), lit.Text);
            count++;
        }
    }
}

I think that josephj1989 is on the correct path, but is missing the table row code that you stated in your comments was required.

The complete solution would be something like this:

<asp:ListView ID="ListView1" runat="server">
  <LayoutTemplate>
    <asp:PlaceHolder id="itemPlaceholder" runat="server" />
  </LayoutTemplate>
  <ItemTemplate>
    <tr>
      <td>
        <ol>
          <asp:Literal ID="Literal1" runat="server" Text = '<%# Eval("Ans1","<li>{0}</li>") %>' Visible = '<%# Eval("Ans1Visible") %>' />
          <asp:Literal ID="Literal2" runat="server" Text = '<%# Eval("Ans2","<li>{0}</li>") %>' Visible = '<%# Eval("Ans2Visible") %>' />
          <asp:Literal ID="Literal3" runat="server" Text = '<%# Eval("Ans3","<li>{0}</li>") %>' Visible = '<%# Eval("Ans3Visible") %>' />
          <asp:Literal ID="Literal4" runat="server" Text = '<%# Eval("Ans4","<li>{0}</li>) %>' Visible = '<%# Eval("Ans4Visible") %>' />
        </ol>
      </td>
    </tr>
  </ItemTemplate>
</asp:ListView>

If you are wanting to use the OnItemDataBound approach, the following event handler would work for you:

protected void lvData_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
    int count = 1;

    foreach (Literal lit in e.Item.Controls.OfType<Literal>())
    {
        if (lit.Visible)
        {
            lit.Text = String.Format("{0}. {1}", count.ToString(), lit.Text);
            count++;
        }
    }
}
叹沉浮 2024-09-09 22:36:02

上述示例的替代方法是在 SQL Select 语句中使用 ROW_NUMBER() 与 Ans 字段连接。

An alternative to the example above would be to concat using ROW_NUMBER() in the SQL Select statement with the Ans field.

独夜无伴 2024-09-09 22:36:02

根据您的代码和您想要显示的内容,似乎 itemCreated 和 ItemDataBound 事件只会被触发一次,这意味着(至少对我来说)它们不是添加这些数字的好地方(除非它是 1每次-6)。

您确定您完全了解ListView应该如何使用吗?您使用什么作为此 ListView 的数据源?

如果您确定已按照您希望的方式设置了所有内容,则可以将 1-6 硬编码到标记中:

<tr>
            <td>1:<asp:Label ID="Label1" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans1") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans1Visible") %>'></asp:Label>
                <br />
                2:<asp:Label ID="Label2" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans2") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans2Visible") %>'></asp:Label>
                <br />
                3:<asp:Label ID="Label3" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans3") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans3Visible") %>'></asp:Label>
                <br />
                4:<asp:Label ID="Label4" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans4") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans4Visible") %>'></asp:Label>
                <br />
                5:<asp:Label ID="Label5" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans5") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans5Visible") %>'></asp:Label>
                <br />
                6:<asp:Label ID="Label6" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans6") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans6Visible") %>'></asp:Label>
           </td>
        </tr>

Based on your code and what you want displayed it seems like the itemCreated and ItemDataBound events will only be fired once which would mean (to me at least) that they wouldn't be a great place to add these numbers (unless it will be 1-6 everytime).

Are you sure you fully understand how the ListView should be used? What are you using as your datasource for this ListView?

If you are sure you have everything setup the way you would like you could just hardcode 1-6 into the markup:

<tr>
            <td>1:<asp:Label ID="Label1" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans1") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans1Visible") %>'></asp:Label>
                <br />
                2:<asp:Label ID="Label2" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans2") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans2Visible") %>'></asp:Label>
                <br />
                3:<asp:Label ID="Label3" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans3") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans3Visible") %>'></asp:Label>
                <br />
                4:<asp:Label ID="Label4" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans4") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans4Visible") %>'></asp:Label>
                <br />
                5:<asp:Label ID="Label5" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans5") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans5Visible") %>'></asp:Label>
                <br />
                6:<asp:Label ID="Label6" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans6") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans6Visible") %>'></asp:Label>
           </td>
        </tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文