views:

13

answers:

2

I would like to show different values in a dropdownlist, while editing a gridview, depending on which user is logged in. For example...

The officer would see "Approved by Officer" status. The director would see "Approved by Director" status

I am trying to add these programatically to a dropdownlist which I have in the edit template of my gridview (approvalsGrid). Here is my code:

   Protected Sub ApprovalsGrid_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles ApprovalsGrid.RowUpdating
        Dim ApprovalEditDD As DropDownList = CType(ApprovalsGrid.Rows(ApprovalsGrid.EditIndex).FindControl("ApprovalEdit"), DropDownList)
        If User.Identity.Name = "officer" Then
            ApprovalEditDD.Items.Add("Approved by Officer")
        End If
    End Sub

I do not get any errors. But get an empty dropdown with no items. Am I using the correct event?

+1  A: 

Try the event: ApprovalsGrid_RowEditing instead of ApprovalsGrid_RowUpdating. The GridView event RowUpdating fires when the user submits their updates to the datasource, but before those updates actually occur (RowUpdated fires after the updates occur). RowEditing fires as the GridView enters edit mode, this seems to be the proper place to try to bind your Drop Down List. Still be careful with PostBacks and such things to make sure your options in the list stay there, as I haven't set up the whole process to try it out.

Plus here is an MSDN link that explains all the different GridView events (there are a lot of them).

ben f.
+1  A: 

Why not try this

<script runat=server>
    string GetApprovedBy()
    {
        if (User.Identity.Name == "officer")
        {
            return "Approved by Officer";
        }
        else if(User.Identity.Name == "some other name")
        {
            return "something else";
        }
        else return string.Empty;
    } 
</script>
<asp:GridView runat="server">
    <Columns>
        <asp:TemplateField>
            <EditItemTemplate>
                <asp:DropDownList runat="server">
                    <asp:ListItem Text='<%= GetApprovedBy() %>' />
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

This of course works if each user type would have an associated drop down item. Does this fit your needs?

scripni
This sounded like a great solution. Except when I try it out I get 'Code Blocks are not support in this context'. I then try to do the processing in the codebehind and just output the result in a literal or variable which also did not work.
Phil
i just copied and pasted this code in an .aspx page and the page showed with no errors, did you copy this exact text? When do you get this exception?
scripni