Modify Page Head in User Control - asp.net

How can I modify the head portion of a page from within an embeded user control? I know I can have the control run in the head portion of the .aspx page but I have a existing site with numerous pages that I don't want to change. One thing they all have in common is the menubar.ascx. So, I figured I could put the code there to modify the head element of the containing page, but no dice. The code I am trying to implement looks like this, however, the Page.Header is null.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim favicon As New HtmlLink
favicon.Attributes.Add("REL", "SHORTCUT ICON")
favicon.Attributes.Add("HREF", "images/bh_favicon.ico")
Page.Header.Controls.Add(favicon)
End Sub
I tried putting it in the PreRender and the Render events but same thing. The Page.Parent.Page.Header is null too. Is there a better way to do what I want to do? Utlimately I want to add a favicon to a group of pages that is different from the default favicon. Basically I have two sites on in the same code base.
Be nice, this is my first post.
TIA

You may need to make your Page Head run at server, so the usercontrol can see it.
eg:
<head runat="server">
Which I guess sort of defeats the point if this isn't already done on all your pages. Maybe a solution wide RegEx search/replace would be in order to implement this.

Thanks for your answers. I know I was asking for the least amount of work solution, however, I want to make the code easy for me to manage. I think what I am going to do is construct a master page as a template for all pages (like #devstuff suggested). Then I am going to change the existing pages, about 50 pages, to use the master page. That way if something like this pops in the future I can easily change everything in one place.
Thanks for you help!

As mentioned by #Program.X you may need a full search/replace. If you are going to do that you might want to go one step further and use a Master page, but it really depends on your time constraints and how many pages there are to modify.

Related

How to wait three seconds then turn to another webpage

I'm new to web development, and I'm currently using ASP.net. I wonder what would I need to do to let the browser wait for 3 seconds so my users can read the text "Customer Successfully Added" before turning to another page? I have attached my code as follows.
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim db As New DatabaseClass
db.addProfile(txtLN.Text, txtFN.Text, txtUsername.Text, txtPassword.Text, txtAddress.Text, txtZip.Text, txtPhone.Text, txtEmail.Text)
lblMessage.Text = "Customer Successfully Added"
End Sub
In addition, I'm not sure how to utilize MSDN. For me, its information overload, I'm wondering how to go about finding the solution on MSDN so i would be able to solve my problems in the future. Thank you!
You can't do it in the code behind of the page because of how asp.net works - the label text would not update until after the timeout occurred if you did it in the code-behind.
The server-side processing spits all the html back to the browser only after it has completely processed any server-side code unless you're using Ajax. Since you're new, I won't even bother going into how to do it with Ajax as there is a MUCH simpler option for accomplishing what you want.
A simple method to accomplish what you're looking for would be to have a simple HTML page that just has a message that says "Customer successfully added" and use javascript (client-side code) to pause and then redirect using the Javascript "SetTimeout" function.
There's an example here: http://www.bloggingdeveloper.com/post/JavaScript-Url-Redirect-with-Delay.aspx
The logic flow wshould work like this:
The original page should add the record (in code-behind) then redirect to this simple html page (in code-behind). The html page should have the "Customer Added" message and use the SetTimeout and Redirect to go to whatever page you want the user to see after viewing the message.
For stuff like this you need the code to run client side rather than on the server. The easiest way to do this is to return some javascript with your page (in the .aspx part rather than the code behind)
Take a look here for an idea of what to do :)
The page is displayed for a few seconds and then the javascript triggers a redirect to a url of your choosing. Just add something like this into your html.
You can emit javascript to redirect to the other page, using the setTimeout function.
This is best accomplished using the ScriptManager to register any javascript on the page.

Output caching a page except a user control in it

I have a page which contains a user control. The structure of the page is as shown below:
Incase your not able to see the above image, please check it at http://i54.tinypic.com/2r4id5f.jpg Now, apart from the contents of the UserControl, I'd like to cache the entire page. I tried using the OutputCache attribute in the .aspx page, however it caches the contents of the UserControl as well.
Kindly let me know how will I be able to cache the contents of the page except that of the user control.
Thanks in advance.
I think you can use the asp.net Substitution control to achieve this. Here is a link to ScottGu walking through an example.
The basic idea is that you cache you whole page as per usual, but mark parts for substitution that can be replaced for each request.
I think you are looking for VaryByControl. Also check out this post on fragment caching
Look at using substitutions.
This should help
However, the snag is, since substitution is done outside of the Page lifecycle, you can't render a user control for your substitution. You have to write a method that returns a string for the substitution. But this may work for you.
Have you tried adding the #OutputCache to both the usercontrol and the page but the usercontrol set the varyByParam="qsvalue;postvalue" where qsvalue is a generated query string you make random for every call of the page and postvalue is the same for postback.
The user control will still get cached, but in theory it should never get a chache hit as the qsvalue/postvalue is always different from that cached. It may not scale well - best set duration to the minimum as well, to prevent large numbers of them building up in the cache.

How can I link to a page that only exists with a postback?

I am trying to link to a third-party site. On that site is a list of links, and I need to be able to link my site to destination of one of those. The problem is that the links are not going to URLs, they're using ASP.NET postbacks to display the description of each item in the list.
Here is the URL where the list is. I want to be able to link to specific entries in this list.
https://www.license.state.tx.us/ContinuingEducation/ContinuingEducationProvider.aspx?ProviderType=750
Check for IsPostBack in the Page_Load, otherwise perform a Response.Redirect possibly? (If I'm understanding you correctly)
EDIT
Actually, it sounds like you want to link to a page that was generated on the value of a postback. I'm not sure you can, unless you have access to the page's code. You may try simulating the GET variables and see if the page generates the same content, otherwise it's however the author coded the page and what conditions need to be met to generate the "postback content(s)".

What is ct100 and how do I rename it?

Working in .net 4.0, it still seems all my input controls have the attribute 'name', with a value that begins 'ct100$...'.
Is there any way to rename this?
I've gone all the way up the control hierarchy, and given each control an ID and set its clientidmode to 'Static' to no avail, even the 'earliest' controls on the page still inherit the prefix.
This is the master page's ID. I change it by adding a Page_Init to my masterpage which sets its id:
Private Sub InitSub(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
ID = "master"
End Sub
This ID is normally empty/null so when it renders it it generates an id (starting at ct100 and going up)
Like #Scott Stafford said, keep it short because it prefixes every client id on your page.
I use words like "mBio", "mHome", etc..
Why rename it? You can, as #Bob Fincheimer describes, but so what? Also, if you DO rename it, keep the new name short, because that name shows up in all generated HTML and all POSTing variables hundreds of times, possibly enough to actually affect performance of your site.
If you want to remove it, look in your web.config for the following tag:
<system.web>
...
<pages ... clientIDMode="*something*">
</pages>
...
</system.web>
Remove the clientIDMode="something" property specification. Just take it out.
** I stole my own answer from here.

VB.NET if button1 caused post back then

I have a situation where I need to ignore parts of page load sub inside a isPostback = true. Basically inside the isPostBack I want something like if button1 caused postback.... do this else do this...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = True Then
If TextBox1.Text <> String.Empty Then
CheckDate()
End If
End If
End Sub
I think what you need is the reference to the control name which triggered the postback.
http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
Implement the solution which is there in the above link (Got it from here.... How to Get Source of postback)
If the control name is your button then do not do what needs to be done upon postback.
HTH
You should probably not have all this going on inside the Page_Load event. Instead, you should be handling events for each control that can cause a postback. This helps with code clarity, and ease of maintenance, not to mention better control in the first place.
Here's a nice brief blog entry I found on the subject: http://www.sitepoint.com/blogs/2007/01/21/page_load-is-evil/
Incidentally, handling events is much different in ASP.NET than in other environments, My guess, just based on the fact that you're trying to accomplish this in the Page_Load event is that you're not yet "getting" the event-driven programming model. (If I'm wrong, I apologize, I don't mean to be insulting).
If I'm right, however, once you get used to it, it's going to be a lot simpler for you than things were in the classic ASP days, for example, where you had to do things like try to figure out which button was clicked. Here's another nice article to explain this further: http://articles.sitepoint.com/article/driven-asp-net-development-c
It's hard to see this as a good idea. From the short snippet you posted, it looks like what you really need is a Validation control attached to your textbox.
Have a look at the POSTed items. You should see some sort of reference to that button in there. IIRC, if it was clicked, you will see some sort of reference in there, and if it wasn't it wouldn't be in there.
I had this same problem a while a ago and that's how I circumvented loading some stuff.

Resources