0

I have a gridview which lists Tools and Access values. To edit I have an edit imagebutton on each row. I have an OnRowBound method which assigns an OnClick attribute to each button so that I will know which record I need to edit. The code is

    Protected Sub ChangeFirstRowIcon(ByVal Sender As Object, ByVal e As GridViewRowEventArgs) Handles gv_AccessRights.RowDataBound

    'This sub fires on each gridview row created...
    'It first checks that the row is a data row (as opposed to Header, Footer etc.)
    'If ib_Edit is true then change add an attribut to button with aid, tid and ac values attached.

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim ib_Edit As ImageButton = e.Row.FindControl("ib_Edit")
        Dim lb_AccessID As Label = e.Row.FindControl("lb_AccessID")
        Dim hd_ToolID As HiddenField = e.Row.FindControl("hd_ToolID")
        Dim hd_AccessCode As HiddenField = e.Row.FindControl("hd_AccessCode")

        If ib_Edit IsNot Nothing Then
            ib_Edit.Attributes.Add("onClick", "proxyClick('" & lb_AccessID.Text & "', '" & hd_ToolID.Value & "', '" & hd_AccessCode.Value & "')")
        End If

    End If

End Sub

I'm using a hidden proxy button to show a modal popup which the user will use to edit a record... (the same popup will be used to add a new access record... but that will come later). So having passed my details to proxyClick I set values to controls within the modal popup. The javascript is....

    <script type="text/javascript">
    function proxyClick(aid, tid, ac) {
        document.getElementById('hd_AccessID').value = aid;
        document.getElementById('hd_ToolIDMod').value = tid;
        document.getElementById('hd_AccessCodeMod').value = ac;
        document.getElementById('but_SetModalDetails').click();
    }
    </script>

For reference the main bits of the markup are....

<table class="border">
    <tr>
        <td>
            <asp:Button ID="but_SetModalDetails" runat="server" Style="display: none" Text="Set modal details" ClientIDMode="Static" UseSubmitBehavior="true" />
            <asp:Button ID="but_HiddenProxy" runat="server" Style="display: none" Text="Hidden Proxy Button for Modal Popup" ClientIDMode="Static" />
        </td>
        <td class="rt">
        <asp:Button ID="but_AddTool" runat="server" AccessKey="A" CssClass="butGreen" Text="Add Tool" ToolTip="Add Tool - Alt A" />
        </td>
    </tr>
</table>

<asp:ModalPopupExtender ID="mpx_AddEditAccess" runat="server" CancelControlID="but_Cancel"
    BehaviorID="pn_AddEditAccess" PopupControlID="pn_AddEditAccess" TargetControlID="but_HiddenProxy"
    BackgroundCssClass="modalBackground" />
<asp:Panel ID="pn_AddEditAccess" runat="server" Width="500px" CssClass="modalPopup"
    Style="display: block">
    <div class="box">
        <h2>
            <asp:Label ID="lb_ModTitle" runat="server"></asp:Label>
        </h2>
        <asp:HiddenField ID="hd_AccessID" runat="server" ClientIDMode="Static"></asp:HiddenField>
        <div class="block">
            <asp:UpdatePanel ID="up_Access" runat="server" UpdateMode="Always">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="ddl_ToolName" EventName="SelectedIndexChanged" />
                </Triggers>
                <ContentTemplate>
                    <table>
                        <tr>
                            <th class="p66 rt">
                                Tool Name:&nbsp;
                            </th>
                            <td class="p66">
                                <asp:HiddenField ID="hd_ToolIDMod" runat="server" ClientIDMode="Static" />
                                <asp:DropDownList ID="ddl_ToolName" runat="server" AutoPostBack="true" AppendDataBoundItems="True"
                                    DataSourceID="SqlDS_Tools" DataTextField="ToolName" DataValueField="ToolID" OnSelectedIndexChanged="ddl_ToolName_SIC">
                                    <asp:ListItem Text="Please Select..." Value="0"></asp:ListItem>
                                </asp:DropDownList>
                                <asp:SqlDataSource ID="SqlDS_Tools" runat="server" ConnectionString="<%$ ConnectionStrings:ToolsConnString %>"
                                    SelectCommand="SELECT [ToolID], [ToolName] FROM [tbl_Tools] WHERE ([Redundant] = @Redundant)">
                                    <SelectParameters>
                                        <asp:Parameter DefaultValue="False" Name="Redundant" Type="Boolean" />
                                    </SelectParameters>
                                </asp:SqlDataSource>
                                <asp:RequiredFieldValidator ID="rfv_ddl_ToolName" runat="server" ControlToValidate="ddl_ToolName"
                                    CssClass="error" Display="Dynamic" ErrorMessage="Please Select Tool Name" InitialValue="0">
                                </asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <th class="p66 rt">
                                Access Rights:&nbsp;
                            </th>
                            <td class="p66">
                                <asp:HiddenField ID="hd_AccessCodeMod" runat="server" ClientIDMode="Static" />
                                <asp:DropDownList ID="ddl_AccessCode" runat="server" Enabled="false">
                                    <asp:ListItem Text="No Access" Value="0"></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td class="p66">
                                <asp:Button ID="but_Cancel" runat="server" Text="Cancel" />
                            </td>
                            <td class="p66 rt">
                                <asp:Button ID="but_Save" runat="server" Text="Save" />
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </div>
</asp:Panel>

As you can see I have implemented two hidden buttons but_SetModalDetails and but_HiddenProxy. but_SetModalDetails has some codebehind which sets a couple of dropdown lists (one populated from a datasource, the other is populated dynamically based on the value of the first. The codebehind is...

    Protected Sub but_SetModalDetails_Click(ByVal sender As Object, ByVal e As EventArgs) Handles but_SetModalDetails.Click

    If hd_AccessID.Value = "0" Then
        lb_ModTitle.Text = "Assigning Access Rights to:"
        ddl_ToolName.SelectedIndex = 0
        ddl_AccessCode.SelectedIndex = 0
        ddl_AccessCode.Enabled = False
    Else
        lb_ModTitle.Text = "Edit Access Rights to:"
        ddl_ToolName.SelectedValue = hd_ToolIDMod.Value
        ddl_ToolName.Enabled = False
        SqlStr = "SELECT AccessID AS ddlValue, AccessText as ddlText FROM tbl_AccessCodes WHERE ToolID = " & hd_ToolIDMod.Value
        PopulateDDLvalue(ddl_AccessCode, SqlStr)
        ddl_AccessCode.SelectedValue = hd_AccessCodeMod.Value
        ddl_AccessCode.Enabled = True
    End If

    'NOW I NEED TO SIMULATE but_HiddenProxy Click

End Sub

As you can see at the end I need to simulate a click of but_HiddenProxy so that the modalPopup is shown populated with the correct data.

Any Ideas? Thanks

Mych
  • 2,527
  • 4
  • 36
  • 65

1 Answers1

0

After all that... I was able to do everything in codebehind...

I only needed one hidden button but_HiddenProxy.

In the gridview instead of setting an onClick attribute for each edit image button I just set a commandname of 'AccessEdit' (don't use 'Edit'). I then had a method that handled the gridview.RowCommand event. This found the various info I needed by using findControl on the selected row. These values were then used to populate the dropdowns in the popup and then use the show command to make the popup visible.

One bit that did stump me for a while was why my RowCommand was not triggering when an imagebutton was clicked. I'd forgotten that I had validation in the modal which stopped the RowCommand being executed. I stuck a CausesValidation="false" in the imagebutton and all was OK.

Talk about using a hammer to crack a nut!

Mych
  • 2,527
  • 4
  • 36
  • 65