我的母版页上有一个AJAX Extender Calendar控件。当我触发选择更改事件(下面)时,它会拉回信息,然后将其显示给标签控件,但是当导航到站点中的其他页面时,它会丢失信息。
我已经尝试将它放入会话变量中但是在回发时它表示值为null。
代码是
protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
var CalendarParam = new SqlParameter("@G_DateTime", SqlDbType.DateTime);
CalendarParam.Value = Calendar1.SelectedDate;
var CalendarParamNextDay = new SqlParameter("@G_DateTimePlusOneDay", SqlDbType.DateTime);
CalendarParamNextDay.Value = Calendar1.SelectedDate.AddDays(1);
using (SqlConnection sqlconn = new SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings["IslandersDB"].ConnectionString))
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Parameters.Add(CalendarParam);
sqlcmd.Parameters.Add(CalendarParamNextDay);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = "fixtures.GetGameDetails";
sqlcmd.Connection = sqlconn;
sqlconn.Open();
//SqlDataReader sqldr = sqlcmd.ExecuteReader();
using(SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd))
{
DataTable dt = new DataTable();
sqlda.Fill(dt);
foreach(DataRow dr in dt.Rows)
{
GetOpponenet go = new GetOpponenet();
go.GameDate = Convert.ToDateTime(dr[0]);
go.Opponenet = dr[1].ToString();
go.Location = dr[2].ToString();
string location = go.Location ;
string opponent = go.Opponenet;
DateTime GameDate = go.GameDate; ;
lblGameDate.Text = GameDate.ToString();
lblLocation.Text = location;
lblOponenet.Text = opponent;
}
}
}
}
任何和所有帮助都非常感激
母版页
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Islanders.master.cs" Inherits="cs_ASP_Islanders.Islanders" %>
<!DOCTYPE html PUBLIC>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Peterborough Islanders</title>
<link href="_CSS/Islanders-Main.css" rel="stylesheet" type="text/css">
<link href="_CSS/StickyFooter.css" rel="stylesheet" type="text/css">
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<meta name="description" content="Peterborough Islanders NIHL Ice Hockey Club" />
<script type="text/javascript">
function printDiv(ticket_sale) {
var printContents = document.getElementById(ticket_sale).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
</head>
<body class="BackGround">
<form id="form1" runat="server">
<div id="wrap">
<div id="main">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div class="Main">
<div class="Header">
<img src="_images/PageBasics/Logo.fw.png" class="Logo" alt="Peterborough Islanders" />
<img src="_images/PageBasics/billboard.fw.png" class="BillBoard" alt="Next Game" />
</div>
<div class="Body">
<div class="Links">
<a href="Default.aspx"><img src="_images/PageBasics/News.fw.png" border="0" height="46px" width="137px" alt="News"/></a>
<a href="Contact.aspx"><img src="_images/PageBasics/Contact.fw.png" border="0" height="46px" width="137px" alt="Contact Us" class="HeaderLink"/></a>
<a href="Fixtures.aspx"><img src="_images/PageBasics/Fixtures.fw.png" border="0" height="46px" width="137px" alt="Fixtures and Results" class="HeaderLink"/></a>
<a href="Gallery.aspx"><img src="_images/PageBasics/Gallery.fw.png" border="0" height="46px" width="137px" alt="Gallery" class="HeaderLink"/></a>
<a href="Roster.aspx"><img src="_images/PageBasics/Roster.fw.png" border="0" height="46px" width="137px" alt="Roster" class="HeaderLink"/></a>
<a href="Sponsorship.aspx"><img src="_images/PageBasics/Sponsors.fw.png" border="0" height="46px" width="137px" alt="Sponsors" class="HeaderLink"/></a>
<a href="Stats.aspx"><img src="_images/PageBasics/Stats.fw.png" border="0" height="46px" width="137px" alt="Stats" class="HeaderLink"/></a>
</div>
<div class="PageLeft">
<asp:ContentPlaceHolder ID="body" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="SubBody" runat="server" >
</asp:ContentPlaceHolder>
</div>
<div class="PageRight">
<div class="RightContent" style="Display:none">
<p>Last Game</p>
<img src="_images/OpponentLogo/IslandersGameIcon.fw.png" alt="Home Team" />
-V-
<img alt="" src="_images/OpponentLogo/CardiffDevilsLogo.png" height="102" width ="124"/>
<table width="295" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="195px">Teams</td>
<td width="25px">1</td>
<td width="25px">2</td>
<td width="25px">3</td>
<td width="25px">F</td>
</tr>
<tr>
<td>Peterborugh Islanders</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Cardiff Devils</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
<div class="RightContent">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Calendar ID="Calendar1" runat="server" BackColor="White"
BorderColor="Black" BorderStyle="Solid" CellSpacing="1" Font-Names="Verdana"
Font-Size="9pt" ForeColor="Black" Height="250px" NextPrevFormat="ShortMonth"
Width="300px" ondayrender="Calendar1_DayRender"
onselectionchanged="Calendar1_SelectionChanged1">
<DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333"
Height="8pt" />
<DayStyle BackColor="#CCCCCC" />
<NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
<OtherMonthDayStyle ForeColor="#000000" />
<SelectedDayStyle BackColor="#646473" ForeColor="White" Font-Bold ="True" />
<TitleStyle BackColor="#000000" BorderStyle="Solid" Font-Bold="True"
Font-Size="12pt" ForeColor="White" Height="12pt" />
<TodayDayStyle BackColor="#000000" ForeColor="White" />
</asp:Calendar>
<%--</ContentTemplate>
</asp:UpdatePanel>--%>
Blue = Home Game <br />
Orange = Away Game
<br /><br />
<div class="calendarGameInfo">
Game Date :<asp:Label ID="lblGameDate" runat="server" Text=""></asp:Label><br />
Location :<asp:Label ID="lblLocation" runat="server" Text=""></asp:Label><br />
Opponent :<asp:Label ID="lblOponenet" runat="server" Text=""></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="RightContent">
<asp:ContentPlaceHolder ID="BasketCounter" runat="server">
<asp:Panel ID="basket_panel" runat="server" Visible="True" Width="110%">
<div class="basket">
<div class="cart-count">
<span class="counter">
<img src="_images/Stack.png" class="ion-bag" />
<%--<b id="order_counter" runat="server">
<asp:Label ID="lblCounter" runat="server" Text=""></asp:Label>
</b>--%></span><span class="info-link"><a href="Checkout.aspx">Go to My Basket <i class="ion-chevron-right"></i></a>
</span>
</div>
</div>
</asp:Panel>
</asp:ContentPlaceHolder>
</div>
<img src="_images/PageBasics/facebook.png" class="FaceBook" alt="Join Us on Facebook" />
<p>Proud to be working with</p>
<img src="_images/PageBasics/phantoms.fw.png" alt="Peterborough Phantoms" />
</div>
</div>
</div>
</div>
</div>
<div id="footer">
<div class="Sponsors">
<img src="_images/PageBasics/LineDivide.fw.png" alt="" />
<p>Proudly supported by: </p>
<br/>
<img src="_images/Sponsors/_logo-h87.fw.png" alt="" class="SponsorImage"/>
<img src="_images/Sponsors/officialclubnights.png" alt="Offical Club Night Out" class="SponsorImage" />
<br />
<img src="_images/Sponsors/RedLionBourne_logo.png" alt="The Red Lion - Bourne" class="SponsorImage" />
<img src="_images/Sponsors/PlanetIceSmall.fw.png" alt="Planet Ice Peterborough" class="SponsorImage" />
<img src="_images/Sponsors/SiNET.fw.png" alt="Website hosted by SiNET Development" />
</div>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
namespace cs_ASP_Islanders
{
public partial class Islanders : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
public DataTable GetDates()
{
DataTable dt = new DataTable();
SqlCommand sqlcmd = new SqlCommand();
SqlConnection sqlconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["IslandersDB"].ConnectionString);
{
sqlcmd.Connection = sqlconn;
try
{
sqlconn.Open();
sqlcmd.CommandText = "select f.hometeam, f.awayteam, f.GameDateandTime, f.location from fixtures.fixtures f";
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlcmd);
sqlDa.Fill(dt);
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
sqlconn.Close();
}
return dt;
}
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
try
{
DataTable dt = GetDates();
DateTime eventDate;
string eventType = string.Empty;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
//Where ColumnFieldForDate and ColumnFieldForEventType are the field names from your database
eventDate = Convert.ToDateTime(dt.Rows[i]["GameDateandTime"]);
eventType = dt.Rows[i]["Location"].ToString();
if (e.Day.Date == eventDate.Date)
{
if (eventType == "Peterborough")
{
e.Cell.BackColor = System.Drawing.Color.SkyBlue;
}
else
{
e.Cell.BackColor = System.Drawing.Color.Orange;
}
}
}
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}
protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
var CalendarParam = new SqlParameter("@G_DateTime", SqlDbType.DateTime);
CalendarParam.Value = Calendar1.SelectedDate;
var CalendarParamNextDay = new SqlParameter("@G_DateTimePlusOneDay", SqlDbType.DateTime);
CalendarParamNextDay.Value = Calendar1.SelectedDate.AddDays(1);
using (SqlConnection sqlconn = new SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings["IslandersDB"].ConnectionString))
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Parameters.Add(CalendarParam);
sqlcmd.Parameters.Add(CalendarParamNextDay);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = "fixtures.GetGameDetails";
sqlcmd.Connection = sqlconn;
sqlconn.Open();
//SqlDataReader sqldr = sqlcmd.ExecuteReader();
using(SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd))
{
DataTable dt = new DataTable();
sqlda.Fill(dt);
foreach(DataRow dr in dt.Rows)
{
GetOpponenet go = new GetOpponenet();
go.GameDate = Convert.ToDateTime(dr[0]);
go.Opponenet = dr[1].ToString();
go.Location = dr[2].ToString();
string location = go.Location ;
string opponent = go.Opponenet;
DateTime GameDate = go.GameDate; ;
lblGameDate.Text = GameDate.ToString();
lblLocation.Text = location;
lblOponenet.Text = opponent;
}
}
}
}
//private void check_cart()
//{
// //if (Session["cart_counter"] == null)
// //{
// // order_counter.InnerText = "0";
// //}
// //else
// //{
// // basket_panel.Visible = true;
// // order_counter.InnerText = Convert.ToString(Session["cart_counter"]);
// //}
//}
}
}
答案 0 :(得分:1)
我发现我通过使用会话正确地做到了,但是我需要在Page_PreRender中获取信息
protected void Page_PreRender(object sender, EventArgs e)
{
if(Session["Date"] != null)
{
string opponent = (string)Session["Opponent"];
string location = (string)Session["Location"];
DateTime GameDate = (DateTime)Session["Date"];
//DateTime CalendarDate = (DateTime)Session["Calendar"];
lblOponenet.Text = opponent;
lblLocation.Text = location;
lblGameDate.Text = GameDate.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}