Initial Hide/Show of control in Gridview EditItemTemplate

I want to selectively hide or show a drop-down list(DropDownList2) in my EditItemTemplate in a GridView based on the value of a databound drop down(DropDownList1).

I can do this no problem using the onChange event of DDL1 to call a javascript function but initially when the user clicks the Edit button DDL2 is always visible when it shouldn't be unless the value of DDL1 is "Call Taken By".

Is there an event that fires after the user clicks the edit button and the controls are rendered?  I've tried using onRowEditing but this seems to fire before it switches to edit mode and my DDL controls are rendered and therefore cannot find the DDL controls to hide or show them.

Is there any other way to do this?  I'm using vb.net by the way.

Thanks.
<EditItemTemplate>
                            <asp:DropDownList DataTextField="Action" DataValueField="Action" onChange="ActionUser(this);" ID="DropDownList1" runat="server" SelectedValue='<%# IIf(left(DataBinder.Eval(Container.DataItem, "Action"),13) = "Call Taken By", "Call Taken By", Eval("Action")) %>' TabIndex="3">
                            <asp:ListItem style="color: red">Please Call Back</asp:ListItem>
                            <asp:ListItem style="color: blue">Left Voicemail</asp:ListItem>
                            <asp:ListItem style="color: green">Will Call Back</asp:ListItem>
                            <asp:ListItem style="color: green">Call Taken</asp:ListItem>
                            <asp:ListItem style="color: goldenrod">Call Taken By</asp:ListItem>
                            </asp:DropDownList>

                            <div id="ActionUser" style="visibility:visible; display:inline">
                            <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource5" DataTextField="FullName"
                            DataValueField="FullName" AppendDataBoundItems="True" TabIndex="4"
                            SelectedValue='<%# IIf(left(DataBinder.Eval(Container.DataItem, "Action"),13) = "Call Taken By", actionSubstringFunction(Eval("Action")), "PA") %>'>
                            <asp:ListItem Value="PA">PA</asp:ListItem>
                            </asp:DropDownList>    
                            </div>
                        </EditItemTemplate>

Open in new window

alankinaneAsked:
Who is Participating?
 
masterpassConnect With a Mentor Commented:
If you want to aceess controls, Its not possible (I thought you were going to do some JS function).... To access the controls you have to rely on RowDatabound
Protected Sub MembersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.RowState = DataControlRowState.Edit Then
            ' you can access all your controls here
        End If
    End If
End Sub

Open in new window

0
 
masterpassCommented:
can't you just change the visibility to hidden ..

When you want it to be shown you can change it to visibility: visible
<div id="ActionUser" style="visibility:hidden; display:inline">
                            <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource5" DataTextField="FullName"
                            DataValueField="FullName" AppendDataBoundItems="True" TabIndex="4"
                            SelectedValue='<%# IIf(left(DataBinder.Eval(Container.DataItem, "Action"),13) = "Call Taken By", actionSubstringFunction(Eval("Action")), "PA") %>'>
                            <asp:ListItem Value="PA">PA</asp:ListItem>
                            </asp:DropDownList>    
                            </div>

Open in new window

0
 
alankinaneAuthor Commented:
That's how I am changing the visibility via the javascript function based on the selected value of DDL1.  If I make it hidden initially (as you suggested) then I have the opposite problem in that DDL2 will be hidden initially when the user clicks 'edit' and I may want it visible initially if the value of DDL1 is 'Call Taken By'.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
masterpassCommented:
In that case your question need to be answerred

Is there an event that fires after the user clicks the edit button and the controls are rendered?---->

The answer is YES, it is OnRowEditing

OnRowEditing="GridView_RowEditing"

code behind
Protected Sub GridView_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
   
    MembersGridView.EditIndex = e.NewEditIndex
        'do your code here based on condition
End Sub

Open in new window

0
 
alankinaneAuthor Commented:
I've been trying onRowEditing but I can't seem to access my controls I just get a NullReference.

It looks to me like onRowEditing fires before the Gridview switches fully to edit mode and renders the controls in my EditItemTemplate.  If I'm mistaken could you provide the code to access the values of my DDL controls?
0
 
alankinaneAuthor Commented:
So close!  That seems to work for me but it's only working for the first row of my gridview.  Do I have to iterate through each row somehow or can I select the specific Row I want to edit?

Thanks for all the help.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            If e.Row.RowState = DataControlRowState.Edit Then

                Dim Action As DropDownList
                Action = CType(e.Row.FindControl("DropDownList1"), DropDownList)

                Dim ActionUser As DropDownList
                ActionUser = CType(e.Row.FindControl("EditCallTakenByUser"), DropDownList)

                If Action.Text = "Call Taken By" Then
                    ActionUser.Visible = True
                Else
                    ActionUser.Visible = False
                End If

            End If

        End If

End Sub

Open in new window

0
 
alankinaneAuthor Commented:
Ignore that, it's something else......
0
 
alankinaneAuthor Commented:
That code is working, it's just that the RowDataBound event only fires once so if I set the ActionUser.Visible property to false during this event then that control stays invisible, I need it to appear again if the user selects 'Call Taken By' from the other drop down.

I think I need to set an onIndexChanged event on the DropDownList1 control to check the condition and show the control again if necessary.  Not sure how to do this though....
0
 
alankinaneAuthor Commented:
Got this working.  I used the code below as my IndexChanged event along with the RowDataBound event above.  Thanks for your help.  Points Awarded.
    Protected Sub Action_IndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Action As DropDownList
        Dim ActionUser As DropDownList

        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        Dim row As GridViewRow = TryCast(ddl.NamingContainer, GridViewRow)

        Action = CType(row.FindControl("DropDownList1"), DropDownList)
        ActionUser = CType(row.FindControl("EditCallTakenByUser"), DropDownList)

        If row IsNot Nothing Then
            If Action.Text = "Call Taken By" Then
                ActionUser.Visible = True
            Else
                ActionUser.Visible = False
            End If
        End If
    End Sub

Open in new window

0
 
alankinaneAuthor Commented:
This was very helpful to me although I had to do a bit more work in code the onIndexChanged event of my DDL.
0
All Courses

From novice to tech pro — start learning today.