15
2012
04

Asp.Net 使用Repeater绑定数据,并进行数据转换

使用Repeater绑定数据

        <table cellpadding="0" cellspacing="1" style="width: 730px; vertical-align: middle;">
            <asp:Repeater ID="RpDB" runat="server" OnItemDataBound="RpDB_ItemDataBound">
                <ItemTemplate>
                    <tr class="title12666666" style="height: 28px; width: 730px; vertical-align: middle;">
                        <td style="width: 110px; height: 130px; text-align: center;">
                            <asp:Label ID="ZiDuan1" runat="server" Text='<%#Eval("ZiDuan1")%>' Visible="false"></asp:Label>
                        </td>
                        <td align="left" style="width: 245px; text-align: left;">
                            <table style="width: 100%; text-align: left;">
                                <tr style="height: 28px;">
                                    <td style="width: 50%;">
                                        姓名:<%#Eval("Name") %>
                                    </td>
                                    <td style="width: 50%;">
                                        证号:<%# (Eval("ID").ToString())%>
                                    </td>
                                    <td style="width: 50%;">
                                        等级:<asp:Label ID="Level" runat="server" Text='<%# (Eval("Level").ToString())%>'></asp:Label>
                                    </td>
                                </tr>
                            </table>
                        </td>
                </ItemTemplate>
                <AlternatingItemTemplate>
                    <td style="width: 110px; height: 130px; text-align: center;">
                        <asp:Label ID="ZiDuan1" runat="server" Text='<%#Eval("ZiDuan1")%>' Visible="false"></asp:Label>
                    </td>
                    <td align="left" style="width: 245px; text-align: left;">
                        <table style="width: 100%; text-align: left;">
                            <tr style="height: 28px;">
                                <td style="width: 50%;">
                                    姓名:<%#Eval("Name") %>
                                </td>
                                <td style="width: 50%;">
                                    证号:<%# (Eval("ID").ToString())%>
                                </td>
                                <td style="width: 50%;">
                                    等级:<asp:Label ID="Level" runat="server" Text='<%# (Eval("Level").ToString())%>'></asp:Label>
                                </td>
                            </tr>
                        </table>
                    </td>
                    </tr>
                </AlternatingItemTemplate>
                <FooterTemplate>
                    <tr class="title12666666" style="height: auto; width: 730px; vertical-align: middle;">
                        <td style="width: 98%; height: 40px; text-align: center;" colspan="4">
                            <asp:Label ID="lblEmptyZP" Text="暂时没有您需要的信息" runat="server" Visible='<%#bool.Parse((RpTourConductor.Items.Count==0).ToString())%>'>
                            </asp:Label>
                        </td>
                    </tr>
                </FooterTemplate>
            </asp:Repeater>
        </table>

后台使用RpDB_ItemDataBound进行数据转换

    protected void RpTourConductor_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            try
            {
                string dj = ((Label)e.Item.FindControl("Level")).Text.Trim().ToString();  //获取Level的值
                switch (dj)  /使用switch 进行判断转换
                {
                    case "1": ((Label)e.Item.FindControl("Level")).Text = "初级"; break;
                    case "2": ((Label)e.Item.FindControl("Level")).Text = "中级"; break;
                    case "3": ((Label)e.Item.FindControl("Level")).Text = "高级"; break;
                    case "4": ((Label)e.Item.FindControl("Level")).Text = "特级"; break;
                }

            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
        }
    }

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。