【问题标题】:ImageButton not working on Tab Update PanelImageButton 在选项卡更新面板上不起作用
【发布时间】:2017-10-09 06:23:26
【问题描述】:

我目前正在处理一个带有标签的网页。我在此代码中实现更新面板的目的是防止当我按下任何按钮时,我当前所在的选项卡跳回第一个选项卡。我在每个选项卡中有 2 个图像按钮,即添加和保存。实现更新面板后,“添加”按钮正在工作,因为它能够向网格视图添加新行并保持在同一选项卡上,但“保存”按钮不起作用,单击后没有任何效果。我目前是 ASP.NET C# 的新手。希望从 StackOverFlow 获得帮助,帮助我看穿代码并指出我的错误。谢谢大家。

<html>    
<head runat="server">
        <title>ERO</title>
    </head>
    <body>
        <div class="tab">
            <button class="tabName" onclick="openTab(event, 'Student')" id="defaultOpen">Student</button>
            <button class="tabName" onclick="openTab(event, 'Lecturer')">Lecturer</button>
            <button class="tabName" onclick="openTab(event, 'Subject')">Subject</button>
        </div>
        <form id="form1" runat="server">
            <h1>ERO</h1>
            <div id="Student" class="tabContent">
                <h1>Student Registration</h1>
                <div>
                    <asp:GridView ID="StudentGridView" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBoundStudent" ShowFooter="true" OnRowDeleting="StudentGridView_RowDeleting">
                        <Columns>
                            <asp:BoundField DataField="RowNumber" HeaderText="#" />
                            <asp:TemplateField HeaderText="Student Name" ItemStyle-Width="100">
                                <ItemTemplate>
                                    <asp:TextBox ID="StudentName" runat="server" Text='<%# Eval("StudentName") %>' />
                                    <asp:RequiredFieldValidator ID="StudentNameValidator" runat="server" ControlToValidate="StudentName" ErrorMessage="*" ValidationGroup="StudentGroup" />
                                    <asp:RegularExpressionValidator ID="StudentNameValidator1" runat="server" ControlToValidate="StudentName" ErrorMessage="Cannot contain '." ValidationGroup="StudentGroup" ValidationExpression="[^']*" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Student ID" ItemStyle-Width="100">
                                <ItemTemplate>
                                    <asp:TextBox ID="StudentID" runat="server" Text='<%# Eval("StudentID") %>' />
                                    <asp:RequiredFieldValidator ID="StudentIDValidator" runat="server" ControlToValidate="StudentID" ErrorMessage="*" ValidationGroup="StudentGroup" />
                                    <asp:RegularExpressionValidator ID="StudentIDValidator1" runat="server" ControlToValidate="StudentID" ErrorMessage="Cannot contain '." ValidationGroup="StudentGroup" ValidationExpression="[^']*" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Student Address" ItemStyle-Width="200">
                                <ItemTemplate>
                                    <asp:TextBox ID="StudentAddress" runat="server" Text='<%# Eval("StudentAddress") %>' Width="200" Height="50px" TextMode="MultiLine" />
                                    <asp:RequiredFieldValidator ID="StudentAddressValidator" runat="server" ControlToValidate="StudentAddress" ErrorMessage="*" ValidationGroup="StudentGroup" />
                                    <asp:RegularExpressionValidator ID="StudentAddressValidator1" runat="server" ControlToValidate="StudentAddress" ErrorMessage="Cannot contain '." ValidationGroup="StudentGroup" ValidationExpression="[^']*" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Student Phone Number" ItemStyle-Width="100">
                                <ItemTemplate>
                                    <asp:TextBox ID="StudentPNumber" runat="server" Text='<%# Eval("StudentPNumber") %>' />
                                    <asp:RequiredFieldValidator ID="StudentPNumberValidator" runat="server" ControlToValidate="StudentPNumber" ErrorMessage="*" ValidationGroup="StudentGroup" />
                                    <asp:RegularExpressionValidator ID="StudentPNumberValidator1" runat="server" ControlToValidate="StudentPNumber" ErrorMessage="Cannot contain '." ValidationGroup="StudentGroup" ValidationExpression="[^']*" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Student Email Address" ItemStyle-Width="100">
                                <ItemTemplate>
                                    <asp:TextBox ID="StudentEAddress" runat="server" Text='<%# Eval("StudentEAddress") %>' />
                                    <asp:RequiredFieldValidator ID="StudentEAddressValidator" runat="server" ControlToValidate="StudentEAddress" ErrorMessage="*" ValidationGroup="StudentGroup" />
                                    <asp:RegularExpressionValidator ID="StudentEAddressValidator1" runat="server" ControlToValidate="StudentEAddress" ErrorMessage="Cannot contain '." ValidationGroup="StudentGroup" ValidationExpression="[^']*" />
                                    <asp:RegularExpressionValidator ID="StudentEAddressValidator2" runat="server" ControlToValidate="StudentEAddress" ErrorMessage="Email format incorrect." ValidationGroup="StudentGroup" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Student Course" ItemStyle-Width="200">
                                <ItemTemplate>
                                    <asp:DropDownList ID="StudentCourse" runat="server" Width="200" />
                                    <asp:RequiredFieldValidator ID="StudentCourseValidator" runat="server" ControlToValidate="StudentCourse" ErrorMessage="*" InitialValue="-- Select Course --" ValidationGroup="StudentGroup" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Student Password" ItemStyle-Width="100">
                                <ItemTemplate>
                                    <asp:TextBox ID="StudentPassword" runat="server" Text='<%# Eval("StudentPassword") %>' />
                                    <asp:RequiredFieldValidator ID="StudentPasswordValidator" runat="server" ControlToValidate="StudentPassword" ErrorMessage="*" ValidationGroup="StudentGroup" />
                                    <asp:RegularExpressionValidator ID="StudentPasswordValidator1" runat="server" ControlToValidate="StudentPassword" ErrorMessage="Cannot contain '." ValidationGroup="StudentGroup" ValidationExpression="[^']*" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowDeleteButton="True" ValidationGroup="StudentGroup" />
                        </Columns>
                    </asp:GridView>
                    <asp:ImageButton ID="ButtonSaveNewStudent" runat="server" OnClick="ButtonSaveNewStudent_Click" ValidationGroup="StudentGroup" ImageUrl="Picture\Save.jpg" ImageAlign="Right" Width="50px" Height="50px" />
                    <asp:ImageButton ID="ButtonAddNewStudent" runat="server" OnClick="ButtonAddNewStudent_Click" ValidationGroup="StudentGroup" ImageUrl="Picture\Student_Add.jpg" ImageAlign="Right" Width="50px" Height="50px" />
                </div>
            </div>

            <asp:ScriptManager ID="TabScriptManager" runat="server" />
            <asp:UpdatePanel ID="LecturerUpdatePanel" UpdateMode="Conditional" runat="server">
                <ContentTemplate>
                    <div id="Lecturer" class="tabContent">
                        <h1>Lecturer Registration
                        </h1>
                        <div>
                            <asp:GridView ID="LecturerGridView" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBoundLecturer" ShowFooter="true" OnRowDeleting="LecturerGridView_RowDeleting">
                                <Columns>
                                    <asp:BoundField DataField="RowNumber" HeaderText="#" />
                                    <asp:TemplateField HeaderText="Lecturer Name" ItemStyle-Width="100">
                                        <ItemTemplate>
                                            <asp:TextBox ID="LecturerName" runat="server" Text='<%# Eval("LecturerName") %>' />
                                            <asp:RequiredFieldValidator ID="LecturerNameValidator" runat="server" ControlToValidate="LecturerName" ErrorMessage="*" ValidationGroup="LecturerGroup" />
                                            <asp:RegularExpressionValidator ID="LecturerNameValidator1" runat="server" ControlToValidate="LecturerName" ErrorMessage="Cannot contain '." ValidationGroup="LecturerGroup" ValidationExpression="[^']*" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Lecturer ID" ItemStyle-Width="100">
                                        <ItemTemplate>
                                            <asp:TextBox ID="LecturerID" runat="server" Text='<%# Eval("LecturerID") %>' />
                                            <asp:RequiredFieldValidator ID="LecturerIDValidator" runat="server" ControlToValidate="LecturerID" ErrorMessage="*" ValidationGroup="LecturerGroup" />
                                            <asp:RegularExpressionValidator ID="LecturerIDValidator1" runat="server" ControlToValidate="LecturerID" ErrorMessage="Cannot contain '." ValidationGroup="LecturerGroup" ValidationExpression="[^']*" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Lecturer Address" ItemStyle-Width="200">
                                        <ItemTemplate>
                                            <asp:TextBox ID="LecturerAddress" runat="server" Text='<%# Eval("LecturerAddress") %>' Width="200" Height="50px" TextMode="MultiLine" />
                                            <asp:RequiredFieldValidator ID="LecturerAddressValidator" runat="server" ControlToValidate="LecturerAddress" ErrorMessage="*" ValidationGroup="LecturerGroup" />
                                            <asp:RegularExpressionValidator ID="LecturerAddressValidator1" runat="server" ControlToValidate="LecturerAddress" ErrorMessage="Cannot contain '." ValidationGroup="LecturerGroup" ValidationExpression="[^']*" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Lecturer Phone Number" ItemStyle-Width="100">
                                        <ItemTemplate>
                                            <asp:TextBox ID="LecturerPNumber" runat="server" Text='<%# Eval("LecturerPNumber") %>' />
                                            <asp:RequiredFieldValidator ID="LecturerPNumberValidator" runat="server" ControlToValidate="LecturerPNumber" ErrorMessage="*" ValidationGroup="LecturerGroup" />
                                            <asp:RegularExpressionValidator ID="LecturerPNumberValidator1" runat="server" ControlToValidate="LecturerPNumber" ErrorMessage="Cannot contain '." ValidationGroup="LecturerGroup" ValidationExpression="[^']*" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Lecturer Email Address" ItemStyle-Width="100">
                                        <ItemTemplate>
                                            <asp:TextBox ID="LecturerEAddress" runat="server" Text='<%# Eval("LecturerEAddress") %>' />
                                            <asp:RequiredFieldValidator ID="LecturerEAddressValidator" runat="server" ControlToValidate="LecturerEAddress" ErrorMessage="*" ValidationGroup="LecturerGroup" />
                                            <asp:RegularExpressionValidator ID="LecturerEAddressValidator1" runat="server" ControlToValidate="LecturerEAddress" ErrorMessage="Cannot contain '." ValidationGroup="LecturerGroup" ValidationExpression="[^']*" />
                                            <asp:RegularExpressionValidator ID="LecturerEAddressValidator2" runat="server" ControlToValidate="LecturerEAddress" ErrorMessage="Email format incorrect." ValidationGroup="LecturerGroup" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Lecturer Course" ItemStyle-Width="200">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="LecturerCourse" runat="server" Width="200" />
                                            <asp:RequiredFieldValidator ID="LecturerCourseValidator" runat="server" ControlToValidate="LecturerCourse" ErrorMessage="*" InitialValue="-- Select Course --" ValidationGroup="LecturerGroup" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Lecturer Password" ItemStyle-Width="100">
                                        <ItemTemplate>
                                            <asp:TextBox ID="LecturerPassword" runat="server" Text='<%# Eval("LecturerPassword") %>' />
                                            <asp:RequiredFieldValidator ID="LecturerPasswordValidator" runat="server" ControlToValidate="LecturerPassword" ErrorMessage="*" ValidationGroup="LecturerGroup" />
                                            <asp:RegularExpressionValidator ID="LecturerPasswordValidator1" runat="server" ControlToValidate="LecturerPassword" ErrorMessage="Cannot contain '." ValidationGroup="LecturerGroup" ValidationExpression="[^']*" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:CommandField ShowDeleteButton="True" ValidationGroup="LecturerGroup" />
                                </Columns>
                            </asp:GridView>
                            <asp:ImageButton ID="ButtonSaveNewLecturer" runat="server" OnClick="ButtonSaveNewLecturer_Click" ValidationGroup="LecturerGroup" ImageUrl="~/Picture/Save.jpg" ImageAlign="Right" Width="50px" Height="50px" />
                            <asp:ImageButton ID="ButtonAddNewLecturer" runat="server" OnClick="ButtonAddNewLecturer_Click" ValidationGroup="LecturerGroup" ImageUrl="~/Picture/Lecturer_Add.jpg" ImageAlign="Right" Width="50px" Height="50px" />
                        </div>
                    </div>
                </ContentTemplate>
            </asp:UpdatePanel>

            <asp:UpdatePanel ID="SubjectUpdatePanel" UpdateMode="Conditional" runat="server">
                <ContentTemplate>
                    <div id="Subject" class="tabContent">
                        <h1>Subject Registration</h1>
                        <div>
                            <asp:GridView ID="SubjectGridView" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBoundSubject" ShowFooter="true" OnRowDeleting="SubjectGridView_RowDeleting">
                                <Columns>
                                    <asp:BoundField DataField="RowNumber" HeaderText="#" />
                                    <asp:TemplateField HeaderText="Subject Name" ItemStyle-Width="120">
                                        <ItemTemplate>
                                            <asp:TextBox ID="SubjectName" runat="server" Text='<%# Eval("SubjectName") %>' />
                                            <asp:RequiredFieldValidator ID="SubjectNameValidator" runat="server" ControlToValidate="SubjectName" ErrorMessage="*" ValidationGroup="SubjectGroup" />
                                            <asp:RegularExpressionValidator ID="SubjectNameValidator1" runat="server" ControlToValidate="SubjectName" ErrorMessage="Cannot contain '." ValidationGroup="SubjectGroup" ValidationExpression="[^']*" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Subject ID" ItemStyle-Width="120">
                                        <ItemTemplate>
                                            <asp:TextBox ID="SubjectID" runat="server" Text='<%# Eval("SubjectID") %>' />
                                            <asp:RequiredFieldValidator ID="SubjectIDValidator" runat="server" ControlToValidate="SubjectID" ErrorMessage="*" ValidationGroup="SubjectGroup" />
                                            <asp:RegularExpressionValidator ID="SubjectIDValidator1" runat="server" ControlToValidate="SubjectID" ErrorMessage="Cannot contain '." ValidationGroup="SubjectGroup" ValidationExpression="[^']*" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Subject Credit Hour" ItemStyle-Width="120">
                                        <ItemTemplate>
                                            <asp:TextBox ID="SubjectCreditHour" runat="server" Text='<%# Eval("SubjectCreditHour") %>' />
                                            <asp:RequiredFieldValidator ID="SubjectCreditHourValidator" runat="server" ControlToValidate="SubjectCreditHour" ErrorMessage="*" ValidationGroup="SubjectGroup" />
                                            <asp:CompareValidator ID="SubjectCreditHourValidator1" runat="server" ControlToValidate="SubjectCreditHour" ErrorMessage="Must be number." Operator="DataTypeCheck" Type="Integer" ValidationGroup="SubjectGroup" />
                                            <asp:RegularExpressionValidator ID="SubjectCreditHourValidator2" runat="server" ControlToValidate="SubjectCreditHour" ErrorMessage="Cannot contain '." ValidationGroup="SubjectGroup" ValidationExpression="[^']*" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Subject Course" ItemStyle-Width="500">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="SubjectCourse" runat="server" Width="500" />
                                            <asp:RequiredFieldValidator ID="SubjectCourseValidator" runat="server" ControlToValidate="SubjectCourse" ErrorMessage="*" InitialValue="-- Select Course --" ValidationGroup="SubjectGroup" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:CommandField ShowDeleteButton="True" ValidationGroup="SubjectGroup" />
                                </Columns>
                            </asp:GridView>
                            <div align="center" style="width: 1960px">
                                <asp:ImageButton ID="ButtonAddNewSubject" runat="server" OnClick="ButtonAddNewSubject_Click" ValidationGroup="SubjectGroup" ImageUrl="Picture\Add.ico" Style="position: relative; top: 2px" Width="50px" Height="50px" />
                                <asp:ImageButton ID="ButtonSaveNewSubject" runat="server" OnClick="ButtonSaveNewSubject_Click" ValidationGroup="SubjectGroup" ImageUrl="Picture\Save.jpg" Style="position: relative; top: 2px" Width="50px" Height="50px" />
                            </div>
                        </div>
                    </div>
                </ContentTemplate>
            </asp:UpdatePanel>

            <script>
                function openTab(evt, Name) {
                    var i, tabName, tabContent;
                    tabName = document.getElementsByClassName("tabName");
                    for (i = 0; i < tabName.length; i++) {
                        tabName[i].className = tabName[i].className.replace("active", "");
                    }

                    tabContent = document.getElementsByClassName("tabContent");
                    for (i = 0; i < tabContent.length; i++) {
                        tabContent[i].style.display = "none";
                    }

                    document.getElementById(Name).style.display = "block";
                    evt.currentTarget.className += "active";
                }

                document.getElementById("defaultOpen").click();
            </script>
        </form>
    </body>
    </html>

【问题讨论】:

    标签: javascript c# html asp.net tabs


    【解决方案1】:

    从您的 UpdatePanel 中删除此 UpdateMode="Conditional" 属性并尝试。

    <asp:UpdatePanel ID="UpdatePanel" runat="server">
    

    如果您使用的是 UpdateMode="Conditional",那么您必须手动触发 UpdatePanel 的 &lt;Trigger&gt; 标记中的按钮点击。

    </ContentTemplate>
    <Triggers>   
        <asp:AsyncPostBackTrigger ControlID="ButtonSave" EventName="Click" />
     </Triggers>
    

    【讨论】:

    • 嗨。 @Asif.Ali 谢谢你的回复。删除UpdateMode="Conditional" 后,当我按下添加按钮时,它将显示来自其他选项卡的内容以及当前选项卡的内容,并且保存按钮仍然无法工作。我也尝试了&lt;asp:AsyncPostBackTrigger ControlID="ButtonSaveNewLecturer" EventName="Click" /&gt; 的触发模式,但它也不起作用。
    • 嗨。 @Asif.Ali 感谢您的回复。手动触发器是否与我在之前的评论中提到的相同?如果相同,则不工作。
    • 如果您使用的是 UpdateMode="Conditional" 那么您必须手动使用触发器。
    • 嗨。 @Asif.Ali 感谢您的回复。我尝试了两种方式,但它们都不起作用。但是谢谢你的回答。欣赏它。
    猜你喜欢
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 2010-12-24
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多