如果在asp.net上发现重复,则标签不显示

本文关键字:标签 显示 asp net 发现 如果 | 更新日期: 2023-09-27 18:26:23

从数据库检索值时,我会检查文本框的值是否包含在数组中。数组包含从数据库中检索到的值。如果它确实包含值,那么它应该显示一个标签。这一直有效,直到我在if语句中添加了一个else,之后标签就拒绝显示了。

主页:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:MultiView ID="ManageCourseMultiView" ActiveViewIndex="1" runat="server">
            <asp:View ID="OptionsView" runat="server">
                <asp:LinkButton ID="CourseLinkButton" CommandName="SwitchViewByID" CommandArgument="ManageCourseView" runat="server">Please click here to manage your course.</asp:LinkButton>
                &nbsp;
                <asp:LinkButton ID="CourseContentLinkButton" CommandName="SwitchViewByID" CommandArgument="ManageCourseContentView" runat="server">Please click here to manage your course content.</asp:LinkButton>&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:HyperLink ID="CreateCourseLink" NavigateUrl="~/Instructors/CreateCourse2.aspx" runat="server">Please click here to create a course.</asp:HyperLink>
                <br />
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT * FROM [Course] WHERE ([CourseId] = @CourseId)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="GridView1" Name="CourseId" PropertyName="SelectedValue" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </asp:View>
            <asp:View ID="ManageCourseView" runat="server">
                <asp:Label ID="UserIdLabel" Visible="true" runat="server" Text="Label"></asp:Label>
                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False" DataKeyNames="CourseId" DataSourceID="GridViewSource" Width="1072px">
                    <Columns>
                        <asp:CommandField ShowSelectButton="True" />
                        <asp:BoundField DataField="CourseId" HeaderText="CourseId" InsertVisible="False" ReadOnly="True" SortExpression="CourseId" />
                        <asp:BoundField DataField="CourseName" HeaderText="CourseName" SortExpression="CourseName" />
                        <asp:BoundField DataField="ForRole" HeaderText="ForRole" SortExpression="ForRole" />
                        <asp:CheckBoxField DataField="Certification" HeaderText="Certification" SortExpression="Certification" />
                        <asp:BoundField DataField="UserId" Visible="false" HeaderText="UserId" SortExpression="UserId" />
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="GridViewSource" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT Course.* FROM Course WHERE (UserId = @UserId)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="UserIdLabel" Name="UserId" PropertyName="Text" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <br />
                <table>
                    <tr>
                        <td>
                            <asp:Label ID="CourseEditName" runat="server" Text="Course Name:"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="CourseEditNameTextBox" runat="server"></asp:TextBox>&nbsp;
                            <asp:Label ID="nameerror" runat="server" ForeColor="Red" Visible="false" Text="This Coursename already exists, please enter another one."></asp:Label>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic" ForeColor="Red" ControlToValidate="CourseEditNameTextBox" ErrorMessage="Course Name cannot be empty."></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="CourseEditForRoles" runat="server" Text="For Role: "></asp:Label>
                        </td>
                        <td>
                            <asp:DropDownList ID="CourseEditForRole" runat="server">
                                <asp:ListItem Text="Parent" Value="Parent"></asp:ListItem>
                                <asp:ListItem Text="Staff" Value="Staff"></asp:ListItem>
                                <asp:ListItem Text="Health Care PRofessional" Value="HealthPro"></asp:ListItem>
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="ForRoleUpdate" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT RoleName FROM Roles WHERE (RoleName &lt;&gt; 'BackOffice') AND (RoleName &lt;&gt; 'Instructor')"></asp:SqlDataSource>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="CourseEditCertification" runat="server" Text="Certification: "></asp:Label>
                        </td>
                        <td>
                            <asp:CheckBox ID="CourseEditCertificationCheckBox" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label9" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label10" runat="server" Text="Label"></asp:Label>
                            <asp:Label ID="Label11" runat="server" Text="Label"></asp:Label>
                        </td>
                        <td>
                            <asp:Button ID="CourseEditUpdate" OnClick="CourseEditUpdate_Click" runat="server" Text="Finish Editing" />
                            <asp:Button ID="CourseEditCancel" runat="server" CommandName="Cancel" CausesValidation="false" Text="Cancel" />
                        </td>
                    </tr>
                </table>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" DeleteCommand="DELETE FROM [Course] WHERE [CourseId] = @CourseId" InsertCommand="INSERT INTO [Course] ([CourseName], [ForRole], [Certification], [UserId]) VALUES (@CourseName, @ForRole, @Certification, @UserId)" SelectCommand="SELECT * FROM [Course]" UpdateCommand="UPDATE [Course] SET [CourseName] = @CourseName, [ForRole] = @ForRole, [Certification] = @Certification, [UserId] = @UserId WHERE [CourseId] = @CourseId">
                    <DeleteParameters>
                        <asp:Parameter Name="CourseId" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="CourseName" Type="String" />
                        <asp:Parameter Name="ForRole" Type="String" />
                        <asp:Parameter Name="Certification" Type="Boolean" />
                        <asp:Parameter Name="UserId" Type="Object" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="CourseName" Type="String" />
                        <asp:Parameter Name="ForRole" Type="String" />
                        <asp:Parameter Name="Certification" Type="Boolean" />
                        <asp:Parameter Name="UserId" Type="Object" />
                        <asp:Parameter Name="CourseId" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
            </asp:View>
            <asp:View ID="ManageCourseContentView" runat="server">
                <h2>Test</h2>
            </asp:View>
        </asp:MultiView>
    </ContentTemplate>
</asp:UpdatePanel>

CodeBhind:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    Boolean checkboxvalue;
    string cert;
    using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
        connection.Open();
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT * FROM Course WHERE CourseId = '" + GridView1.SelectedValue.ToString() + "'";
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    CourseEditForRole.SelectedValue = reader["ForRole"].ToString();
                    CourseEditNameTextBox.Text = reader["CourseName"].ToString();
                    checkboxvalue = Convert.ToBoolean(reader["Certification"]);
                    cert = reader["Certification"].ToString();
                    if (cert == "False")
                    {
                        CourseEditCertificationCheckBox.Checked = false;
                    }
                    else
                    {
                        CourseEditCertificationCheckBox.Checked = true;
                    }
                }
            }
        }
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT CourseName FROM Course WHERE (CourseId = '" + GridView1.SelectedValue.ToString() + "')";
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    coursenamevalue = reader["CourseName"].ToString();
                }
            }
        }
        using (var command2 = connection.CreateCommand())
        {
            command2.CommandText = "SELECT CourseName FROM [Course] WHERE (CourseName <> '" + coursenamevalue + "')";
            using (var reader = command2.ExecuteReader())
            {
                int i = 0;
                while (reader.Read())
                {
                    coursename[i] = reader["CourseName"].ToString();
                    i++;
                }
                counter = i;
            }
        }
    }
}
 protected void CourseEditUpdate_Click(object sender, EventArgs e)
{
    Boolean checkboxvalue;
    string cert;
    using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
        connection.Open();
        using (var command2 = connection.CreateCommand())
        {
            command2.CommandText = "SELECT CourseName FROM [Course] WHERE (CourseName <> '" + coursenamevalue + "')";
            using (var reader = command2.ExecuteReader())
            {
                int i = 0;
                while (reader.Read())
                {
                    coursename[i] = reader["CourseName"].ToString();
                    if (CourseEditNameTextBox.Text.Equals(reader["CourseName"].ToString(), StringComparison.Ordinal))
                    {
                        //cameerror.Visible = true;
                    }
                    i++;
                }
                counter = i;
            }
        }
    }
    int k = 0;
    while(!String.IsNullOrEmpty(coursename[k]))
    {
        if (coursename[k].Contains(CourseEditNameTextBox.Text))
        {
            nameerror.Visible = true;
        }
        else
        {
            nameerror.Visible = false;
        }
        k++;
    }
}

如果在asp.net上发现重复,则标签不显示

问题是你继续while循环,如果它存在,你需要打破循环,因为数组中的最后一项不同,那么标签的文本框内容就会消失,你必须这样做:

int k = 0;
while(!String.IsNullOrEmpty(coursename[k]))
{
    if (coursename[k].Contains(CourseEditNameTextBox.Text))
    {
        nameerror.Visible = true;
        Break;
    }
    else
    {
        nameerror.Visible = false;
    }
    k++;
}