2013-04-07 102 views
0

任何人都可以告訴我爲什麼當我試圖運行這個代碼時,頁面不斷加載(從不加載)?ASP.NET VB.NET - 頁面不加載

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 [email protected]_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 [email protected]" 
     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 [email protected]" 
     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 

Admin.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> 

MasterPage.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 [email protected]" 
      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 [email protected] and [email protected]" 

     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旗艦版使用VB.NET/ASP.NET與SQL-Server的2012。

這可能是因爲我有多個數據讀取器?

+0

您能夠調試在Visual Studio中的頁面可能會連接到數據庫(或選擇的IDE)? – 2013-04-07 22:28:26

+0

你可以加載一個空白頁面嗎?即在代碼隱藏中沒有控制或邏輯的頁面。 – Patrick 2013-04-07 22:29:02

+0

@Patrick是的,我已經(成功)加載了另一個頁面。 – Brian 2013-04-07 22:39:30

回答

0

你沒有在Page_Load中執行任何代碼,那爲什麼你的頁面還要繼續加載?此外,你有太多的數據庫調用(關閉和打開連接)在您的Buttons_ClickDropdowns_SelectedIndexChanged

我懷疑你的SelectedIndexChanged事件爲您的下拉列表。當頁面加載項目添加到下拉列表中時,也會調用此事件。這會觸發事件,每個事件觸發在下拉每個項目

採取SelectedIndexChanged並嘗試加載網頁時,再次

+0

你的意思是刪除它? – Brian 2013-04-07 22:38:07

+0

是的。我的意思是再次從下拉列表中刪除事件 – codingbiz 2013-04-14 04:00:13