English 中文(简体)
Setting the Page-Title in .Net using C# from a Class
原标题:

Question,

How do i set the title of a page from a class. Is it even possible? I can and have set the page title from a page itself and a usercontrol.

Can I, How Do I do this via a class using C# .Net

Here is what im looking to do, From the Aspx Page i want to call a function that passes in the string title, and have the class set the page title.

SomePage.Aspx.CS

page_onload()   {   setPageTitle(titleValue);   }

SetPageTitleClass.CS

public static void setPageTitle(string iTitle)   {   Page.title = iTitle;  }

The problem is "Page.Title" is not available from the Class

最佳回答

First: why would you want to do that? --- give it back and let the page set it ... u can set it in a base class or master page.

If you still want to do it, is along the lines:

var page = (Page)HttpContext.Current.Handler;
page.Title = "someTitle";
问题回答

You will need to pass in a reference to the Page you want to set the title of to the c# class you are going to use.

Could you post more detail about what you are trying to do?

I think the best way would be to have the class expose a TitleChanged event, which the page can subscribe to.

In this way, you are not tightly coupling your solution and everything is kept nice and clean.

Yes. You must get a hold of the Page object. In the Page and UserControls this is relatively easy.

Page.Title = "My Title";




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签