I have a webpage that I would like to change it copyright text.
The problem is I don't want to compile the whole website.
So in the page.aspx we have:
<div id="CopyrightDIV">
<span id="copyright" runat="server"></span>
</div>
The code behind sets the text on pageload.
How can I set the page.aspx so that it will show the text I set on it and not the text that is given by the page.cs?
Thanks
Since the final HTML will be rendered to the browser, we can always manipulate that, you can change the text in JavaScript like this:-
<script type="text/javascript">
$(function () {
$('#<%=copyright.ClientId%>').text("Ranch");
});
</script>
Considering you have imported JQuery library.
Ok, how about pasting a script block directly inline into the page? That way you can hook into the page lifecycle and change the span text at the last possible moment?
<script language="c#" runat="server">
public void Page_Render(object sender, EventArgs e)
{
((HtmlGenericControl)this.Page.FindControl("copyright")).InnerText = "Your text here";
}
</script>
Here's a rundown of the page lifecycle model. So long as the span was originally being set in the page load or init then you should be able to override this at a latter stage.
http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.85%29.aspx
Related
i'm quite new developing for asp.net and found a problem i can't figure it out.
My .aspx look like the follow:
<form action="#" class="" runat="server" >
<asp:DropDownList ID="ddlCombo" runat="server">
</asp:DropDownList>
</form>
<div class="clsbox-1" runat="server">
<div class="dropzone clsbox" id="mydropzone">
</div>
</div>
As you can see i have a dropdownlist that i will populate on page_load and a dropzone inside a div (i need to be this way).
<script>
Dropzone.autoDiscover = false;
$("#mydropzone").dropzone({
url: "WebForm1.aspx?param=1",
addRemoveLinks: true,
maxFilesize: 0.5,
dictDefaultMessage: 'drag and drop',
dictResponseError: 'Error uploading file!'
});
<script>
this is the script that make div like a dropzone, i'm not sure if the url is right though.
The problems comes when in the page_load part, since i need to populate the dropdownlist on page load,
when i try to upload a file via dropzone it will be sent as postback and enter in the 'if' and populate again the combobox, reseting the value that was selected.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
loadCombo(); //populate ddlCombo
}
string aux = ddlCombo.SelectedValue.ToString();//this will have the first item
//processing file...
}
Please let me know if there is any fancy way to do this because i can't figure it out!
EDIT:
I don't understand why if i send within a form it work as intended:
<form action="#" class="dropzone" runat="server" >
<asp:DropDownList ID="ddlCombo" runat="server">
</asp:DropDownList>
<div class="clsbox-1 " id="mydropzone" runat="server">
<div class="clsbox" >
</div>
</div>
</form>
When i upload a file here it doesn't send as post back.
Thx you!
Your dropzone url parameter should be to a different page/httphandler/controller you have created to accept the upload and save it to a file (or whatever you want to do with it). You could technically use the page you're currently on, but it'd be a little trickier in your situation as you'd need to detect what caused the postback and handle appropriately (ie don't reset the dropdown).
I would recommend using an HttpHandler instead of a normal page for receiving the files. Here's an example of one.
How do I execute Javascript after submission of an ASP.NET form?
For example, if I got a submit button click after submission of page then I want to display a DIV which I have hidden on page.
Since you are using a full postback, you can easily include the javascript from the code-behind.
Place your JavaScript Code inside a Placeholder
<asp:Placeholder ID="javascriptPlaceholder" runat="server" Visible="false">
<%-- Your Javascript here -->
</asp:Placeholder>
I assume in your OnClick handling method, you are getting the value of the form and do something else with it. In this method you could set the Visibile property of the placeholder:
this.javascriptPlaceholder.Visible = true;
If you would only want to convert the hidden div to a visible one then that can be done on the server side. In the aspx, give the div an id and a runat="server". Then in the server side, set its display style attribute to 'block'. For example, say the div is defined as
<div id="divTest" runat="server" style="display:none;">
This is a div test
</div>
Then in the post event of the submit button you can set its style as given below:
divTest.Style["display"] = "block";
If you only want to emit scripts after the post then using the ScriptManager you could use:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ScriptRegisterTest", "alert('Testing');", true);
Maybe I'm not understanding your question, but if you're going back to the server and doing some processing, why do you need javascript? Just add a runat="server" to the div in question and make it visible after the processing.
<div id="YourDiv" runat="server" Visible="false" >
...Whatever is here
</div>
Code Behind:
protected void Button_Click(object sender, EventArgs e)
{
/*
Your Logic Here
*/
YourDiv.Visible = true;
}
I'm looking for a solution to my problem for two days and I didn't find anything that can help. I have two page : page 1 and page 2
Page1 contains parameters that will be send to page2 and a button "Create"
Page2 contains the GridView and on page_load I have
GetParameters() ; // extract parameters
PrepareParameters(); // prepare local variable
DataTable dt = CreateDataTable(); // create the BoundField/Columns based on prepared local variables
myGridView.DataSource = dt;
myGridView.DataBind();
My problem is that the method PrepareParameters() takes time due to the huge amount of data even though it's optimized. When I click on "Create" button the browser doesn't move to page2 but instead it doesn't respond and stay in page1 and the user does not know what happens.
So I'm wondering how to redirect the browser to page2 and show to the user a loading animation on the grid place until the PrepareParameters() finish processing data and display the GridView once finished?
Thanks for your reply.
You can use Javascript to do that. Get any loader image you prefer, there are many available on net, google it
put this image top/bottom of grid, assign some id to it and set default property Visible = false.
Now on button click call javascript where you make this image visible.
Sample code
<html>
<head>
<script type="text/javascript" language="JavaScript">
function Loading() {
document.getelemanebyId('Img').visibility = visible;
}
</script>
</head>
<body>
<form action="" method="POST" id="myForm">
//Gridview control here
<img id="Img" src="give image file path" runat="server" visible="false"/>
<button name="Create" onClientClick="Loading();" runat="server" >
</form>
</body>
</html>
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
I am refactoring a page that uses <body onload="myJS();"> to a user control. I understand I can do it using server side script registration on load.
If I want to do it on the client side (ascx), how would I do this?
Taken from http://snipplr.com/view/3116/cross-browser-add-event-listener/
// Cross-browser implementation of element.addEventListener()
function addEvent(evnt, elem, func) {
if (elem.addEventListener) // W3C DOM
elem.addEventListener(evnt,func,false);
else if (elem.attachEvent) { // IE DOM
var r = elem.attachEvent("on"+evnt, func);
return r;
}
Use it like this: addEvent(window, your_onload_handler). Or you could just use jquery for this and a lot of other things.
Romme's client-side approach is best. But just in case somebody wanted to know how to do it using server-side code in the ASCX file, stick this in at the top of your template:
<script runat="server">
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
string strScript = "myJS();";
Page.ClientScript.RegisterStartupScript(this.GetType(), "OnLoadScript_" + this.ClientID, strScript, true);
}
</script>
OnLoad is generally a bad way to initialize JS, because it is only fired when all the resources have finished loading (including any images in the page).
The simplest, and most reliable way to call Javascript that you want to execute when the page finishes loading is to put an inline <script> block into the bottom of your HTML, right before the closing </body> tag:
<body>
... your page here ...
<script>myJS();</script>
</body>
</html>