所以我目前正在学习ASP.NET,实际上今天是我的第二天。
所以现在我想要创建一个页面,其中我首先有一个动态生成的下拉列表,它只选择数据库和表中的第一个名称。我有工作
接下来我有一个gridview表,显示数据库中的所有数据,目前我已经硬编码了一个名称,因此页面不会崩溃(有100000行数据)
最后,当我发布我的代码时,你会看到一个复选框,就在我点击它时,gridview会显示出来。我只是试着这样做,因为这是我今天学到的一课的一部分。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Test of First Database pull</h1>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Text="Show Panel" OnCheckedChanged="checkbox1_CheckedChanged" />
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="FirstName" DataValueField="FirstName" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ASP.NETConnectionString1 %>" ProviderName="<%$ ConnectionStrings:ASP.NETConnectionString1.ProviderName %>" SelectCommand="SELECT DISTINCT [FirstName] FROM [SampleData] ORDER BY 'FirstName' "></asp:SqlDataSource>
<br />
<br />
<asp:Panel ID="Panel1" runat="server" Visible="False" >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="SSN" HeaderText="SSN" SortExpression="SSN" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="HomeStreet" HeaderText="HomeStreet" SortExpression="HomeStreet" />
<asp:BoundField DataField="HomeCity" HeaderText="HomeCity" SortExpression="HomeCity" />
<asp:BoundField DataField="HomeState" HeaderText="HomeState" SortExpression="HomeState" />
<asp:BoundField DataField="HomeZip" HeaderText="HomeZip" SortExpression="HomeZip" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="CompanyStreet" HeaderText="CompanyStreet" SortExpression="CompanyStreet" />
<asp:BoundField DataField="CompanyCity" HeaderText="CompanyCity" SortExpression="CompanyCity" />
<asp:BoundField DataField="CompanyState" HeaderText="CompanyState" SortExpression="CompanyState" />
<asp:BoundField DataField="CompanyZip" HeaderText="CompanyZip" SortExpression="CompanyZip" />
<asp:BoundField DataField="HomePhone" HeaderText="HomePhone" SortExpression="HomePhone" />
<asp:BoundField DataField="CompanyPhone" HeaderText="CompanyPhone" SortExpression="CompanyPhone" />
<asp:BoundField DataField="CellPhone" HeaderText="CellPhone" SortExpression="CellPhone" />
<asp:BoundField DataField="DateOfBirth" HeaderText="DateOfBirth" SortExpression="DateOfBirth" />
<asp:BoundField DataField="DateOfHire" HeaderText="DateOfHire" SortExpression="DateOfHire" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASP.NETConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:ASP.NETConnectionString1.ProviderName %>"
SelectCommand="SELECT [SSN], [LastName], [FirstName], [HomeStreet], [HomeCity], [HomeState], [HomeZip], [CompanyName],
[CompanyStreet], [CompanyCity], [CompanyState], [CompanyZip], [HomePhone], [CompanyPhone], [CellPhone],
[DateOfBirth], [DateOfHire] FROM [SampleData] WHERE [FirstName] = 'Bob' "></asp:SqlDataSource>
</asp:Panel>
</div>
</form>
</body>
</html>
这是我调用会话的C#部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Demos_GridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string name = DropDownList1.SelectedValue;
Session["FName"] = name;
}
protected void checkbox1_CheckedChanged(object sender, EventArgs e)
{
Panel1.Visible = CheckBox1.Checked;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
如你所见,我打电话给会议“我相信”我尝试使用这个
<%:Session["user"]%>
在该sql查询中,但它说我不能拥有&lt; %%&gt;在asp sql调用中。 所以,如果有人能帮助我,这将是惊人的。
答案 0 :(得分:0)
也许您应该将用户名添加为SqlParameter
?
SqlDataSource1.SelectParameters.Add(new SqlParameter("@Name", name));
// Of course don't forget to edit your query: WHERE [FirstName] = @Name
上述内容应该有效(注意我还没有测试过)。
修改强>
您可以通过配置SqlDataSource以从@Name
或Control
设置Session
参数的值来进一步缩短此过程。