【问题标题】:In Datalist button inside Modalpopupextender not firing在 Modalpopupextender 内的 Datalist 按钮中未触发
【发布时间】:2019-04-20 08:21:39
【问题描述】:

我有一个数据列表,下面有一个打开 Modalpopupextender 的按钮。此 modalpopupextender 中有一个未触发的提交按钮。请指教。

   <table style="width: 100%">
        <tr>
            <td colspan="2">
                <asp:DataList ID="dl1" runat="server" CellPadding="20" RepeatDirection="Vertical"
                    Style="width: 100%;" OnItemCommand="dl1_ItemCommand" OnSelectedIndexChanged="dl1_SelectedIndexChanged">
                    <ItemTemplate>
                        <table style="background-color: White; padding: 10px; border-color: Gray; width: 100%;">
                            <tr>
                                <td style="width: 30%; text-align: center;">
                                    <asp:Image ID="imgPhoto" runat="server" ImageUrl='  
                                    <%# "~/CoachPhoto/" + Eval("CoachId") + ".jpg" %>' Style="margin-right: 20px; margin-left: 50px;"
                                        Height="150px" Width="150px" BorderColor="Black" BorderStyle="Solid" />
                                </td>
                                <td style="width: 40%;">
                                    <table style="width: 100%;">
                                        <tr>
                                            <td>
                                                <asp:Label ID="lblName" runat="server" Font-Size="Large" ForeColor="DarkBlue" Text='<%# String.Format("{0} {1} {2} {3}", Eval("FirstName"), Eval("LastName"), ", ", Eval("AcademyName")) %>'>
                                                </asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:Label ID="lblLocation" Font-Size="Large" ForeColor="DarkBlue" runat="server"
                                                    Text='<%# Eval("Location") + ", " + Eval("City") %>'>  
                                                </asp:Label>
                                            </td>
                                        </tr>
                                        <%--<tr>
                                            <td>
                                                <asp:Label ID="lblReview" ForeColor="Black" runat="server" Text="Review">  
                                                </asp:Label>
                                            </td>
                                        </tr>--%>
                                        <tr>
                                            <td>
                                                <table style="width: 100%;">
                                                    <tr>
                                                        <td style="width: 50%;">
                                                            <div style="font-size: small;">
                                                                Sport
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <div style="font-size: small;">
                                                                Starting at</div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <asp:Label ID="lblSport" runat="server" Font-Size="Medium" ForeColor="Black" Text='<%# Eval("Sport") %>'>
                                                            </asp:Label>
                                                        </td>
                                                        <td>
                                                            <asp:Label ID="lblFees" runat="server" Font-Size="Medium" ForeColor="Black" Text='<%# String.Format("{0} {1} {2} {3}", "Rs", Eval("Fees"), "per", Eval("FeesType")) %>'>
                                                            </asp:Label>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <table style="width: 100%;">
                                                    <tr>
                                                        <td style="width: 50%;">
                                                            <div style="font-size: small;">
                                                                Contact Number
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <div style="font-size: small;">
                                                                Location</div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <asp:HyperLink ID="hypTelephoneNumber" runat="server" NavigateUrl='<%# "tel:" + Eval("Mobile")%>'
                                                                Font-Size="Medium" ForeColor="Black" Text='<%# Eval("Mobile")  %>' />
                                                        </td>
                                                        <td>
                                                            <asp:Label ID="lblCompleteLocation" runat="server" Font-Size="Medium" ForeColor="Black"
                                                                Text='<%# Eval("Address") + ", " + Eval("Location") %>'>
                                                            </asp:Label>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:Label ID="lblProfile" Font-Size="Medium" ForeColor="Black" runat="server" Text='<%# Eval("CoachProfile") %>'>  
                                                </asp:Label>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                                <td>
                                    <%--<asp:Button ID="lnkViewProfile" runat="server" Text="View Profile" PostBackUrl='<%# String.Format("{0} {1}", "~/CoachProfile.aspx?Id=", Eval("CoachId")) %>'>
                                    </asp:Button><br />
                                    <br />--%>
                                    <asp:Button ID="btnMessageCoach" runat="server" Text="Message Coach" Style="border-radius: 5px;
                                        height: 30px; background: #32BC29; width: 180px; font-size: Medium; color: Black;">
                                    </asp:Button>
                                    <cc1:ModalPopupExtender ID="mdMessage" BehaviorID="behaviorIDmp3" runat="server"
                                        PopupControlID="Panel3" TargetControlID="btnMessageCoach" CancelControlID="lnkCancelMessage"
                                        DropShadow="true" BackgroundCssClass="modalBackground">
                                    </cc1:ModalPopupExtender>
                                    <br />
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:DataList>
                <asp:Panel ID="Panel3" runat="server" CssClass="modalPopup" align="center" Style="display: none;
                    height: 450px; width: 650px;" DefaultButton="btnSendMessage">
                    <asp:TextBox ID="txtMessage" TextMode="MultiLine" Height="200px" runat="server" class="txtFirstName"
                        Width="500px" name="email" Font-Size="Large" Style="margin-bottom: 30px; margin-top: 20px;"
                        ValidationGroup="Message" PlaceHolder="Message" />
                    <br />
                    <asp:RequiredFieldValidator EnableClientScript="true" ID="reqtxtMessage" runat="server"
                        ValidationGroup="Message" ForeColor="Red" ControlToValidate="txtMessage" ErrorMessage="Message Empty"
                        Display="Dynamic" />
                    <br />
                    <asp:TextBox ID="txtContactNumber" runat="server" class="txtFirstName" Width="500px"
                        name="contactnumber" value="" Height="40px" Font-Size="Medium" PlaceHolder="Contact Number" />
                    <asp:RequiredFieldValidator EnableClientScript="true" ID="reqtxtContactNumber" runat="server"
                        ValidationGroup="Message" ForeColor="Red" ControlToValidate="txtContactNumber"
                        ErrorMessage="Contact Number Empty" Display="Dynamic" />
                    <br />
                    <asp:RegularExpressionValidator ID="regtxtContactNumber" runat="server" ControlToValidate="txtContactNumber"
                        ValidationGroup="Message" ErrorMessage="RegularExpressionValidator" ForeColor="Red"
                        ValidationExpression="[0-9]{10}" Text="Enter 10 digit mobile number">
                    </asp:RegularExpressionValidator>
                    <div>
                        <asp:Button ID="btnSendMessage" ValidationGroup="Message" runat="server" Text="Send"
                            Style="border-radius: 5px; height: 35px; background: #32BC29; width: 400px; font-size: Medium;
                            color: Black; margin-top: 30px; color: Black;" OnClick="btnSendMessage_Click" /></div>
                    <br />
                    <asp:HyperLink ID="lnkCancelMessage" runat="server" Text="Cancel" CssClass="btnClosePopup"
                        Style="margin-top: 10px;">Close</asp:HyperLink>
                </asp:Panel>
            </td>
        </tr>
    </table>
上面是一个数据列表,其中有一个 modalpopupextender,当单击按钮时会打开 "btnMessageCoach" 。现在,当弹出窗口打开时,会有一个消息文本框和发送按钮。但是这个发送按钮不会在点击时触发。请指教。

【问题讨论】:

  • 我注意到 Panel3 弹出窗口位于 ItemTemplate 内部,这意味着将为多个数据源记录呈现多个面板。我建议您将 &lt;asp:Panel ID="Panel3" 控件移出 DataList ItemTemplate。
  • 谢谢,这是可行的,但我将如何在 button_click 事件中获取数据列表的键
  • 您能分享一下您尝试获取密钥的按钮事件处理程序代码吗?
  • 按照您的建议,我从 Datalist 中删除了面板并将其保留在外部,并为其添加了一个 onclick 事件。我想在下面的事件中获取数据列表项键。 protected void btnSendMessage_Click(object sender, EventArgs e) { Response.Redirect("default.aspx?value = "); }
  • 不确定。请分享最新的aspx和服务器端点击事件处理代码

标签: asp.net datalist modalpopupextender


【解决方案1】:

您需要对您的代码进行以下更新:

&lt;asp:Panel ID="Panel3" runat="server"移到DataList之外,这样它就不会被多次渲染。

要在服务器端获取点击项的coachId,请执行以下操作:

在DataList外添加&lt;asp:HiddenField

<asp:HiddenField ID="hidSelectedID" runat="server" />

在 MessageCoach 按钮上添加 OnClientClick 处理程序以在隐藏字段中设置 id:

    <asp:Button ID="btnMessageCoach" runat="server" 
OnClientClick="javascript:document.getElementById('<%=hidSelectedID.ClientId%>').value='<%#Eval("CoachId")>';" Text="Message Coach" Style="border-radius: 5px; height: 30px; background: #32BC29; width: 180px; font-size: Medium; color: Black;"></asp:Button>

在 btnSendMessage_Click 句柄中,您可以使用 hidSelectedID.Value 访问隐藏字段值

【讨论】:

    猜你喜欢
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    相关资源
    最近更新 更多