Frågan#1
Hej,
Har ett boknings verktyg som laddar en översikt. Det är byggt på MOSS.
Det fungerar och så men en sak som jag skulle vilja fixa är att inte göra en postaback och ladda om hela sidan när man t.ex. väljer ett annat datum utan bara kontrollen för översikten men andra parametrar.
Jag tror att det är AJAX jag bör använda. Hur ska jag gå till väga? har inte använt mig av det innan.
Måste jag göra om mycket nedan eller finns det något enkelt sätt med AJAX att "skriva" över postbacken ? :)
public partial class ResourceOverviewPage : UnsecuredLayoutsPageBase
{
private List<Resource> olist = new List<Resource>();
private ResourceOverview ctlResourceOverview = null;
//private MyBookings ctlMyBookings = null;
private trainingInfo ctlTrainingInfo = null;
//private DropDownList ddlSelectResourceType = null;
//private DropDownList ddlResourceTypes = null;
protected ToolBar tlb;
private int ResourceSelectedIndex = 0;
private DateTime _selectedDate = DateTime.Today;
private string _location;
private string _building;
private string _resourceSelectedValue;
private SPWeb site;
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["Location"] != null)
{}
else
{
if (Request.Params[BTUtilities.Constants.QueryString.Location] != null)
{
ViewState["Location"] = Request.Params[BTUtilities.Constants.QueryString.Location].ToString();
}
}
if (ViewState["Building"] != null)
{ }
else
{
if (Request.Params[BTUtilities.Constants.QueryString.Building] != null)
{
ViewState["Building"] = Request.Params[BTUtilities.Constants.QueryString.Building].ToString();
}
}
if (Request.Params["SelectedDate"] != null)
{
ViewState["_SelectedDate"] = SPUtility.CreateDateTimeFromISO8601DateTimeString(Request.Params["SelectedDate"]);
}
if (ViewState["ResourceSelectedValue"] != null)
{ }
else
{
if (Request.Params[BTUtilities.Constants.QueryString.ResourceSelectedValue] != null)
{
ViewState["ResourceSelectedValue"] = Request.Params[BTUtilities.Constants.QueryString.ResourceSelectedValue];
}
}
if (Request.Params[BTUtilities.Constants.QueryString.ResourceName] != null)
{
ViewState["ResourceName"] = Request.Params[BTUtilities.Constants.QueryString.ResourceName];
}
if (Request.Params[BTUtilities.Constants.QueryString.ResourceOverviewType] != null)
{
ViewState[BTUtilities.Constants.ViewStateString.ResourceOverviewType] = Request.Params[BTUtilities.Constants.QueryString.ResourceOverviewType];
}
ViewState["ResourceSelectedIndex"] = 0;
ViewState["LocationSelectedIndex"] = 0;
ViewState["BuildingSelectedIndex"] = 0;
if (!Page.IsPostBack)
{
loadTime.Text = "<!--BT_LOADTIME" + DateTime.Now.ToString();
buildcontrols();
loadTime.Text = loadTime.Text + ">>>" + DateTime.Now.ToString() + "-->";
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
//olist = ResourceManager.GetRooms();
//ddlSelectResourceType = new DropDownList();
//ddlSelectResourceType.AutoPostBack = true;
//ddlSelectResourceType.SelectedIndexChanged += new EventHandler(ddlSelectResourceType_SelectedIndexChanged);
site = SPControl.GetContextWeb(Context);
if (site != null)
{
this.MasterPageFile = site.MasterUrl;
}
}
private void buildcontrols()
{
SPContext.Current.Web.CheckPermissions(SPBasePermissions.ViewPages);
if (ViewState["_SelectedDate"] == null)
{
ViewState["_SelectedDate"] = _selectedDate;
}
DateTime StartDate = DateTime.Today.AddYears(-4);// (DateTime)ViewState["_SelectedDate"];
DateTime EndDate = DateTime.Today.AddYears(4); // StartDate.AddDays(Convert.ToDouble(ResourceOverview.GantView.TwoWeek));
// no filter only date...
// all resources when no location and resourcetyep is selected
if (ViewState["Location"] == null && ViewState["ResourceSelectedValue"] == null)
{
olist = ResourceManager.GetRooms(StartDate, EndDate);
}
// all resources with location
if (ViewState["Location"] != null && ViewState["ResourceSelectedValue"] == null && ViewState["Building"] == null)
{
olist = ResourceManager.GetRooms(ViewState["Location"].ToString(), null, null, null, StartDate, EndDate);
}
// all resources with ContentType
if (ViewState["Location"] == null && ViewState["ResourceSelectedValue"] != null)
{
olist = ResourceManager.GetRooms(ViewState["ResourceSelectedValue"].ToString(), StartDate, EndDate);
}
// all resources with contenttype, location
if (ViewState["Location"] != null && ViewState["ResourceSelectedValue"] != null && ViewState["Building"] == null)
{
olist = ResourceManager.GetRooms(ViewState["ResourceSelectedValue"].ToString(), ViewState["Location"].ToString(), StartDate, EndDate);
}
// all resources with contenttype, location and building
if (ViewState["Location"] != null && ViewState["Building"] != null && ViewState["ResourceSelectedValue"] != null)
{
olist = ResourceManager.GetRooms(ViewState["ResourceSelectedValue"].ToString(), ViewState["Location"].ToString(), ViewState["Building"].ToString(), StartDate, EndDate);
}
// all resources with location and building
if (ViewState["Location"] != null && ViewState["Building"] != null && ViewState["ResourceSelectedValue"] == null)
{
olist = ResourceManager.GetRooms(ViewState["Location"].ToString(), ViewState["Building"].ToString(), null, null, null, null, StartDate, EndDate);
}
if (ViewState["ResourceSelectedValue"] == null || ViewState["ResourceSelectedValue"].ToString() == BTUtilities.Constants.ResourceTypes.AllResources)
{
// Remove training items
for (int i = olist.Count - 1; i >= 0; i--)
{
if (olist[i].ContentType == BTUtilities.Constants.ResourceTypes.Training)
{
olist.Remove(olist[i]);
}
}
}
//ctlMyBookings = (MyBookings)Page.LoadControl(@"~/_CONTROLTEMPLATES/LocalTransport/MyBookings.ascx");
ctlResourceOverview = (ResourceOverview)Page.LoadControl(@"~/_CONTROLTEMPLATES/LocalTransport/ResourceOverview.ascx");
ctlResourceOverview.Datasource = olist;
ctlResourceOverview.ImageHeight = "";
ctlResourceOverview.ImageWidth = "40%";
ctlResourceOverview.ShowCalendar = ResourceOverview.GantView.TwoWeek;
ctlResourceOverview.StartDateCalendar = (DateTime)ViewState["_SelectedDate"];
string query;
if (ViewState["Location"] != null)
{
_location = ViewState["Location"].ToString();
}
else
{
_location = string.Empty;
}
if (ViewState["Building"] != null)
{
_building = ViewState["Building"].ToString();
}
else
{
_building = string.Empty;
}
if (ViewState["ResourceSelectedValue"] != null)
{
_resourceSelectedValue = ViewState["ResourceSelectedValue"].ToString();
}
else
{
_resourceSelectedValue = string.Empty;
}
string resourceOverviewType = null;
if (ViewState[BTUtilities.Constants.ViewStateString.ResourceOverviewType] != null)
{
resourceOverviewType = ViewState[BTUtilities.Constants.ViewStateString.ResourceOverviewType].ToString();
}
query = BTUtilities.buildQuerystring(resourceOverviewType, null, _location, _building, _resourceSelectedValue, (DateTime)ViewState["_SelectedDate"]);
ctlResourceOverview.QueryString = query;
pnl.Controls.Add(ctlResourceOverview);
//pnl.Controls.Add(ctlMyBookings);
}
private void ShowTrainingInfo(string ResourceName)
{
ctlTrainingInfo = (trainingInfo)Page.LoadControl(@"~/_CONTROLTEMPLATES/LocalTransport/trainingInfo.ascx");
pnl.Controls.Add(ctlTrainingInfo);
}
protected override void CreateChildControls()
{
tlb = (ToolBar)Page.LoadControl(@"~/_CONTROLTEMPLATES/Toolbar.ascx");
tlb.ID = "NavNodesTB";
BtnTemplate buttons = new BtnTemplate();
if (ViewState["Location"] != null)
{
buttons.Location = ViewState["Location"].ToString();
}
if (ViewState["Building"] != null)
{
buttons.Building = ViewState["Building"].ToString();
}
tlb.Template_Buttons = buttons;//new BtnTemplate();
buttons.Event_Resource_SelectedIndexChanged += new BtnTemplate.ResourceSelectChanged(buttons_Resource_SelectedIndexChanged);
buttons.Event_DateChanged += new BtnTemplate.DateChanged(buttons_dtco_DateChanged);
buttons.Event_Location_SelectedIndexChanged += new BtnTemplate.LocationSelectChanged(buttons_Location_SelectedIndexChanged);
buttons.Event_Building_SelectedIndexChanged += new BtnTemplate.BuildingSelectChanged(buttons_Building_SelectedIndexChanged);
buttons.SelectedDate = (DateTime)ViewState["_SelectedDate"];
if (ViewState["ResourceSelectedValue"] != null)
{
buttons.SelectedResourceType = ViewState["ResourceSelectedValue"].ToString();
// Disable the select resource picker if ResourceSelectedValue = training
if (ViewState["ResourceSelectedValue"].ToString() == BTUtilities.Constants.ResourceTypes.Training)
{
buttons.ShowResourcePicker = false;
}
}
pnlSelection.Controls.Add(tlb);
base.CreateChildControls();
}
void buttons_dtco_DateChanged(object sender, DateChangedEventArgs e)
{
ViewState["_SelectedDate"] = e.SelectedDate;
//Server.Transfer("/intranet/_layouts/localtransport/resourceoverview.aspx?selecteddate=123");
buildcontrols();
}
void buttons_Resource_SelectedIndexChanged(object sender, TemplateEventArgs e)
{
if (e != null)
{
ViewState["ResourceSelectedIndex"] = e.SelectedIndex;
if (e.SelectedContentType == "All Resources")
{
ViewState["ResourceSelectedValue"] = null;
}
else
{
ViewState["ResourceSelectedValue"] = e.SelectedContentType;
}
}
buildcontrols();
}
void buttons_Location_SelectedIndexChanged(object sender, LocationEventArgs e)
{
if (e != null)
{
ViewState["LocationSelectedIndex"] = e.SelectedIndex;
if (e.SelectedValue == "All Locations")
{
ViewState["Location"] = null;
}
else
{
ViewState["Location"] = e.SelectedValue;
}
}
buildcontrols();
}
void buttons_Building_SelectedIndexChanged(object sender, BuildingEventArgs e)
{
if (e != null)
{
ViewState["BuildingSelectedIndex"] = e.SelectedIndex;
if (e.SelectedValue == "All Buildings")
{
ViewState["Building"] = null;
}
else
{
ViewState["Building"] = e.SelectedValue;
}
}
buildcontrols();
}
}