页面绑定使用服务器方法

后台CS代码:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Hashtable list = new Hashtable();
                list.Add("1", "1");
                list.Add("2", "2");
                list.Add("3", "3");
                this.DataList1.DataSource = list;
                this.DataList1.DataBind();
            }

        }
        public int GetSumCount(int n, int m)
        {
            return n + m;
        }

前台页面HTML代码:

    <div>
        <asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333">
            <HeaderTemplate>
                <table width="200">
                    <tr>
                        <td width="100">
                            数值1
                        </td>
                        <td>
                            数值2
                        </td>
                        <td>
                            求和
                        </td>
                    </tr>
                </table>
            </HeaderTemplate>
            <ItemTemplate>
                <table width="200">
                    <tr>
                        <td width="100">
                            <%# ((DictionaryEntry)Container.DataItem).Key %>
                        </td>
                        <td>
                            <%# ((DictionaryEntry)Container.DataItem).Value %>
                        </td>
                        <td>
                            <%# GetSumCount(int.Parse(((DictionaryEntry)Container .DataItem).Key.ToString()) ,int.Parse(((DictionaryEntry)Container .DataItem).Value.ToString()))%>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
    </div>

其中调用了后台CS服务端的方法GetSumCount 进行计算

最终显示结果:

posted on 2011-03-11 10:25  云起的自学笔记  阅读(281)  评论(0编辑  收藏  举报

导航