自定义gridview - 使用非静态函数绑定

时间:2009-05-26 16:26:50

标签: c# asp.net data-binding

我目前正在尝试实现自定义gridview接口,以显示从ObjectDataSource前端到ASP.net成员资格的数据。 gridview的asp.net代码是

        <asp:GridView
            id="grdUsers"
            HeaderStyle-cssclass="grid_header"
            RowStyle-cssclass="row"
            AlternatingRowStyle-cssclass="alternating"
            OnRowUpdating="grdUsers_RowUpdating"
            OnRowDeleting="grdUsers_RowDeleting"
            OnRowCancelingEdit="grdUsers_cancelEdit"
            datasourceid="srcUsers"
            autogeneratecolumns="false"
            allowsorting="true"
            AllowPaging="true"
            EmptyDataText="No users..."
            pagesize="9"
            runat="server">
            <Columns>
                <asp:CommandField
                    HeaderText="Ops"
                    ButtonType="Image"
                    ShowEditButton="true"
                    EditText="E"
                    UpdateText="U"
                    ShowCancelButton="true"
                    CancelText="X"
                    ShowDeleteButton="true"
                    DeleteText="D" />
                <asp:BoundField
                    ReadOnly="true"
                    DataField="UserName"
                    HeaderText="UserName" />
                <asp:BoundField
                    DataField="Email"
                    HeaderText="Contact Email" />
                <asp:TemplateField
                    HeaderText="Role">
                    <ItemTemplate>
                    <asp:Label 
                        ID="lblRole"
                        text="<%# namespace.Admin.getRoleOfUser( ) %>" 
                        runat="server" />
                    </ItemTemplate>
                    </asp:TemplateField>
                <asp:CheckBoxField
                    DataField="IsApproved"
                    HeaderText="User is Approved" />
                <asp:TemplateField 
                    HeaderText="UserLocked">
                    <ItemTemplate>
                    <asp:CheckBox 
                        id="grdUsers_lockCheckBox"
                        Checked='<%# Eval("IsLockedOut") %>'
                        Enabled="false"
                        runat="server" />
                    </ItemTemplate>
                    <EditItemTemplate>
                    <asp:Button 
                        id="grdUsers_unlockUser"
                        OnClick="grdUsers_unlockUser"
                        Text="Unlock"
                        runat="server" />
                    <asp:HyperLink 
                        id="grdUsers_lockInfo"
                        text="?"
                        onclick='popup("lock_info.html")'
                        runat="server" /> 
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            </asp:GridView>

我的代码目前正在死亡,因为函数get roleOfUser()是一个公共函数。我希望将UserName的一个字符串(从GridView的第二个单元格中拉出)传递给此函数,但我不确定,以及如何调用此非静态函数,因为我不知道如何从asp.net代码实例化它。

    public string getRoleOfUser( )
    {
        GridView users = (GridView)admin.FindControlRecursive(Page, "grdUsers");
        Int32 i = users.EditIndex;

        GridViewRow userRow = users.Rows[i];

        return userRow.Cells[1].Text;
    }

    private Control FindControlRecursive(Control root, string id)
    {
        if (root.ID == id)
        {
            return root;
        }

        foreach (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, id);
            if (t != null)
            {
                return t;
            }
        }

        return null;
    } 

1 个答案:

答案 0 :(得分:1)

应该是:

<% getRoleOfUser( ) %>

假设它是GridView所在页面的成员函数。