ImageButton无法使用选项卡更新面板

时间:2017-10-09 06:23:27

标签: javascript c# html asp.net tabs

我目前正在制作一个包含标签的网页。我在此代码中实现更新面板的目的是为了防止当我按下任何按钮时当前的选项卡跳回到第一个选项卡。我在每个选项卡中都有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>

1 个答案:

答案 0 :(得分:0)

从UpdatePanels中删除此UpdateMode="Conditional"属性,然后尝试。

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

如果您正在使用UpdateMode="Conditional",那么您必须手动触发UpdatePanels的<Trigger>标记中的按钮点击。

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