0

我有一个列出工具和访问值的网格视图。要进行编辑,我在每一行都有一个编辑图像按钮。我有一个 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。但是_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 填充了正确的数据。

有任何想法吗?谢谢

4

1 回答 1

0

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

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

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

让我难过一阵子的一点是,为什么单击图像按钮时我的 RowCommand 没有触发。我忘记了我在阻止执行 RowCommand 的模式中进行了验证。我在图像按钮中粘贴了一个 CausesValidation="false" ,一切正常。

谈论用锤子敲碎坚果!

于 2013-02-07T16:32:27.450 回答