如何操作sqldatasource返回的Listview itemtemplate中的值?

时间:2009-12-17 10:25:18

标签: asp.net listview events

我在ItemTemplate中有一个listview和一个标签,我已将文本设置为

<asp:Label id="GreenDate" runat="server" Text='<%# Eval("NewsDate") %>'></asp:Label>

但我想操纵datasource返回的值,即Eval(“NewsDate”)并在标签中显示新值。 在哪个事件我可以访问这个值以及如何?

2 个答案:

答案 0 :(得分:1)

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            HtmlTableCell lblpwdHeader = (HtmlTableCell)this.ListView1.FindControl("tdColumn");
            HtmlTableCell tdPwdData = (HtmlTableCell)e.Item.FindControl("tdPwd");

            if (lblpwdHeader != null)
            {
                lblpwdHeader.Visible = false;
            }
            if (tdPwdData != null)
            {
                tdPwdData.Visible = false;
            }
        }
    }




============

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="ListView1_ItemDataBound">
            <LayoutTemplate>
                <table border="0" cellpadding="1" width="50%">
                    <tr style="background-color: #E5E5FE">
                        <th style="width: 30%">
                            <asp:LinkButton ID="lnkId" runat="server" CommandName="Sort" CommandArgument="ID">Id</asp:LinkButton>
                        </th>
                        <th style="width: 40%">
                            <asp:LinkButton ID="lnkName" runat="server" CommandName="Sort" CommandArgument="UserName">Name</asp:LinkButton>
                        </th>
                        <th id="tdColumn" runat="server" style="width: 30%">
                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Sort" CommandArgument="Password">Password</asp:LinkButton>
                        </th>
                    </tr>
                    <tr id="itemPlaceholder" runat="server">
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <table border="0" cellpadding="1" cellspacing="2" width="50%">
                    <tr>
                        <td style="width: 30%; text-align: center">
                            <asp:Label runat="server" ID="lblId"><%#Eval("ID") %></asp:Label>
                        </td>
                        <td style="width: 40%; text-align: center">
                            <asp:Label runat="server" ID="lblName"><%#Eval("UserName")%></asp:Label>
                        </td>
                        <td id="tdPwd" runat="server" style="width: 30%; text-align: center">
                            <asp:Label ID="lblPwd" runat="server"><%#Eval("Password")%></asp:Label>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <table border="0" cellpadding="1" cellspacing="2" width="50%">
                    <tr>
                        <td style="width: 30%; text-align: center">
                            <asp:Label runat="server" ID="lblId"><%#Eval("ID") %></asp:Label>
                        </td>
                        <td style="width: 40%; text-align: center">
                            <asp:Label runat="server" ID="lblName"><%#Eval("UserName")%></asp:Label>
                        </td>
                        <td id="tdPwd" runat="server" style="width: 30%; text-align: center">
                            <asp:Label ID="lblPwd" runat="server"><%#Eval("Password")%></asp:Label>
                        </td>
                    </tr>
                </table>
            </AlternatingItemTemplate>
        </asp:ListView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestAshokConnectionString %>"
            SelectCommand="SELECT * FROM [Users]"></asp:SqlDataSource>

答案 1 :(得分:0)

使用以下活动:ItemDataBound,并使用与此DataBinder.Eval(e.Item.DataItem, "NewsDate")等效的<%# Eval("NewsDate") %>

以下是获取Label控件的语法:

Label lbl = (Label) Item.Controls.FindControlByID("GreenDate");