如何访问< div>来自C#代码的属性? [英] How do I access a <div> attribute from the C# code behind?
问题描述
大家好,
我正试图从后面的C#代码访问(并编辑)我的asp页面中特定
我的div:
< div id =searchParamsmodifiedstring =这是一个测试runat =server>< / div>
在C#代码隐藏中,我试图通过此功能访问它:
public static void getItemsBySubject(string search)
{
List< HeritageItems> ItemList = DataAccess.FindItemsBySubject(search);
string modifiedstring = Jsonify(ItemList);
*我想在这里访问searchParams但不能*
}
我得到一个非静态字段,方法或属性需要对象引用错误
但是我可以在页面后面的相同代码上访问它:
protected void Page_Load(object sender,EventArgs e)
{
searchParams.etcetera
}
我尝试过:
我显然是初学者,所以我可能会遗漏一些非常明显的东西。我在网上看到的所有类似问题的帮助页面都显示了Page_Load函数中的示例。这让我觉得它与它有关....
但是我怀疑(希望)有一种方法可以从另一个独立的功能访问它我会从另一页打电话。在控制台应用程序中工作时,这可能需要实例化一个对象,但是我尝试尝试一些不同的方法的尝试没有奏效,因为即使在语句中使用也无法访问
任何建议都会受到赞赏。
这是因为你的功能是静态的。要么使函数非静态,要么将对象作为参数传递给函数并从那里使用它。
静态函数存在于类型级别但是你的page是您的类型的实例。如果您有五个页面实例,那么每个页面都有自己的searchParams实例,但是它们都共享相同的getItemsBySubject函数,所以如果你在该函数中使用了searchParams,那么它将使用该对象的哪个实例
Hi everyone,
I'm trying to access (and edit) the attribute of a particular
My div:
<div id="searchParams" modifiedstring="This is a Test" runat="server"></div>
In the C# code-behind I am trying to access it from this function:
public static void getItemsBySubject(string search) { List<HeritageItems> ItemList = DataAccess.FindItemsBySubject(search); string modifiedstring = Jsonify(ItemList); *I Want to access searchParams here but cannot* }
I get an "object reference is required for the non-static field, method, or property" error
However I can access it just fine here on the same code behind page:
protected void Page_Load(object sender, EventArgs e) { searchParams.etcetera }
What I have tried:
I'm obviously a beginner in this so I could be missing something very obvious. All the help pages I've seen online for similar questions show the example in the Page_Load function. Which makes me think that has something to do with it....
But I suspect (hope) there is a way of accessing this from another stand alone function that I will call from another page. When working in console apps this might require an object to be instantiated, but my attempts at trying a few different methods haven't worked because the
Any suggestions would be appreciated.
It's because your function is static. Either make the function non-static, or pass the object in as a param to the function and use it from there.
Static functions exist at the type level however your page is an instance of your type. If you have five instances of your page then each page has its own instance of searchParams, however they all share the same getItemsBySubject function, so if you used "searchParams" inside that function, which instance of that object would it use as there are five.
这篇关于如何访问< div>来自C#代码的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!