Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi everyone,

I'm trying to access (and edit) the attribute of a particular
in my asp page from the C# code behind. While I can get at it by ID in the main Page_Load section, I cannot access it from a function on the page, which is where I want to do the work.

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
id is simply not accessible to even use in a statement.

Any suggestions would be appreciated.
Posted
Updated 26-Feb-19 4:02am

1 solution

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.
 
Share this answer
 
Comments
Member 14159645 26-Feb-19 11:22am    
Thanks, that's helpful. Changing the function to non-static resolved that, but opened up a new can of worms!

Are you suggesting that I could pass the div id in as a parameter of the function, then edit the attributes that way?
F-ES Sitecore 26-Feb-19 11:27am    
As long as you're calling getItemsBySubject from a non-static function you can pass it in, yes, just add it as a second param

public static void getItemsBySubject(string search, CorrectTypeHere control)
{
control.
}

I don't know what the type is going to be off the top of my head, you'll need to work that out.

BTW something I'll add here is that if this code is running as the result of a webmethod or an ajax call then you can't access server controls in this way; it only works for code running as a postback, ie a result of a form submission.
Member 14159645 28-Feb-19 10:21am    
Thanks again for your feedback. I'm still working on it, but hopefully I'm going in the right direction.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900