Reputation: 29
This problem has been bothering me for weeks now. This page is basically a template and i want to change the text property of the various labels to whatever info i get from my database. This also needs to be done before the user sees the page. All the labels are null in the page_load event so i read online that i should wait later in the page life cycle. When i put it in the page_prerendercomplete event it works unreliably.Sometimes it works locally sometimes it doesnt. It never works live though. I also tried to use the FindControl method but that also fails.When i created a new test page, it did seem to work in the prerender complete event, but by the time i add all my code in it fails again.
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Library.aspx.cs" Inherits="Library" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id = "ContentContainer" align="center">
<br />
<br />
<h1 runat = 'server' id = 'HeaderTitle' align="center"
style="margin: 20px 20px 20px 20px; color: #000000; width: 750px; border-bottom-style: dotted; border-bottom-width: 1px; border-bottom-color: #808080;">
<asp:Literal ID="Literal1" runat="server">New C# All</asp:Literal></h1>
<br />
<br />
<br />
<br />
<br />
<br />
<div id = "Main" align="left">
Language:<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" DataSourceID="LinqDataSource1" DataTextField="Language1"
DataValueField="Language1">
</asp:DropDownList>
Types:<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True">All</asp:ListItem>
<asp:ListItem>Snippets</asp:ListItem>
<asp:ListItem>Tutorials</asp:ListItem>
<asp:ListItem>Collections</asp:ListItem>
</asp:DropDownList>
Sort By:<asp:DropDownList ID="DropDownList5" runat="server" AutoPostBack="True">
<asp:ListItem>New</asp:ListItem>
<asp:ListItem>Popular</asp:ListItem>
<asp:ListItem>Your</asp:ListItem>
</asp:DropDownList>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext" EntityTypeName=""
TableName="Languages">
</asp:LinqDataSource>
<asp:Panel ID="Panel1" runat="server">
<asp:Label ID="Label1" runat="server" Text="There are no records to display"></asp:Label>
</asp:Panel>
</div>
<div id = "Side" align="right">
</div>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<div id = "ContentContainer" align="center">
<br />
<br />
<h1 runat = 'server' id = 'HeaderTitleLoggedin' align="center"
style="margin: 20px 20px 20px 20px; color: #000000; width: 750px; border-bottom-style: dotted; border-bottom-width: 1px; border-bottom-color: #808080;">
<asp:Literal ID="Literal2" runat="server">New C# All</asp:Literal></h1>
<br />
<br />
<br />
<br />
<br />
<br />
<div id = "Main" align="left">
Language:<asp:DropDownList ID="DropDownList3" runat="server"
AutoPostBack="True" DataSourceID="LinqDataSource2" DataTextField="Language1"
DataValueField="Language1">
<asp:ListItem Selected="True">All</asp:ListItem>
</asp:DropDownList>
Types:<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True">All</asp:ListItem>
<asp:ListItem>Snippet</asp:ListItem>
<asp:ListItem>Tutorial</asp:ListItem>
<asp:ListItem>Collection</asp:ListItem>
</asp:DropDownList>
Sort By:<asp:DropDownList ID="DropDownList6" runat="server" AutoPostBack="True">
<asp:ListItem>New</asp:ListItem>
<asp:ListItem>Popular</asp:ListItem>
<asp:ListItem>Your</asp:ListItem>
</asp:DropDownList>
<asp:LinqDataSource ID="LinqDataSource2" runat="server"
ContextTypeName="DataClassesDataContext" EntityTypeName=""
TableName="Languages">
</asp:LinqDataSource>
<asp:Panel ID="Panel2" runat="server">
<asp:Label ID="Label2" runat="server" Text="There are no records to display"></asp:Label>
</asp:Panel>
</div>
<div id = "Side" align="right">
<div id = "Login">
</div>
</div>
</div>
</asp:Content>
Heres the code behind file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections;
public partial class Library : BasePage//System.Web.UI.Page
{
string language;
string author;
string title;
List<string> compare = new List<string>();
string compareto = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
language = Request.QueryString["language"];
author = Request.QueryString["author"];
title = Request.QueryString["title"];
if (string.IsNullOrEmpty(language + author + title))
{
FillPage();
}
else
{
FillBy();
}
//FillPage();
}
public static ArrayList GetContentByType(string ContentType, string language)
{
ArrayList list = new ArrayList();
DataClassesDataContext db = new DataClassesDataContext();
if (ContentType == "All" & language == "All")
{
var allcontents = db.User_Contents;
var allcontent = from ac in db.User_Contents
where ac.isApproved.ToString().Contains(char.Parse("t").ToString())
select ac;
foreach (User_Content ac in allcontent)
{
list.Add(ac);
}
}
if (ContentType != "All" & language != "All")
{
var contents = db.User_Contents;
var content = from c in db.User_Contents
where c.Type == ContentType
where c.Languages == language
where c.isApproved.ToString().Contains(char.Parse("t").ToString())
select c;
foreach (User_Content c in content)
{
list.Add(c);
}
}
if (ContentType != "All" & language == "All")
{
var contents = db.User_Contents;
var content = from c in db.User_Contents
where c.Type == ContentType
where c.isApproved.ToString().Contains(char.Parse("t").ToString())
select c;
foreach (User_Content c in content)
{
list.Add(c);
}
}
if (ContentType == "All" & language != "All")
{
var contents = db.User_Contents;
var content = from c in db.User_Contents
where c.Languages == language
where c.isApproved.ToString().Contains(char.Parse("t").ToString())
select c;
foreach (User_Content c in content)
{
list.Add(c);
}
}
return list;
}
private void FillPage()
{
try
{
try
{
Literal1.Text = DropDownList5.SelectedItem.Text + " " + DropDownList1.SelectedItem.Text + " " + DropDownList2.SelectedItem.Text;
Label1.Text = "";
}
catch
{
(FindControl("Literal1") as Literal).Text = DropDownList5.SelectedItem.Text + " " + DropDownList1.SelectedItem.Text + " " + DropDownList2.SelectedItem.Text;
(FindControl("Label1") as Label).Text = "";
}
}
catch
{
try
{
Literal2.Text = DropDownList6.SelectedItem.Text + " " + DropDownList3.SelectedItem.Text + " " + DropDownList4.SelectedItem.Text;
Label2.Text = "";
}
catch
{
(FindControl("Literal2") as Literal).Text = DropDownList6.SelectedItem.Text + " " + DropDownList3.SelectedItem.Text + " " + DropDownList4.SelectedItem.Text;
(FindControl("Label2") as Label).Text = "";
}
}
DataClassesDataContext db = new DataClassesDataContext();
ArrayList ContentList = new ArrayList();
if (!IsPostBack)
{
try
{
foreach (User_Content con in GetContentByType("All", DropDownList1.SelectedItem.Text))
{
ContentList.Add(con);
}
}
catch
{
foreach (User_Content con in GetContentByType("All", DropDownList3.SelectedItem.Text))
{
ContentList.Add(con);
}
}
}
else
{
try
{
foreach (User_Content con in GetContentByType(DropDownList2.SelectedItem.Text,DropDownList1.SelectedItem.Text))
{
ContentList.Add(con);
}
}
catch
{
foreach (User_Content con in GetContentByType(DropDownList4.SelectedItem.Text,DropDownList3.SelectedItem.Text))
{
ContentList.Add(con);
}
}
}
StringBuilder sb = new StringBuilder();
// <tr>
// <th>Author: </th>
// <td>{1}</td>
// </tr>
foreach (User_Content content in ContentList)
{
string description = "";
string useridimg = "";
var images = db.User_Infos;
var image = from i in db.User_Infos
where i.UserID == content.UserID
select i.ProfilePicLink;
useridimg = image.First<string>();
if ((content.ContentDescription).Length > 100)
{
content.ContentDescription.Remove(100);
}
else
{
description = content.ContentDescription;
}
string unit = "Days";
int time = 0;
var difference = ((DateTime.Now).Subtract(content.ContentCreationDate.Value));
if ((difference).TotalHours < 1)
{
time = (int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalSeconds;
unit = "seconds";
}
if ((difference).TotalHours < 24)
{
time = (int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalHours;
unit = "hours";
}
if ((difference).TotalHours > 24)
{
time = ((int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalDays);
unit = "days";
}
if ((difference).TotalDays > 365)
{
unit = "over a year ago";
}
string longago = "";
if (time != 0)
{
longago = time +" "+ unit + " ago";
}
else
{
longago = unit;
}
sb.Append(
string.Format(
@"<a href = 'ContentDisplay.aspx?ContentID={8}'> <table class = 'contentTable'>
<tr>
<th rowspan='6' width='150px'><img width='128' height='128' runat='server' src='{7}' /></th>
<th width = '50px'></td>
<td><h3>{0}</h3></td>
</tr>
<tr>
<td colspan='2'> Posted {5} by {1} </td>
</tr>
<tr>
<th>Language: </th>
<td>{6}</td>
</tr>
<tr>
<th>Description: </th>
<td>{2}</td>
</tr>
<tr>
<th>Rating: </th>
<td>{3}</td>
</tr>
<tr>
<th>Tags: </th>
<td>{4}</td>
</tr>
</table></a>",
content.ContentName, content.ContentAuthor, description, content.ContentRating, content.ContentTags, longago, content.Languages, useridimg, content.ContentID));
try
{
Label1.Text = sb.ToString();
}
catch
{
Label2.Text = sb.ToString();
}
}
}
public void FillFromFile()
{
}
public void FillBy()
{
DataClassesDataContext db = new DataClassesDataContext();
var ContentByLanguage = from l in db.User_Contents
//where l.Languages == language
select l;
var ContentByAuthor = from a in db.User_Contents
//where a.ContentAuthor == author
select a;
var ContentByTitle = from t in db.User_Contents
//where t.ContentName == title
select t;
#region All Cases
if (!string.IsNullOrEmpty(language) & string.IsNullOrEmpty(author) & string.IsNullOrEmpty(title))
{
var allcontent = from c in db.User_Contents
select c.Languages;
compare = allcontent.ToList();
}
if (!string.IsNullOrEmpty(author) & string.IsNullOrEmpty(language) & string.IsNullOrEmpty(title))
{
var allauthors = from a in db.User_Contents
select a.ContentAuthor;
compare = allauthors.ToList();
}
if (!string.IsNullOrEmpty(title) & string.IsNullOrEmpty(author) & string.IsNullOrEmpty(language))
{
var alltitles = from t in db.User_Contents
select t.ContentName;
compare = alltitles.ToList();
}
if (!string.IsNullOrEmpty(language) & !string.IsNullOrEmpty(author) & string.IsNullOrEmpty(title))
{
//var allLanguagesandAuthors = from la in db.User_Contents
// where la.Languages == language
// where la.ContentAuthor == author
// select la.
}
#endregion
foreach (var possibility in OrderByProximity(compareto, compare))
{
var content = from c in db.User_Contents
where c.Languages == possibility.Key
select c;
StringBuilder sb = new StringBuilder();
//string description = "";
//string useridimg = "";
//var images = db.User_Infos;
//var image = from i in db.User_Infos
// where i.UserID == content.UserID
// select i.ProfilePicLink;
//useridimg = image.First<string>();
//if ((content.ContentDescription).Length > 100)
//{
// content.ContentDescription.Remove(100);
//}
//else
//{
// description = content.ContentDescription;
//}
//string unit = "Days";
//int time = 0;
//var difference = ((DateTime.Now).Subtract(content.ContentCreationDate.Value));
//if ((difference).TotalHours < 1)
//{
// time = (int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalSeconds;
// unit = "seconds";
//}
//if ((difference).TotalHours < 24)
//{
// time = (int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalHours;
// unit = "hours";
//}
//if ((difference).TotalHours > 24)
//{
// time = ((int)((DateTime.Now).Subtract(content.ContentCreationDate.Value)).TotalDays);
// unit = "days";
//}
//if ((difference).TotalDays > 365)
//{
// unit = "over a year ago";
//}
//string longago = "";
//if (time != 0)
//{
// longago = time +" "+ unit + " ago";
//}
//else
//{
// longago = unit;
//}
sb.Append(
string.Format(
@"<a href = 'ContentDisplay.aspx?ContentID={7}'> <table class = 'contentTable'>
<tr>
<th rowspan='6' width='150px'><img width='128' height='128' runat='server' src='{6}' /></th>
<th width = '50px'></td>
<td><h3>{0}</h3></td>
</tr>
<tr>
<td colspan='2'> Posted by {1} </td>
</tr>
<tr>
<th>Language: </th>
<td>{5}</td>
</tr>
<tr>
<th>Rating: </th>
<td>{2}</td>
</tr>
<tr>
<th>Tags: </th>
<td>{3}</td>
</tr>
</table></a>",
content.First().ContentName, content.First().ContentAuthor, content.First().ContentRating, content.First().ContentTags, content.First().Languages, content.First().ContentID));
try
{
Label1.Text = sb.ToString();
}
catch
{
Label2.Text = sb.ToString();
}
}
}
public List<KeyValuePair<string, int>> OrderByProximity(string input, List<string> possibilities)
{
List<KeyValuePair<string, int>> RawData = new List<KeyValuePair<string,int>>();
foreach (var i in possibilities)
{
RawData.Add(new KeyValuePair<string, int>(i, FindClosestMatch(i, input)));
}
RawData.Sort(Compare1);
return RawData;
}
static int Compare1(KeyValuePair<string, int> a, KeyValuePair<string, int> b)
{
return a.Key.CompareTo(b.Key);
}
static int Compare2(KeyValuePair<string, int> a, KeyValuePair<string, int> b)
{
return a.Value.CompareTo(b.Value);
}
protected void Reload_Click(object sender, EventArgs e)
{
FillPage();
}
public int FindClosestMatch(string s, string t)
//{
//public static int Compute(string s, string t)
{
int n = s.Length;
int m = t.Length;
int[,] d = new int[n + 1, m + 1];
// Step 1
if (n == 0)
{
return m;
}
if (m == 0)
{
return n;
}
// Step 2
for (int i = 0; i <= n; d[i, 0] = i++)
{
}
for (int j = 0; j <= m; d[0, j] = j++)
{
}
// Step 3
for (int i = 1; i <= n; i++)
{
//Step 4
for (int j = 1; j <= m; j++)
{
// Step 5
int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
// Step 6
d[i, j] = Math.Min(
Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
d[i - 1, j - 1] + cost);
}
}
// Step 7
return d[n, m];
}
}
Upvotes: 1
Views: 1207
Reputation: 70513
In terms of your title and question I would take a look at this page:
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.85).aspx
It describes the page life cycle in .NET systems. In particular note the difference between Load() (Called for the page before all controls are initialized) and the LoadComplete() (Called after the controls are loaded).
I always used PreRender() myself. This will occur after events are fired.
I expect you want the LoadComplete() event.
In addition this:
Not sure about your problem but the GetContentByType can be improved, like this:
public static ArrayList GetContentByType(string ContentType, string language)
{
var allcontent = from ac in db.User_Contents
where c.Type == (ContentType == "All" ? c.Type : ContentType)
where c.Languages == (language == "All" ? c.Languages : language)
where ac.isApproved.ToString().Contains(char.Parse("t").ToString())
select ac;
ArrayList result = new ArrayList(allcontent);
return result;
}
Here it is without ArrayList (to make @JohnSaunders happy):
public static List<User_Content> GetContentByType(string ContentType, string language)
{
return (from ac in db.User_Contents
where c.Type == (ContentType == "All" ? c.Type : ContentType)
where c.Languages == (language == "All" ? c.Languages : language)
where ac.isApproved.ToString().Contains(char.Parse("t").ToString())
select ac).ToList();
}
Upvotes: 1