void Application_End(Object sender, EventArgs e) { // Code that runs on application shutdown }
void Application_Error(Object sender, EventArgs e) { // Code that runs when an unhandled error occurs } void Session_Start(Object sender, EventArgs e) { // Code that runs when a new session is started } void Session_End(Object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. }
如果不是上述情况,是单独位于页面中的话,你可在代码中读取需要的数据库数据,然后再对label的属性进行操作.label不支持诸如你所说的DataBindings,Binding之类操作,但你可以使用Page.DataBind()为它绑定数据,但这种做法是不明智的.
this.lblName.Text = (string)Reader["Name"];
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Data.OleDb"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat=Server>
private DataRow dw;
private DataTable dt;
private int curLine;
void Page_Load()
{
if (!Page.IsPostBack)
{
Init_CacheVar();
RefreshData();
}
} void Init_CacheVar()
{
Session.Remove("DataTable");
Session.Remove("DataRow");
Session.Remove("CurLine");
}
void Page_PreRender()
{
if(Page.IsPostBack)
RefreshData();
IDValue.Text = dw["ID"].ToString();
DescValue.Text = dw["ifDescr"].ToString();
}
void RefreshData()
{
if (Session["DataTable"] == null)
{
String connectString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=D:\\SourceSample\\MIB_DELPHI\\TSysnapseVersionNew3\\复件 MIBInfos97.mdb;Mode=Share Deny None;Extended Properties=\"\";Jet OLEDB:System database=\"\";Jet OLEDB:Registry Path=\"\";Jet OLEDB:Engine Type=4;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False";
OleDbDataAdapter adapter = new OleDbDataAdapter("Select ID,ifDescr from MIBInfos", connectString);
DataSet ds = new DataSet();
adapter.Fill(ds, "Table1"); dt = ds.Tables[0];
dw = dt.Rows[0];
curLine = 0; Session["DataTable"] = dt;
Session["DataRow"] = dw;
Session["CurLine"] = curLine;
}
else
{
dt = (DataTable)Session["DataTable"];
curLine = int.Parse(Session["CurLine"].ToString());
dw = dt.Rows[curLine];
}
} void NextBtn_Click(object sender, EventArgs e)
{
int curLine = (int)Session["CurLine"];
dt = (DataTable)Session["DataTable"];
if (curLine < dt.Rows.Count-1)
{
Session["CurLine"] = curLine + 1;
}
} void PreviousBtn_Click(object sender, EventArgs e)
{
int curLine = (int)Session["CurLine"]; if (curLine > 0)
{
Session["CurLine"] = curLine -1;
}
} void LastBtn_Click(object sender, EventArgs e)
{
dt = (DataTable)Session["DataTable"];
Session["CurLine"] = dt.Rows.Count - 1;
} void FirstBtn_Click(object sender, EventArgs e)
{
Session["CurLine"] = 0;
}</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="IDLabel" Runat="server" Text="ID:" Width="30px" Height="19px"></asp:Label>
<asp:Label ID="IDValue" Runat="server" Width="124px" Height="19px"></asp:Label>
<br />
<asp:Label ID="DescLabel" Runat="server" Text="ifDescr:" Width="78px" Height="19px"></asp:Label>
<asp:Label ID="DescValue" Runat="server" Width="124px" Height="19px"></asp:Label>
<br />
<br />
<asp:Button ID="FirstBtn" Runat="server" Text="First" Width="94px" Height="24px" OnClick="FirstBtn_Click" />
<asp:Button ID="NextBtn" Runat="server" Text="Next" Width="92px" Height="24px" OnClick="NextBtn_Click" />
<asp:Button ID="PreviousBtn" Runat="server" Text="Previous" Width="100px" Height="24px"
OnClick="PreviousBtn_Click" />
<asp:Button ID="LastBtn" Runat="server" Text="Last" Width="84px" Height="24px" OnClick="LastBtn_Click" />
</div>
</form>
</body>
</html>
<!--下面是Global.asax文件中的内容-->
<%@ Application Language="C#" %><script runat="server"> void Application_Start(Object sender, EventArgs e) {
// Code that runs on application startup }
void Application_End(Object sender, EventArgs e) {
// Code that runs on application shutdown }
void Application_Error(Object sender, EventArgs e) {
// Code that runs when an unhandled error occurs } void Session_Start(Object sender, EventArgs e) {
// Code that runs when a new session is started } void Session_End(Object sender, EventArgs e) {
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised. }
</script>
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 49: if(myReader.Read())
行 50: {
行 51: lbNo.Text=(string)myReader["Id"];
行 52: lbName.Text=(string)myReader["name"];
行 53: lbType.Text=(string)myReader["type"];
源文件: c:\inetpub\wwwroot\website\sql_webdatagrid2\showbook.aspx.cs 行: 51
lbName.Text=(string)myReader["name"];
lbNamelbType.Text=(string)myReader["type"];都改成lbNo.Text=myReader["Id"].Tostring();
lbName.Text=myReader["name"].Tostring();
lbNamelbType.Text=myReader["type"].Tostring();就可以了啊。
lbName.Text=Convert.ToString(myReader["name"]);
lbNamelbType.Text=Convert.ToString(myReader["type"]);
或
lbNo.Text=myReader["Id"].ToString();
lbName.Text=myReader["name"].ToString();
lbNamelbType.Text=myReader["type"].ToString();