[英]How to add connection string reference in master page code behind
我正在嘗試使用master.cs文件引用在web.config文件中設置的連接字符串,該文件應該為我的常規aspx.cs文件提供此值。 當我引用在母版頁的cs文件中設置的連接字符串變量時,我的常規.cs頁或頁后代碼在智能感知中無法識別它。 我是個新手,所以我對文件背后的母版頁代碼如何與作為母版的aspx和cs文件進行交互有一個小小的誤解。 注意,我使用的是AHAH方法,而不是AJAX,在該方法中,沒有xml或json作為get請求的接收者,而是返回了html
addBusiness.aspx
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="worker.master.cs" Inherits="worker" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
addBusiness.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class addBusiness : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["firstName"] != null && Request["lastName"] != null && Request["fullBusinessName"] != null && Request["displayName"] != null && Request["fullAddress"] != null && Request["phone"] != null && Request["email"] != null && Request["password"] != null && Request["streetAddress"] != null && Request["city"] != null && Request["state"] != null && Request["zip"] != null && Request["lat"] != null && Request["lng"] != null)
{
//this is where I am trying to reference the connection string from worker.master
con.open();
}
}
}
worker.master.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class worker : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ViewEta"].ConnectionString);
}
}
工人大師
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="worker.master.cs" Inherits="worker" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
System.Web.UI.Page類上有一個屬性,該屬性包含對名為Master的母版頁的引用。
為了共享在母版頁中創建的連接,您必須將其存儲在Web頁可以找到的位置。 這通常在屬性中完成,例如
public partial class worker : System.Web.UI.MasterPage
{
public IDbConnection Con {get; private set;}
protected void Page_Load(object sender, EventArgs e)
{
this.Con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ViewEta"].ConnectionString);
}
}
然后在您的網頁中,使用Master屬性獲取母版頁。 您需要對特定類型進行強制轉換,才能訪問您的主人
public partial class addBusiness : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["firstName"] != null && Request["lastName"] != null && Request["fullBusinessName"] != null && Request["displayName"] != null && Request["fullAddress"] != null && Request["phone"] != null && Request["email"] != null && Request["password"] != null && Request["streetAddress"] != null && Request["city"] != null && Request["state"] != null && Request["zip"] != null && Request["lat"] != null && Request["lng"] != null)
{
worker masterPage = this.Master as worker;
masterPage.Con.open();
}
}
}
您已聲明
SqlConnection con
page_load函數中的變量。 因此,無法在母版頁的page_load方法之外訪問該變量。 即使在母版頁本身內。
使用public / protected訪問修飾符在方法外聲明變量,然后像這樣訪問它們,
((MyMasterPage)this.Master).con;
謝謝,
請確保您正在將正確的庫添加到母版頁,請選中此謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.