我正在使用母版页创建一个包含两个页面的网站。我想在登录时在第二页的 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" />
<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>
<asp:Label ID="lfn" runat="server" Text="First Name: "
Visible="False"></asp:Label>
<asp:TextBox ID="fname" runat="server" Enabled="False" Visible="False"></asp:TextBox>
<asp:Label ID="fn" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lln" runat="server" Text="Last Name: "
Visible="False"></asp:Label>
<asp:TextBox ID="lname" runat="server" Enabled="False" Visible="False"></asp:TextBox>
<asp:Label ID="ln" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lag" runat="server" Text="Age:" Visible="False"></asp:Label>
<asp:TextBox ID="age" runat="server" Enabled="False" Visible="False"></asp:TextBox>
<asp:Label ID="ag" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lgen" runat="server" Text="Gender: " Visible="False"></asp:Label>
<asp:DropDownList ID="gender" runat="server" Enabled="False" Visible="False">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
</p>
<p>
<asp:Label ID="lem" runat="server" Text="Email:" Visible="False"></asp:Label>
<asp:TextBox ID="email" runat="server" Enabled="False" Visible="False"></asp:TextBox>
<asp:Label ID="em" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lun" runat="server" Text="Username:" Visible="False"></asp:Label>
<asp:TextBox ID="username" runat="server" Enabled="False" Visible="False"></asp:TextBox>
<asp:Label ID="un" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="lpw" runat="server" Text="Password:" Visible="False"></asp:Label>
<asp:TextBox ID="pass" runat="server" TextMode="Password" Enabled="False"
Visible="False"></asp:TextBox>
<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>
<asp:TextBox ID="pass2" runat="server" TextMode="Password" Enabled="False"
Visible="False"></asp:TextBox>
<asp:Label ID="pw2" runat="server" ForeColor="#CC3300"></asp:Label>
</p>
<p>
<asp:Label ID="ladm" runat="server" Text="Admin?" Visible="False"></asp:Label>
<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" />
<asp:Button ID="badd" runat="server" Text="Add Record" Visible="False" />
<asp:Button ID="bdelete" runat="server" Text="Delete Record" Visible="False" />
</p>
</asp:Content>