1
我目前使用Visual Studio 2012,並且完全不熟悉ASP.NET編程。儘管如此,儘管搜索谷歌並發現了一些看起來可能會有所幫助的事情,但我最終陷入了這種情況。大多數遇到類似「名稱」錯誤的人在當前情況下並不存在,但似乎沒有非常類似的問題,而且他們中沒有一個與我相關。名稱「BodyTag」在當前上下文中不存在
到目前爲止,我已經嘗試過(刪除並重新創建設計器文件,無濟於事。這是一些以前的計算器問題推薦的,(b,這個小小的解決方法可以從外部訪問body標籤母版頁(即本質上是什麼,我想在這裏做)
這裏是我的代碼:
public HtmlGenericControl BodyTag
{
get
{
return MasterPageBodyTag;
}
set
{
MasterPageBodyTag = value;
}
}
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication1
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
HelloWorldLabel.Text = "Hello" + TextInput.Text;
if (Request.Cookies["BackgroundColor"] != null)
{
ColorSelector.SelectedValue = Request.Cookies["BackgroundColor"].Value;
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
}
}
protected void GreetList_SelectedIndexChanged(object sender, EventArgs e)
{
HelloWorldLabel.Text = "Hello, " + GreetList.SelectedValue;
}
protected void ColorSelector_IndexChanged(object sender, EventArgs e)
{
BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
HttpCookie cookie = new HttpCookie("BackgroundColor");
cookie.Value = ColorSelector.SelectedValue;
cookie.Expires = DateTime.Now.AddHours(1);
Response.SetCookie(cookie);
}
}
}
BodyTag已被此變通辦法在這裏定義
這看起來沒有問題,但最終沒有解決問題。我還進口權類等
請注意,我的母版頁有這樣的:
<body id="MasterPageBodyTag" runat="server">
萬一你認爲這可能是問題。另外請注意,這是由於合併各種代碼而產生的 - 我沒有自己創建這個代碼!我只是從事這樣的事情而開始工作。
無論如何,所有幫助大或小是非常讚賞。在此先感謝您的時間!
好吧發現輕微的變通。在默認頁面上創建一個div標籤並將其命名,然後將其放入我的代碼中。它只改變了一頁的背景,而不是主站點的背景。所以我仍然喜歡這個代碼無法正常工作的原因,否則它會困擾我一段時間。 –
閱讀標題。另外,請閱讀「這基本上是我在這裏要做的事情」。 –
如果已經很清楚,現在有人可能會試圖回答你的問題。我建議你更好地解釋你的問題是什麼,你試圖達到什麼樣的目標,以及你在哪裏得到一個錯誤。 – Blachshma