[英]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.