开发者

How to manipulate the value in Listview itemtemplate, returned by sqldatasource?

开发者 https://www.devze.com 2022-12-14 11:21 出处:网络
I have a listview and a开发者_StackOverflow label in the ItemTemplate, i have set the text to <asp:Label id=\"GreenDate\" runat=\"server\" Text=\'<%# Eval(\"NewsDate\") %>\'></asp:Lab

I have a listview and a开发者_StackOverflow label in the ItemTemplate, i have set the text to

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

but i want to manipulate the value returned by datasource,which is Eval("NewsDate") and show the new value in the label. in which event i can access this value and how?


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>


Use the following Event: ItemDataBound, and use DataBinder.Eval(e.Item.DataItem, "NewsDate") which is equivalent to this <%# Eval("NewsDate") %>.

Here's the syntax to get the Label Control:

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

精彩评论

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