Custom Control Render JavaScript Code In Head Tags?

Feb 3, 2011

I am creating a custom control and was wondering if it is possible to render any JavaScript in the head tags of the page it is placed on? I am thinking I would need to use the FindControl method, but am not sure what I need to bind to. I am thinking the page object? Let me know if I am thinking in the right direction or if there is another option.

My only concern with the above mentioned idea is that how do I render all my content while placing some code in the head tags?

View 2 Replies


Similar Messages:

C# - How To Render Children Tags In A Custom Server Control

Nov 17, 2010

I'm working on a custom ServerControl, I've created it like below :

[ParseChildren(true), PersistChildren(true)]
[ToolboxData("<{0}:Menu runat="server"></{0}:Menu>")]
public class Menu : WebControl
{
.....
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public MenuItem MenuItems { get; set; }
}
[ParseChildren(true), PersistChildren(true)]
public class MenuItem : WebControl
{
......
[PersistenceMode(PersistenceMode.InnerProperty)]
public MenuItem SubMenuItems
{
get
{
if (_SubMenuItems == null) return new MenuItem();
return _SubMenuItems;
}
set
{
_SubMenuItems = value;
}
}
private MenuItem _SubMenuItems;
[TemplateContainer(typeof(MenuItem))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate Template { get; set; }
}
<%@ Register Assembly="JQueryMenu" Namespace="JQueryMenu" TagPrefix="MdsMenu" %>
<MdsMenu:Menu ID="Menu1" runat="server">
<AnimationItems AnimationSpeed="Fast" AnimationType="Opacity_Height" DropShadow="true"
Delay="1000" />
<MdsMenu:MenuItem ID="MenuItem1" runat="server" Text="MenuItem 01">
<MdsMenu:MenuItem runat="server">
<Template>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CheckBox ID="CheckBox1" runat="server" />
</Template>
</MdsMenu:MenuItem>
</MdsMenu:MenuItem>
<MdsMenu:MenuItem ID="MenuItem2" runat="server" Text="MenuItem 01">
<MdsMenu:MenuItem ID="MenuItem3" runat="server">
<Template>
<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:CheckBox ID="CheckBox2" runat="server" />
</Template>
<MdsMenu:MenuItem ID="MenuItem5" runat="server" Text="MenuItem 05">
</MdsMenu:MenuItem>
<MdsMenu:MenuItem ID="MenuItem6" runat="server" Text="MenuItem 06">
</MdsMenu:MenuItem>
<MdsMenu:MenuItem ID="MenuItem4" runat="server">
<Template>
<asp:Image ID="Image1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</Template>
</MdsMenu:MenuItem>
</MdsMenu:MenuItem>
</MdsMenu:MenuItem>
</MdsMenu:Menu>

Now, How can I Parse it and render it in RenderContent method ? !!!
The following method is always throw the first Exception, it means this.Controls is always empty !!! How can I do it and how I can access to the nested children in RenderControl method ?

public class Menu : WebControl
{
....
public override void RenderControl(HtmlTextWriter output)
{
if (!this.HasControls())
throw new Exception("Controls are empty");
....
}
}

View 1 Replies

Using Code Blocks In Head Content And Link Tags?

Feb 1, 2010

I'm building an asp.net app using themes, and I've assigned a theme to the app using the web config.

I have a bookmark icon that I want to use for my page and it is located in the themes directory, but I am having trouble referencing the themes location from a link tag in my header.

First I tried putting a code block inside the link tags href element, which did not work. Instead all it did was html encode the <% characters and output it directly to the browser:

<link rel="shortcut icon" href="/App_Themes/<%=Page.Theme %>/images/bookmark.ico" type="image/x-icon" runat="server"/>

I am able to put a code block inside an element in an hr tag though, so I don't know why it won't work in a link tag:

<hr test="<%=Page.Theme %>"/>

Then I tried doing a Response.Write inside the head tag, but I got an error saying the Controls collection cannot be modified because the control contains code blocks:

<% Response.Write("<link rel="shortcut icon" href="/App_Themes/" + Page.Theme + "/images/bookmark.ico" type="image/x-icon"/>"); %>

I also tried it just with a string literal, and got the same error:

<%= "<link rel="shortcut icon" href="/App_Themes/" + Page.StyleSheetTheme + "/images/bookmark.ico" type="image/x-icon"/>" %>

Is there any way to reference something from the themes directory inside the link tag?

I'm trying to do this in both an ASP.NET 2 and an ASP.NET 2 MVC app.

View 3 Replies

How To Set Head Tags In A Function In App_code

Mar 11, 2010

The below function has to be put within my common functions file in app_code folder. how do I do it?

It gives error like this:

Reference to a non-shared member requires an object reference

Public Sub setHeadTags(ByVal title As String, ByVal description As String, ByVal keywords As String)
Dim metaDescription As HtmlMeta = DirectCast(Page.Master.FindControl("metaDescription"), HtmlMeta)
Dim metaKeywords As HtmlMeta = DirectCast(Page.Master.FindControl("metaKeywords"), HtmlMeta)
metaDescription.Attributes.Add("content", "My big content description")
metaKeywords.Attributes.Add("content", "all, are, my, keywords")
Dim pageTitle As HtmlTitle = DirectCast(Page.Master.FindControl("pageTitle"), HtmlTitle)
pageTitle.Text = "Hey hey heY"
End Sub

View 2 Replies

Add A Javascript File To The Page Head From A User Control?

Apr 30, 2010

how can i add a javascript file to the page head from a user control?

View 2 Replies

Server Removes Spacing Between Head Tags

Jun 11, 2010

I haven't been able to find relevant information through searches. I'm very green when it comes to sever side scripting. I have an ASPX page with a standard form. In the head I have meta tags, the title tag, and a link tag neatly ordered on their own lines. However, when viewing the source code after publishing to the server, the spacing between the tags is removed and it looks quite messy. (There are also <style> and <script> tags that follow, but they remain unaffected.)

I realize this has no practical effect on the site itself (in an SEO sense or otherwise). My project manager shows the source code to our clients to educate them on meta tags and page titles. It would help if it wouldn't become jumbled like this. I wonder if this is a common issue and if it's possible to prevent through better coding practices. HTML as authored, with tags separated on their own lines:

HTML Code:

<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Welcome to Lawn Care Waukesha - Cut My Lawn. Cut My Lawn - Lawn Care Services has offered quality lawn cutting, fertilizing, aerating, and much more at affordable pricing since 2002! We currently offer lawn care service to Waukesha, Brookfield, Pewaukee, Menomonee Falls, and surrounding communities." />
<meta name="keywords" content="lawn cutting, lawn mowing, lawn care, fertilizing, aeration, mulching, shrub trimming, lawn mowing, edging, pruning, mulching, weed control, waukesha, Brookfield, Pewaukee, menomonee falls" />
<title>Lawn Care Waukesha — Cut My Lawn, Lawn Care Service</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
HTML after being processed by the sever, with all the tags running together:

HTML Code:
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="description" content="Welcome to Lawn Care Waukesha - Cut My Lawn. Cut My Lawn - Lawn Care Services has offered quality lawn cutting, fertilizing, aerating, and much more at affordable pricing since 2002! We currently offer lawn care service to Waukesha, Brookfield, Pewaukee, Menomonee Falls, and surrounding communities." /><meta name="keywords" content="lawn cutting, lawn mowing, lawn care, fertilizing, aeration, mulching, shrub trimming, lawn mowing, edging, pruning, mulching, weed control, waukesha, Brookfield, Pewaukee, menomonee falls" /><title>
Lawn Care Waukesha — Cut My Lawn, Lawn Care Service
</title><link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />

I'm not sure it's relevant, but here's the script used to send the form (which I didn't write, by the way). It's the final tag inside the page head:

HTML Code:
<script type="" runat="server">
Protected Sub SubmitForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsValid Then Exit Sub
Dim SendResultsTo As String = "email"
Dim smtpMailServer As String = "smtp"
Dim smtpUsername As String = "email"
Dim MailSubject As String = "subject"
Try
Dim txtQ As TextBox = Me.FormContent.FindControl("TextBoxQ")
If txtQ IsNot Nothing Then
Dim ans As String = ViewState("hf1")
If ans.ToLower <> txtQ.Text.ToLower Or ans.ToUpper <> txtQ.Text.ToUpper Then
Me.CutMyLawnForm.ActiveViewIndex = 3.......................

View 5 Replies

Inject Code Into Masterpage / Html Head From A User Control?

Jan 10, 2010

I am struggling with something that I guess should be standard practice really. I have a number of user controls that use some JQuery plugins. I do not really want to link to the extra CSS and JS files from my main masterpage as this would cause extra load to the user the first time they hit the site, (admittedly it would only be the once), so I was just putting them links into the top of the user control. Then I looked at my source HTML, not nice! Even worse for controls that repeat multiple times on a page.

So I was thinking is there a way of injecting them into the Head of the page when they are needed from the User Control. For that matter is there a way of doing it to the footer for JS stuff?

View 4 Replies

Javascript - Custom Handling Of Pages / Tags?

Aug 2, 2010

how one might go about implementing your own custom tags in JS pages served by ASP.NET?

Basically what I am looking to achieve is a way of creating "includes" for my Javascript files by adding some code such as.

//Include Src="MyJavascriptFileLocation.JS"

And then having the ASP.Net handler serve my javascript file but also dynamically append the referenced javascript file to the location where the include is written.

View 2 Replies

Custom Server Controls :: Why Custom Gridview Control Not Render HTML Properties Under Properly In Visual Studio 2005

Mar 5, 2010

Why custom Gridview control not render html properlies under <Columns> properly in Visual Studio 2005?

For example:

[code]....

View 1 Replies

Why Does .NET Render Javascript For A Button When There's A Custom Validator On The Page

Feb 1, 2011

I've got two questions - first of all, why does .net render a javascript onclick event for asp buttons when there's a custom validator on the same page, and secondly, how can I get rid of the javascript?

It works fine when javascript is turned off, so I don't know what the point of it is. Here's a mini example:

<form id="form1" runat="server">
<asp:Button ID="Button1" OnClick="Button1_Click" Text="test" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
OnServerValidate="Stuff_Validate" EnableClientScript="false">
</asp:CustomValidator>
</form>

This will generate the following html for the button:

<input type="submit" name="Button1" value="test" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Button1", "", true, "", "", false, false))" id="Button1" />

Without the custom validator, it's:

<input type="submit" name="Button1" value="test" id="Button1" />

View 2 Replies

Custom Server Controls :: User Control Calls Javascript Code On Load?

Jul 23, 2010

I'm building this small control. It should detect client resolution and set height and width values within two separated HtmlInputHidden controls. But tester property is not getting values froms this control.

[ToolboxData("<{0}:UIControlResolution runat=server></{0}:UIControlResolution>")]
public class UIControlResolution : Control
{
HtmlInputHidden _txtwidth = new HtmlInputHidden();
HtmlInputHidden _txtheight = new HtmlInputHidden();

[Code]....

View 2 Replies

Render - Server Control Emitting Server Side Tags?

Feb 19, 2010

I'm new to writing custom ASP.NET server controls, and I'm encountering the following issue:

I have a control that inherits from System.Web.UI.HtmlControls.HtmlGenericControl. I override the control's Render method, use the HtmlTextWriter to emit some custom HTML (basically a TD tag with some custom attributes), and then call the case class' Render method.

Using the control:

<dc:Header id="header1" runat="Server" DataColumn="MemberNumber" Width="30%">Member Number</dc:Header >

The problem is that when I view my rendered HTML, the server tag is emitted to the client as well (right after the TD tag):

<dc:Header id="ctl00_ContentPlaceHolder_testData1_testData1_header1">Member Number</dc:Header>

How do I prevent this from happening?

View 1 Replies

MVC Custom Routing Long Custom Route Not Clicking In Head?

Jun 5, 2010

I have spent several hours today reading up on doing Custom Routing in ASP.NET MVC. I can understand how to do any type of custom route if it expands from or is similar/smaller than the Default Route. However, I am trying figure out how to do a route similar to:

/Language/{LanguageID}/Question/{QuestionID}/

And what I would like, too, is similar to how SO works. Something like:

/Language/{LanguageID}/Arabic/Question/{QuestionID}/Some-Question-Title

Where "Arabic" and "Some-Question-Title" can be almost anything because what really matters is the ID's

A complete url example might be

[URL]

Am I going beyond what can be done with the extended URL past the language ID?

View 1 Replies

C# - Render Multiple Control Collections In Custom Control?

Mar 15, 2010

I've build a custom WebControl, which has the following structure:

<gws:ModalBox ID="ModalBox1" HeaderText="Title" runat="server">
<Contents>
<asp:Label ID="KeywordLabel" AssociatedControlID="KeywordTextBox" runat="server">Keyword: </asp:Label><br />
<asp:TextBox ID="KeywordTextBox" Text="" runat="server" />

[Code]....

However it seems to render properly, it doesn't work anymore if I add some asp.net labels and input controls inside the property (see above asp.net code). I'll get the HttpException:

Unable to find control with id 'KeywordTextBox' that is associated with the Label 'KeywordLabel'.

Somewhat understandable, because the label appears before the textbox in the controlcollection. However, with default asp.net controls it does work, so why doesn't this work? What am I doing wrong? Is it even possible to have two control collections in one control? Should I render it differently?

View 2 Replies

Remove All Html Tags And Javascript Tags Using Regex In Javascript

Jan 31, 2011

How can I remove all html tags and script tags? consider also short tags like unclosed tags

<script>blah...</script>
<body> aaa<b>bbb</body>

This should return aaa bbb.

notice that all the contents inside the script tag is ignored.

View 3 Replies

Custom Server Controls :: Trying To Render Control(ComboBox) Programmatically

Feb 18, 2010

I am trying to render control(ComboBox) programmatically, and i am getting error that tell me that i have to add ScriptManager to the page.

But, i did that, and still its not working:

[Code]....

And the render control html is:

protected override void Render( System.Web.UI.HtmlTextWriter writer )

View 3 Replies

Why Does URLs Get Encoded In <link> Tags Within <head Runat="server"> And How To Avoid It

Jan 4, 2011

(I have tested this with a vanilla asp.net site running from the webdev server and it is a problem here also):

I have the following markup in my .master file

[code]....

Clearly Asp.Net does something to encode the url. As it happens, I really need the head tag to be runat="server" and I would also like to be able to have "&" in link-urls within it is there some trick I can use to have my cake and eat it too?

View 2 Replies

Custom Server Controls :: Composite Control Designer / In The Html View, Tags Are Not Updated?

Jan 22, 2010

I'm trying to develope a collapsible panel control with designer.

I have the following two classes:

[Code]....

and this one:

[Code]....

In fact, the designer class is a copy of a sample class in MSDN and I only tried to customize it to work for me. I'm not sure if all methods are necessary or not.

I have the following two questions:

1- When I use this contrl in design mode, I can add or delete controls to it, in the html view, tags are not updated. so when I close the page and open it again, all changes are lost.

2- I have added this control to the same asp.net project that is using this control. Isn't there any way to have this controil in tool box?

View 5 Replies

Visual Studio :: Remove The <head></head> Tag From And All Information Contained In The Tag

Mar 11, 2010

I have several hundred html files that I want to remove the <head></head> tag from and all information contained in the tag. I am fairly sure I can do this in Visual Studio using the Find and Replace In Files with REGEX.

View 5 Replies

Custom Server Controls :: Add Script Tag Within Head Tag In ServerControl?

Nov 7, 2010

I'm gonna use JQuery files in my custom ServerControl , So I inserted jquery-1.4.3.min.js file into the Solution in JavaScriptFiles Folder. Now, I have to add below line within Head tag whenever a user use this ServerControl.

<script type="text/javascript" src="JavaScriptFiles/jquery-1.4.3.min.js"></script>

How can I do it in ServerControl with C#

View 4 Replies

Web Form Render Engine Outputs A Control Tree / Looking For Info On Render Logic.

Feb 12, 2011

I've been watching a video on Scott Hanselmnn teaching MVC 2 tricks/tips. He mentions how MVC 2 by default uses ASP.NET Web Forms view engine to render the output of the views; he mentions that the web forms view engine is a little slower than it could be for MVC 2 since it generates a control tree and then outputs the HTML to the page (I hope I said that right).

I was wondering what he meant by web forms generating a code tree before outputting the HTML to the page. Does anyone have insight on the view engine of Web forms and the steps of the rendering process works for ASP.NET and MVC2?

View 2 Replies

Creation Of Custom Page With Custom Tags, With Event Handling?

Mar 8, 2010

I am trying out some code which enables me in creating a page with extension .asp2

The tags included will also be custom, something like:

[code]....

I have done the necessary changes so that ASP.NET identifies the extension. I have also kept a mapping of custom tags and asp.net/html tags with myself. With this I am able to render the page on browser. But how can we do event handling(usually done by page postbacks and code) in such a scenario?

View 1 Replies

How To Apply CSS Tags To <li> Tags When Using 4.0 Menu Control

Feb 1, 2011

Is there a way to apply your own CSS tags to <li> tags when using ASP .NET 4.0 menu control? For example, say I have an external style sheet that has tags "class1", "class2", "class3", and etc. I want to apply "class1" to the first menu item. Then if menu item is selected, I want to apply "class2" and if it's not selected, "class3". I played with various 'Static' styles, but just can't get it work. I finally gave up and created my own menu control that extends ASP .NET menu control and provides my own custom rendering. This works, but I'm wondering if there is a way to get it work with built-in menu control.

View 1 Replies

Javascript - Execute Some Custom Code Before AJAX Request Is Sent

May 10, 2010

I have a drop down in an ASP.NET page. Whenever the value of the drop down changes an ASP.NET AJAX request is made to the server. I also attached a jQuery "change" event handler to that list to execute some code when the value is changed. So, probably two different event handlers are being attached to the same drop down, and it's causing some problems, i.e., sometimes wrong drop down values are sent to the server. I don't know why is this happening but I think attaching two different event handlers to a same drop down may be the reason.

Can anyone tell me what is the problem here? If what I guessed is true, then is there any other way to execute some custom javascript code before asp.net AJAX request is sent ?

View 1 Replies

Custom Server Controls :: How To Use A Custom Server Control That Is Defined In Inline Code In The Same Page

Oct 22, 2010

I have to use inline code for an aspx page and I need to use a custom server control that is defined in the same aspx page but the control does not get processed as a server control. it gets returned as is as static html tag.

View 3 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved