在Page_Load和page_preendercomplete中控制null
本文关键字:控制 preendercomplete null Page Load page | 更新日期: 2023-09-27 18:07:03
这个问题已经困扰我好几个星期了。这个页面基本上是一个模板,我想改变各种标签的文本属性,我从我的数据库得到的任何信息。这也需要在用户看到页面之前完成。在page_load事件中所有的标签都是空的,所以我在线阅读,我应该在页面生命周期中等待。当我把它放在page_preendercomplete事件时,它的工作不可靠。有时局部有效,有时不有效。但它从来不会在现场工作。我还尝试使用FindControl方法,但也失败了。当我创建一个新的测试页面时,它似乎确实可以在preender complete事件中工作,但当我添加所有代码时,它再次失败。
<%@ 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>
下面是文件
后面的代码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];
}
}
关于你的题目和问题,我想看看这个页面:
http://msdn.microsoft.com/en-us/library/ms178472 (v = vs.85) . aspx
描述。net系统中的页面生命周期。要特别注意Load()(在所有控件初始化之前为页面调用)和LoadComplete()(在控件加载后调用)之间的区别。
我自己总是使用PreRender()。这将在事件被触发后发生。
我希望你想要LoadComplete()事件。
另外:
不确定你的问题,但GetContentByType可以改进,像这样:
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;
}
这里是没有ArrayList(让@JohnSaunders高兴):
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();
}