Hi I have a usercontrol which includes some JavaScript, if I add the control to a standard web page I can start the JavaScript in the body tag, like this
<body onLoad="Start()">
The problem is that I need to add the control to a webpage which is inside a masterpage, how do I then start the script when a page inside a masterpage doesn't have a body tag.
You can register it using:
Page.ClientScript.RegisterStartupScript()
For your case, it would be this:
Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "Start()", true);
Include jQuery and put your initialization code inside $(document).ready().
If you want to strictly use .NET functionality, and your page is properly marked up with runat="server", you could use Page.ClientScript.RegisterStartupScript() as well.
You can change the body tag to runat server and give it an id.
<body id="masterBody" runat="server">
Then on page load:
public void Page_Load(Object sender, EventArgs e)
{
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("masterBody");
body.Attributes.Add("onload", "Start()");
}
Try binding to the DOM document.ready event to load your content after the document has completed rendering
Related
I want set CSS style attributes of some controls (like input text) for example by Request.Form["x"] method in aspx page .cs code ,runat="server" is set for that controls and them is known in intellisense of .cs and them didn't find by form1.FindControl("x") (for some my reason that depend on jQuery issue) but i want set their CSS style in another syntax.
Thanks for any suggestion
Instead of using a client html control, use a server side control with a css class name. Then modify the definition of the CSS class in the code behind.
In your aspx:
<style type="text/css" runat="server" id="htmlCss"></style>
<asp:TextBox ID="txtField" runat="server" CssClass="myCssClass"></asp:TextBox>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
string css = #".myCssClass { background-color:yellow; }";
htmlCss.InnerHtml = css;
}
All the control needs to know is the styles are in "myCssClass". In Page Load you decide what the styles are going to be.
I need to add two relative Jquery files path in my master pages Head section.
i try this for two file but just one of them calling in my "Head" section.i also use this cods in my master page Code Behind but it didn't work:
protected void Page_PreRender(object sender, EventArgs e)
{
string jquery = ResolveClientUrl("~/JQuery/jquery-1.4.4.min.js");
Page.ClientScript.RegisterClientScriptInclude("jquery", jquery);
string jqueryShadow = ResolveClientUrl("~/JQuery/jquery.shadow.js");
Page.ClientScript.RegisterClientScriptInclude("jqueryShadow", jqueryShadow);
}
Also ,when i try this:
<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
i got same error with 1 link!
When i define both of Jquery files in my master page Head section directly, every things works for me!how can i inject my Jquery files(more than one!) with relative paths exactly on the Head section in Master Page?
Any idea?
Regards.
Have you tried the solution proposed (by #Bharath) on the link you provided?
var control = new HtmlGenericControl("script") ;
control.Attributes.Add("type", "text/javascript");
control.Attributes.Add("src", Page.ResolveClientUrl("~/JQuery/jquery-1.4.4.min.js"));
this.Page.Header.Controls.Add(control);
You could put those statements on the Page_Load event of your MasterPage.
Regards,
I desingned a page with tags, Now I want to access object tags in code behind.
This is aspx page code....
<object type="label" runat="server" class="System.Web.UI.WebControls.Label" id="label_priority" parent="-1" bindedfield="priority" empty="1" value="MyValue">
Here I am adding runat=server in object tags it is giving error as
"An object tag must contain a Class, ClassID or ProgID attribute."
then I added class="System.Web.UI.WebControls.Label", now not giving any error but not showing anything in browser.
so My question is how do I access object tags in aspx.cs page?
or
I want to create a label with object tag that is accessible in code behind.
Sujeet
When you have runat="server" on a <object/> or <script/> tag, .NET expects it to be a server side object. You can however create the entire markup on the server side. Assuming you have <div id="somecontainer" runat="server"></div> in your page:
protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl myObject = new HtmlGenericControl();
myObject.TagName = "object";
myObject.Attributes.Add("data", "somevideo.mpeg");
Page.FindControl("somecontainer").Controls.Add(myObject);
}
The result is:
<div id="somecontainer"><object data="somevideo.mpeg"></object></div>
You can use the same method to add elements inside the object, for instance <param/> tags.
You can use below like code in your page load event if you want to access this in page load :
ScriptManager.RegisterStartupScript(Page,Page.GetType(), "Script", "document.getElementById('DIVID').style.display='none'",true);
Here we are changing the display property of the div .you can change according to your requirement.
Hope this will help you.
you can do something like this
<object id="items" class = "System.Collections.ArrayList" runat="server">
as it is run at server now , you can accessing from code behind
how to Bind webpages that's already created with a new master page ?
AT Page_PreInit event you can set master page.
for example
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "Master.master";
}
You will need to change the markup in the .aspx file something along the following lines
1) Remove html head & body
2) Insert ASP:CONTENT tag
3) Insert MasterPageFile attribute in #PAGE directive
To be honest the easiest way might be to just create a new content page, select the masterpage and then copy any markup you need into the panel and copy your codebehind over too.
i've added to the master page my script "myscript.js".
Then, in a content page, i would like to load myscript() at startup (body onload).
How can i do this ?
Either use
Page.RegisterStartupScript
if (!IsStartupScriptRegistered (key))
{
String script = "<script type=\"text/javascript\">" +
"alert('Hello World');</" + "script>";
RegisterStartupScript(key, script);
}
Or you could use the JQuery library and add the function call to the document.ready function
http://jquery.com/
$(document).ready(function(){
// Add your function call here
});
this will add onload client side event to the master page's body tag:
in the master page:
body id="PageBody" runat="server"
in the content page:
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("PageBody");
body.Attributes.Add("onload", "doSomething();");`
I use a ContentPlaceHolder in the master-page for this purpose. This makes it possible to include specific scripts in the <head> only when you want it.
The form has onLoad event. So use the tag in your Masterpage
Assign id and server="runnat" in master page body tag
<body runat="server" id="body_master">
Then your aspx .CS file insert below code in pageLoad event
HtmlGenericControl body = this.Master.FindControl("body_master") as HtmlGenericControl;
body.Attributes.Add("onLoad", "SessionTimeOuts();");
SessionTimeOuts() function should be defined in your .aspx file
Thank you