从后面的代码执行按钮单击

时间:2013-02-07 13:29:40

标签: asp.net button modalpopupextender

我有一个gridview,其中列出了Tools和Access值。要编辑,每行都有一个编辑图像按钮。我有一个OnRowBound方法,它为每个按钮分配一个OnClick属性,以便我知道我需要编辑哪条记录。 代码是

    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

我正在使用一个隐藏的代理按钮来显示用户将用来编辑记录的模态弹出窗口...(相同的弹出窗口将用于添加新的访问记录......但稍后会出现这种情况) 。所以我已将我的详细信息传递给proxyClick,我将值设置为模态弹出窗口中的控件。 javascript是....

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

作为参考,标记的主要部分是....

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

正如您所看到的,我已经实现了两个隐藏按钮but_SetModalDetails和but_HiddenProxy。 but_SetModalDetails有一些代码隐藏,它设置了几个下拉列表(一个从数据源填充,另一个根据第一个的值动态填充。代码隐藏是......

    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

正如您在最后看到的那样,我需要模拟but_HiddenProxy的单击,以便显示modalPopup,其中填充了正确的数据。

任何想法?感谢

1 个答案:

答案 0 :(得分:0)

毕竟......我能够在代码隐藏中做所有事情......

我只需要一个隐藏按钮but_HiddenProxy。

在gridview中,不是为每个编辑图像按钮设置onClick属性,而是设置命令名为“AccessEdit”(不要使用“编辑”)。然后我有一个处理gridview.RowCommand事件的方法。这通过在所选行上使用findControl找到了我需要的各种信息。然后使用这些值填充弹出窗口中的下拉列表,然后使用show命令使弹出窗口可见。

有一段时间让我感到困惑的是为什么我的RowCommand在单击图像按钮时没有触发。我忘记了我在模态中验证了停止执行RowCommand。我在图像按钮中粘贴了一个CausesValidation =“false”,一切都很好。

谈论用锤子敲打坚果!