我无法根据使用基于会话的登录来填充网格视图。我正在使用母版页。目前我得到一个“必须声明标量变量”。我的想法是在脚本区域的某个地方检索它。任何帮助是极大的赞赏!!!!
现在的问题是非母版页的代码应该是什么,以根据某人的 uname(数据库中的列)或 UID(主键和理想方式)检索某人的特定信息 母版页的代码(至少相关的东西)
protected void btnlogin2_Click(object sender, EventArgs e)
{
if (btnlogin2.Text == "Login")
{
string strCmd = "Select * From Person Where uname=@uname and upass=@upass";
SqlConnection objConn = new SqlConnection(strConn);
SqlCommand objCmd = new SqlCommand(strCmd, objConn);
objCmd.Parameters.AddWithValue("@uname", txtusername.Text);
objCmd.Parameters.AddWithValue("@upass", txtpassword.Text);
using (objConn)
{
objConn.Open();
SqlDataReader objDR = objCmd.ExecuteReader();
if (objDR.Read())
{
Session["uname"] = txtusername.Text;
if (objDR["Type"].ToString() == "Member")
{ Response.Redirect("member.aspx"); }
else if (objDR["Type"].ToString() == "Pledge")
{ Response.Redirect("member.aspx"); }
else if (objDR["Type"].ToString() == "Admin")
{ Response.Redirect("eboard.aspx"); }
}
else
{
// btnlogin2.Text = "Failed";
}
}
btnlogin2.Text = "Logout";
}
else if (btnlogin2.Text == "Logout")
{
btnlogin2.Text = "Login";
txtusername.Visible = true;
txtpassword.Visible = true;
pnlSideBar.Visible = false;
Session.Abandon();
Response.Redirect("index.aspx");
}
btnlogin2.Text = "Failed";
}
页面代码
<%@ Page Language="C#" Debug="true" MasterPageFile="masterpage.master" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Configuration" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string strConn = WebConfigurationManager.ConnectionStrings["cloud2"].ConnectionString;
if (User.Identity.IsAuthenticated)
{
Button btnlogin2 = (Button)Master.FindControl("btnlogin2");
TextBox txtusername = (TextBox)Master.FindControl("txtusername");
TextBox txtpassword = (TextBox)Master.FindControl("txtpassword");
Panel pnlSideBar = (Panel)Master.FindControl("pnlSideBar");
Panel pnlPledge = (Panel)Master.FindControl("pnlPledge");
Panel pnlEboard = (Panel)Master.FindControl("pnlEboard");
string strCmd = "Select * From Person Where uname=@uname";
SqlConnection objConn = new SqlConnection(strConn);
SqlCommand objCmd = new SqlCommand(strCmd, objConn);
objCmd.Parameters.AddWithValue("@uname", Session["uname"]);
using (objConn)
{
objConn.Open();
SqlDataReader objDR = objCmd.ExecuteReader();
if (objDR.Read())
{
btnlogin2.Visible = true;
btnlogin2.Text = "Logout";
txtusername.Visible = false;
txtpassword.Visible = false;
if (objDR["Type"].ToString() == "Admin")
{
pnlEboard.Visible = true;
pnlPledge.Visible = false;
pnlSideBar.Visible = false;
}
else if (objDR["Type"].ToString() == "Member")
{
pnlEboard.Visible = false;
pnlPledge.Visible = false;
pnlSideBar.Visible = true;
}
else if (objDR["Type"].ToString() == "Pledge")
{
pnlPledge.Visible = true;
pnlEboard.Visible = false;
pnlSideBar.Visible = false;
}
lblLogin.Text = "Logged in as: " + objDR["Fname"] + " " + objDR["Lname"];
}
}
}
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphText" runat="server">
<div id="mainContent">
<h1> My Service Hours</h1>
<p> </p>
<table width="318" height="126" border="1" cellspacing="1">
<caption>
Event Event Date Hours
</caption>
<tr>
<th width="100" scope="col">Polar Plunge</th>
<th width="100" scope="col">1/13/12</th>
<th width="100" scope="col">5 hrs</th>
</tr>
</table>
<form id="form3" name="form2" method="post" action="">
<label for="Totalhrs"></label>
Total: 5hrs
</form>
<asp:GridView ID="grvServiceHours" DataSourceID="srcServiceHours" runat="server"/>
<asp:SqlDataSource
id="srcServiceHours"
ConnectionString="<%$ ConnectionStrings:cloud2 %>"
SelectCommand="select Event.Event_Name, Event.Event_Time, Event.Event_Type, Service_Hours.Hours, Service_Hours.Hours_Completed
from Event, Service_Hours, Person
where Event.EvID=Service_Hours.EvID and Person.UID=Service_Hours.UID and Person.Uname=@uname"
Runat="server" />
<p> </p>
<!-- end #mainContent --></div>
</asp:Content>