0

谁能告诉我为什么当我尝试运行此代码时页面一直加载(从不加载)?

Imports System.Data.SqlClient

Partial Class Admin
    Inherits System.Web.UI.Page

    Protected Sub btnSearchUser_Click(sender As Object, e As EventArgs) Handles btnSearchUser.Click

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim searchComm As String = "SELECT * FROM users WHERE username LIKE @username"
        Dim user_id_select As New Integer

        Dim searchSQL As New SqlCommand

        conn.Open()

        searchSQL = New SqlCommand(searchComm, conn)
        searchSQL.Parameters.AddWithValue("@username", txtUserSearch.Text)

        Dim datareader As SqlDataReader = searchSQL.ExecuteReader()

        While datareader.Read

            lstUsers.Items.Add(datareader.Item("username"))

        End While

        datareader.Close()
        conn.Close()


        Dim conn2 As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim selectComm As String = "SELECT user_id FROM users WHERE username=@username_selected"

        Dim selectSQL As New SqlCommand

        conn2.Open()


        selectSQL = New SqlCommand(selectComm, conn2)
        selectSQL.Parameters.AddWithValue("@username_selected", lstUsers.SelectedItem.Text.Trim)

        Dim datareader2 As SqlDataReader = selectSQL.ExecuteReader()

        While datareader2.Read

            If datareader2.HasRows Then

                user_id_select = datareader2("user_id")

                lblUserSelected.Text = "Selected: " + lstUsers.SelectedItem.Text

            ElseIf datareader2.HasRows = False Then

                lblInvalidUsername.Visible = True
                datareader2.Close()


            End If

        End While


        conn2.Close()



    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles drpProjects.SelectedIndexChanged



        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim sqlComm As String = "SELECT project_name FROM projects WHERE project_name=@projectname"
        Dim sqlProjname As New SqlCommand

        conn.Open()

        sqlProjname = New SqlCommand(sqlComm, conn)
        sqlProjname.Parameters.AddWithValue("@projectname", drpProjects.SelectedItem.Text)
        Dim datareader As SqlDataReader = sqlProjname.ExecuteReader()
        datareader.Read()


        If datareader.HasRows Then

            lblProjName.Text = datareader("project_name").ToString()
        End If

        datareader.Close()
        conn.Close()

        lblProjName.Visible = True
        grdProjDetails.Visible = True







    End Sub

    Protected Sub btnManageProj_Click(sender As Object, e As EventArgs) Handles btnManageProj.Click

        MultiView1.SetActiveView(View2)


    End Sub

    Protected Sub btnProjSettings_Click(sender As Object, e As EventArgs) Handles btnProjSettings.Click

        Dim conn2 As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim sqlComm2 As String = "SELECT project_id FROM projects WHERE project_name=@projectname"
        Dim sqlProjID As New SqlCommand

        conn2.Open()

        sqlProjID = New SqlCommand(sqlComm2, conn2)
        sqlProjID.Parameters.AddWithValue("@projectname", drpProjects.SelectedItem.Text)
        Dim datareader As SqlDataReader = sqlProjID.ExecuteReader()
        datareader.Read()


        If datareader.HasRows Then

            Dim UserID As String
            UserID = datareader("project_id").ToString

            Session("project_id") = UserID
            Response.Redirect("Project.aspx")

        End If

        datareader.Close()
        conn2.Close()



    End Sub
End Class

管理员.aspx

    <%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Admin.aspx.vb" Inherits="Admin" %>


<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p style="font-size: xx-large; text-decoration: underline">
    <strong>Manage User</strong>&nbsp;</p>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <p>
                <strong>Search User:</strong>
                <asp:TextBox ID="txtUserSearch" runat="server"></asp:TextBox>
                &nbsp;
                <asp:Button ID="btnSearchUser" runat="server" BorderStyle="None" Text="Search" />
            </p>
            <p>
                <asp:ListBox ID="lstUsers" runat="server" Height="47px" Width="160px"></asp:ListBox>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="lblUserSelected" runat="server" Font-Bold="True"></asp:Label>
            </p>
            <p>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3">
                    <Columns>
                        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                        <asp:BoundField DataField="surname" HeaderText="surname" SortExpression="surname" />
                        <asp:BoundField DataField="occupation" HeaderText="occupation" SortExpression="occupation" />
                        <asp:BoundField DataField="department" HeaderText="department" SortExpression="department" />
                        <asp:BoundField DataField="work_location" HeaderText="work_location" SortExpression="work_location" />
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [name], [surname], [occupation], [department], [work_location] FROM [users] WHERE ([user_id] = @user_id)">
                    <SelectParameters>
                        <asp:SessionParameter Name="user_id" SessionField="UserID" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </p>
            <p>
                <asp:Label ID="lblInvalidUsername" runat="server" Font-Bold="True" ForeColor="#FF3300" Text="Invalid Username selected." Visible="False"></asp:Label>
            </p>
            <p>
                <asp:Button ID="btnViewProfile" runat="server" BorderStyle="None" Text="View Profile" />
            </p>
            <hr />
            <p style="font-size: xx-large; text-decoration: underline">
                <strong>Manage Projects</strong></p>

            <p style="font-size: xx-large; ">
                <asp:Button ID="btnCreateProj" runat="server" BorderStyle="None" Font-Bold="True" Height="38px" Text="CREATE" Width="100px" />
                &nbsp;<asp:Button ID="btnManageProj" runat="server" BorderStyle="None" Font-Bold="True" Height="38px" Text="MANAGE" Width="100px" />
            </p>

            <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
                <asp:View ID="View1" runat="server">
                   <strong> Create a new project</strong><br />
                    </asp:View>
                        <asp:View ID="View2" runat="server">
                        <strong>Manage an existing project</strong><br />
                            Select a project:<asp:DropDownList ID="drpProjects" runat="server" DataSourceID="SqlDataSource1" DataTextField="project_name" DataValueField="project_name" AutoPostBack="True">
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [project_name] FROM [projects]"></asp:SqlDataSource>
                            <br />
                            <br />
                            <asp:Label ID="lblProjName" runat="server" Visible="False" Font-Bold="True" Font-Overline="False" Font-Size="Medium" Font-Underline="False" ForeColor="#006699"></asp:Label>
                            <br />
                            <br />
                            <asp:GridView ID="grdProjDetails" runat="server" AutoGenerateColumns="False" DataKeyNames="project_id" DataSourceID="SqlDataSource2" Visible="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" Font-Names="Verdana" Font-Size="Medium" ForeColor="Black" HorizontalAlign="Center" ShowFooter="True">
                                <Columns>
                                    <asp:TemplateField HeaderText="# ID" InsertVisible="False" SortExpression="project_id">
                                        <EditItemTemplate>
                                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("project_id") %>'></asp:Label>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("project_id") %>'></asp:Label>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="project_type" HeaderText="Type" SortExpression="project_type">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="project_start" HeaderText="Start Date" SortExpression="project_start">
                                    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="project_finish" HeaderText="End Date" SortExpression="project_finish">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="project_duration" HeaderText="Duration" SortExpression="project_duration">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="project_budget" HeaderText="Budget" SortExpression="project_budget">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="project_cost" HeaderText="Cost" SortExpression="project_cost">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="project_manager_id" HeaderText="Project Manager #" SortExpression="project_manager_id">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="team_leader_id" HeaderText="Team Leader #" SortExpression="team_leader_id">
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:BoundField>
                                </Columns>
                                <EditRowStyle BorderStyle="Solid" />
                                <FooterStyle BackColor="#CCCCCC" BorderStyle="None" BorderWidth="0px" />
                                <HeaderStyle BackColor="Black" BorderColor="#003366" BorderStyle="None" BorderWidth="0px" HorizontalAlign="Center" VerticalAlign="Middle" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                                <RowStyle BorderStyle="Solid" BackColor="White" BorderColor="Black" BorderWidth="2px" />
                                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                                <SortedAscendingHeaderStyle BackColor="#808080" />
                                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                                <SortedDescendingHeaderStyle BackColor="#383838" />
                            </asp:GridView>
                            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [project_id], [project_type], [project_start], [project_finish], [project_duration], [project_budget], [project_cost], [project_manager_id], [team_leader_id] FROM [projects] WHERE ([project_name] = @project_name) ORDER BY [project_start]">
                                <SelectParameters>
                                    <asp:ControlParameter ControlID="drpProjects" Name="project_name" PropertyName="SelectedValue" Type="String" />
                                </SelectParameters>
                            </asp:SqlDataSource>
                            &nbsp;<asp:Button ID="btnProjSettings" runat="server" BorderStyle="None" Text="VIEW PROFILE" Width="113px" />
                        </asp:View>
            </asp:MultiView>
            <br />
        </ContentTemplate>
    </asp:UpdatePanel>
<p>

    </p>
</asp:Content>

母版页.aspx

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<script runat="server">



    Protected Sub Page_Load(sender As Object, e As EventArgs)

        If Session("userid") = Nothing Then
            txtLoginUser.Visible = True
            txtLoginPass.Visible = True

        Else


            Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
            Dim useridComm As String = "SELECT name, surname FROM users WHERE user_id=@userid"
            Dim sqlUserID As New SqlCommand

            conn.Open()

            Dim userid As String = Session("userid")

            sqlUserID = New SqlCommand(useridComm, conn)
            sqlUserID.Parameters.AddWithValue("@userid", userid)
            Dim datareader As SqlDataReader = sqlUserID.ExecuteReader()
            If datareader.HasRows Then

                FormsAuthentication.RedirectFromLoginPage(datareader("user_id"), True)
                lblLoggedIn.Text = datareader("name").ToString() & " " & datareader("surname").ToString()

            End If
            datareader.Close()
            conn.Close()

        End If
    End Sub

   Protected Sub Button1_Click(sender As Object, e As EventArgs)

        Dim loginSQL As New SqlCommand
        Dim loginComm As String

        Dim CommonFunctions As New CommonFunctions()
        Dim dec_pass As String = CommonFunctions.EncryptPassword(txtLoginPass.Text.Trim)

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")


        loginComm = "SELECT user_id FROM users WHERE username=@username and password=@password"

        conn.Open()


        loginSQL = New SqlCommand(loginComm, conn)
        loginSQL.Parameters.AddWithValue("@username", txtLoginUser.Text.ToString)
        loginSQL.Parameters.AddWithValue("@password", dec_pass)
        Dim dr As SqlDataReader = loginSQL.ExecuteReader()
        dr.Read()


        If dr.HasRows Then
            Session("userid") = dr("user_id")
        ElseIf dr.HasRows = False Then

            lblRegister.ForeColor = Drawing.Color.Red
            lblRegister.Text = "Incorrect Username/Password."
        End If
        dr.Close()
        conn.Close()

    End Sub





</script>



<head>
    <meta charset="utf-8" />
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <title></title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection" />
</head>

<body>

    <form id="form1" runat="server">

<div id="wrapper">

    <header id="header">
        <strong>Header:</strong> Mobile CMS

        </header>


<section id="login">

    <div id="login-form">

         <p>

                    <asp:Label ID="lblUsername" runat="server" Font-Bold="True" Text="U:"></asp:Label>
&nbsp;<asp:TextBox ID="txtLoginUser" runat="server" BorderStyle="None" BorderWidth="0px" Wrap="False"></asp:TextBox>
&nbsp;
                    <asp:Label ID="lblUsername0" runat="server" Font-Bold="True" Text="P:"></asp:Label>
                    <asp:TextBox ID="txtLoginPass" runat="server" BorderStyle="None" BorderWidth="0px" TextMode="Password" Wrap="False"></asp:TextBox>
&nbsp;
                    <asp:Button ID="btnLogin" runat="server" BorderStyle="None" OnClick="Button1_Click" Text="Login" />

                </p>

                <p>

                    <asp:Label ID="lblRegister" runat="server" Font-Bold="True" Font-Underline="True" ForeColor="#0000CC" Text="Register"></asp:Label>

                    <asp:Label ID="lblLoggedIn" runat="server"></asp:Label>

                </p>
         <p>

                    &nbsp;</p>



    </div>

</section>

<div class="navigation-bar">
       <ul class="navigation-menu">

            <li><a href="#" class="home">Home</a></li>
            <li><a href="#" class="mainsettings">Settings</a></li>
            <li><a href="#" class="profile">Profile</a>

                <ul>
                    <li><a href="#" class="messages">Messages</a></li>
                    <li><a href="#" class="settings">Profile Settings</a></li>
                </ul>

            </li>
            <li><a href="#" class="uploads">Uploads</a></li>
            <li><a href="#" class="documents">Media</a>


                <ul>
                    <li><a href="#" class="docs">Documents</a></li>
                    <li><a href="#" class="others">Others</a></li>
                </ul>

            </li>

            <li><a href="#" class="projects">Projects</a>


                <ul>
                    <li><a href="#" class="yprojects">Your Projects</a></li>
                    <li><a href="#" class="otherprojects">Other Projects</a></li>
                </ul>

            </li>

        </ul>

    </div>


    <section id="middle">

        <div id="container">
            <div id="content">
                <div>
                    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

                    </asp:ContentPlaceHolder>
                </div>
        </div>
        </div>



    </section>

    <footer id="footer">
        <strong>Footer:</strong> adsfdsgfds
    </footer>

</div>

    </form>

</body>
</html>

我在 Win 7 Ultimate 上使用 VB.NET/ASP.NET 和 SQL-Server-2012。

可能是因为我有不止一个数据阅读器吗?

4

1 回答 1

0

您没有在 Page_Load 中执行任何代码,那么为什么您的页面要继续加载呢?Buttons_Click此外,您的数据库调用(关闭和打开连接)过多Dropdowns_SelectedIndexChanged

我怀疑你SelectedIndexChanged的下拉事件。当页面加载为项目添加到下拉列表时,也会调用此事件。这会触发事件,并且每次为下拉列表中的每个项目触发事件时都可能连接到数据库

采取SelectedIndexChanged并尝试再次加载页面

于 2013-04-07T22:33:17.563 回答