网格视图不会显示

时间:2013-09-18 21:55:23

标签: vb.net gridview

我正在使用母版页创建一个包含两个页面的网站。我想在登录时在第二页的gridview中显示我的表的所有内容。 我不知道出了什么问题以及为什么第二页上的gridview不会显示。但如果我在第一页上这样做,它就可以正常工作。

Imports System.Data
Imports System.Xml

Partial Class Default2
    Inherits System.Web.UI.Page
    Dim conn2 As New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & Server.MapPath("~/App_Data/Tony.mdb"))
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("uname") = "" Then
            iduser.Text = "Sorry you are not logged-in to view this page."
        ElseIf Session("uname") = "Wrong username and/or password. Please try again." Or Session("uname") = "Successfully signed-up! Go back to the home page to log-in" Then
            iduser.Text = Session("uname")
        Else
            Dim cload As New OleDb.OleDbDataAdapter("select * from sample where username='" & Session("uname") & "'", conn2)
            Dim tester As New DataSet
            iduser.Text = "Welcome " & Session("uname") & "!"
            tester.Clear()
            cload.Fill(tester)
            Session("valid") = tester.Tables(0).DefaultView.Item(0).Item(9)
            If Session("valid") = "y" Then
                results.DataSource = tester.Tables(0).DefaultView
                results.DataBind()
                lfn.Visible = True
                lln.Visible = True
                lag.Visible = True
            End If
        End If
    End Sub 

接下来是aspx只是现在我已经添加了一个解决方法,虽然我仍然想要一个明确的解释为什么不会gridview显示。正如您所看到的,我向对象添加了一个数据源,因此我不再需要对数据源或数据绑定进行编码。不过,我更喜欢硬编码。

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p style="height: 39px">
        <asp:Label ID="iduser" runat="server"></asp:Label>
        <asp:GridView ID="results" runat="server" AutoGenerateColumns="False" 
            Width="249px" EmptyDataText="No Records" DataSourceID="AccessDataSource1">
            <Columns>
                <asp:BoundField DataField="fname" HeaderText="fname" SortExpression="fname" />
                <asp:BoundField DataField="lname" HeaderText="lname" SortExpression="lname" />
                <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
                <asp:BoundField DataField="gender" HeaderText="gender" 
                    SortExpression="gender" />
            </Columns>
        </asp:GridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/App_Data/Tony.mdb" 
            SelectCommand="SELECT [fname], [lname], [age], [gender] FROM [sample] WHERE ([username] = ?)">
            <SelectParameters>
                <asp:SessionParameter Name="username" SessionField="uname" Type="String" />
            </SelectParameters>
        </asp:AccessDataSource>
        <asp:Button ID="bsearch" runat="server" Text="Search:" Visible="False" />
&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="search" runat="server" Visible="False"></asp:TextBox>
    </p>
    <br />
    <asp:ListBox ID="listsearch" runat="server" Width="216px"></asp:ListBox>
    <br /><h2>
        <asp:Label ID="Label1" runat="server" Text="Edit Profile Here:" Visible="False"></asp:Label>
    </h2>
    <p>
        &nbsp;<asp:Label ID="lfn" runat="server" Text="First Name:&nbsp;&nbsp;" 
            Visible="False"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="fname" runat="server" Enabled="False" Visible="False"></asp:TextBox>
&nbsp;<asp:Label ID="fn" runat="server" ForeColor="#CC3300"></asp:Label>
&nbsp;&nbsp;&nbsp;
</p>
<p>
    <asp:Label ID="lln" runat="server" Text="Last Name:&nbsp;&nbsp;" 
        Visible="False"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="lname" runat="server" Enabled="False" Visible="False"></asp:TextBox>
&nbsp;<asp:Label ID="ln" runat="server" ForeColor="#CC3300"></asp:Label>
&nbsp;&nbsp;&nbsp;
</p>
<p>
    <asp:Label ID="lag" runat="server" Text="Age:" Visible="False"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="age" runat="server" Enabled="False" Visible="False"></asp:TextBox>
&nbsp;<asp:Label ID="ag" runat="server" ForeColor="#CC3300"></asp:Label>
&nbsp;&nbsp;&nbsp;
</p>
<p>
    <asp:Label ID="lgen" runat="server" Text="Gender:&nbsp;&nbsp;" Visible="False"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:DropDownList ID="gender" runat="server" Enabled="False" Visible="False">
        <asp:ListItem>Male</asp:ListItem>
        <asp:ListItem>Female</asp:ListItem>
    </asp:DropDownList>
&nbsp;</p>
<p>
    <asp:Label ID="lem" runat="server" Text="Email:" Visible="False"></asp:Label>
    &nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="email" runat="server" Enabled="False" Visible="False"></asp:TextBox>
&nbsp;<asp:Label ID="em" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
    <asp:Label ID="lun" runat="server" Text="Username:" Visible="False"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="username" runat="server" Enabled="False" Visible="False"></asp:TextBox>
&nbsp;<asp:Label ID="un" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
    <asp:Label ID="lpw" runat="server" Text="Password:" Visible="False"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="pass" runat="server" TextMode="Password" Enabled="False" 
        Visible="False"></asp:TextBox>
&nbsp;<asp:Label ID="pw" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
    <p>
        <asp:Label ID="lpw2" runat="server" Text="Re-type Pass:" Visible="False"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="pass2" runat="server" TextMode="Password" Enabled="False" 
            Visible="False"></asp:TextBox>
&nbsp;<asp:Label ID="pw2" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
    <p>
        <asp:Label ID="ladm" runat="server" Text="Admin?" Visible="False"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:DropDownList ID="admin" runat="server" Visible="False">
        <asp:ListItem>n</asp:ListItem>
        <asp:ListItem>y</asp:ListItem>
    </asp:DropDownList>
    </p>
    <p>
        <asp:Button ID="bedit" runat="server" Text="Edit" Visible="False" />
&nbsp;&nbsp;&nbsp;
        <asp:Button ID="badd" runat="server" Text="Add Record" Visible="False" />
&nbsp;&nbsp;&nbsp;
        <asp:Button ID="bdelete" runat="server" Text="Delete Record" Visible="False" />
    </p>
</asp:Content>

1 个答案:

答案 0 :(得分:0)

您可能忘记打开连接?你能编辑这个

吗?
conn2.Open

在此之前:

Dim cload As New OleDb.OleDbDataAdapter("select * from sample where username='" & Session("uname") & "'", conn2)