开发者

Listview: So you think you know itemDataBound?

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") %>' V开发者_如何学编程isible = '<%# 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()


    <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


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++;
        }
    }
}


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


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>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜