如何在后面的母版页代码中添加连接字符串引用

时间:2016-03-02 21:25:55

标签: c# asp.net

我正在尝试使用master.cs文件引用我的web.config文件中设置的连接字符串,该文件应该为我的常规aspx.cs文件提供此值。当我引用在母版页的cs文件中设置的连接字符串变量时,我的常规.cs页面或页面后面的代码在intellisense中无法识别它。我是一个新手,所以我有一个小小的误解,我有关于文件后面的母版页代码如何与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);
    }
}

worker.master

<%@ 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>

3 个答案:

答案 0 :(得分:1)

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();
        }
    }
}

答案 1 :(得分:1)

您已声明

SqlConnection con
page_load函数中的

变量。所以变量不能在母版页面的page_load方法之外访问。即使在母版页本身内也是如此。

使用public / protected access modifier在方法外声明变量,并像这样访问它们,

((MyMasterPage)this.Master).con;

谢谢,

答案 2 :(得分:1)

请确保您正在为母版页添加正确的自由 请检查一下谢谢。