Custom gridview - snapping using non-static function

I am currently trying to implement a gridview user interface to display data from an ObjectDataSource frontend to an ASP.net membership. The asp.net code for gridview is -

        <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>

      

My code is currently dying because the roleOfUser () get function is a public function. Preferably, I would like to pass the UserName string (pulled from the second cell of the GridView) to this function, but I am not sure about it, and also how to call this non-stationary function as I don’t know how to instantiate it from asp.net code.

    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;
    } 

      

0


a source to share


1 answer


It should be simple:

<% getRoleOfUser( ) %>

      



Assuming its page member function is in your GridView.

+1


a source







All Articles