简体   繁体   中英

How to add connection string reference in master page code behind

I'm trying to reference a connection string set up in my web.config file using a master.cs file that is supposed to supply this value for my regular aspx.cs file. When I reference the connection string variable that is set in the master page's cs file, my regular .cs page or code behind page doesn't recognize it in intellisense. I'm a little of a newbie, so there's a small misunderstanding that I have about how master page code behind files interact with the aspx and cs files that it is a master for. Note, I am using the AHAH method rather than AJAX where there is no xml or json as the recipient of the get request, and instead it returns 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>

There's a property on the System.Web.UI.Page class that contains a reference to the master page named Master.

In order to share the connection created in the master page you are going to have to store it somewhere the Web Page can find it. This is usually done in a property, eg

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

Then in your web page, use the Master property to get the master page. There's a cast to your specific type in order to access the property on your 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();
        }
    }
}

You have declared the

SqlConnection con

variable inside the page_load function. So the variable not accessible outside the master page's page_load method. Even within the master page itself.

Declare the variable outside the method with public/protected access modifier and access them like this,

((MyMasterPage)this.Master).con;

Thanks,

请确保您正在将正确的库添加到母版页,请选中此谢谢。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM