I want my codebehind to dump a very large set of JS into the ASPX page. This is required, as I can't use external JS code for this component, and the code is also unique to each customer. Is it possible to do this from codebehind? I know how to set the value of text boxes etc. (.Text/.Value = xxx) but I can't see how I can just 'dump' code straight onto the page.
have a look at Page.ClientScripts.RegisterStartupScriptBlock or RegisterScriptBlock, these method do exactly what you need.
Try using RegisterClientScript:
http://www.codeproject.com/KB/aspnet/Register_Client_Script.aspx
set a string variable into code behind and then add to the page just
<%= variableName %>
where you want your js code to be dumped to
Or if you like you can create a method and do the same thing
<%= methodName() %>
Related
I have a webform called "Home.aspx".
In Home.aspx.cs, I created an array called myArray.
I want to use this array in Home.aspx to generate a word cloud.
something like below:
<script>
$('#wordcloud').jQCloud(myArray,{shape: 'rectangular'});
</script>
Can someone help me in solving this.
First, you don't want to put the C#-generated array onto your aspx page, because that array is a C# object, and javascript needs to be sent as text. You want to put a string output of the array onto the page. So you should create a string as a protected property in your code behind, and put the string of the array into that.
Then, on the front end, you can write the protected property onto the page with <%= myString %>
There are a couple of ways to achieve it.
Let server side code generate the whole JavaScript for you, which
means you will need to construct the JS code as a string in the
code-behind (.aspx.cs page) then use
ClientScriptManager.RegisterStartupScript method to register it so
that JS code will be executed when page loads.
Use server side script tag on your ASPX page. It's like point 1 but you write server side code on your front page.
Apply JS serialization and let the serialiser generate the JS array for you.
Below is an example of using a serialization approach.
<%
// create you array here
var myArray= new string[]{"apple", "orange"};
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
%>
Then what you need is to place above code on your ASPX page, before your JavaScript code block.
<script>
var myJsArray = <%= serializer.Serialize(myArray) %>;
$('#wordcloud').jQCloud(myJsArray, {shape: 'rectangular'});
</script>
One thing to remember though, the server side array object and client side javascript array are two different things. The serialization/deserialization is required to allow data/array of data to be passed between browser and web server.
In code behind page I create a variable like this (it belongs to one of the class)
string login_status = "you are not logged in";
I want to show this variable value in my Default.aspx page. What do I do?
You can create a variable of type string with protected access modifier and call it on your web page using:
<%= login_status %>
Drop a label onto your aspx page:
<asp:Label ID="Label1" runat="server"></asp:Label>
then in the codebehind, say:
Label1.Text = login_status;
Add a label control onto the default.aspx page. It will automatically be named Label1.
From your code behind you can place your string into the label.
Label1.Text = login_status
http://haacked.com/archive/2007/02/15/asp.net_tip_-_use_the_label_control_correctly.aspx
you can use asp.Literal control instead of Label.
You should add a control to the page (label, literal) that will display this nicely and set it's value in the code behind.
You can also user runat="server" on any html tag to set it's inner html (etc) in the code behind.
Finally if you want to do it the 'quick' way you can put <%=login_status%> in the markup.
There are multiple ways you can do
First Variables you trying to access should be protected or public
Then you can access these variables like
<% Response.Write(login_status); %>
or
<%= login_status%>
or you can use control like asp control like labels and change its Text property on code behind
So I was told the following would not be possible:
I have a aspx page that has a number of dropdowns. Each dropdown is set with either a class=leftcolumn or class=rightcolumn.
I need to assign an attribute:
propertyID.Attributes["nameOfAttribute"] = "false";
But instead of manually writing out each controlID and setting its attribute with the line above, I had hoped there was a way to go through and set the attribute on each control ID if it had class=leftcolumn.
This is something I know is possible to do through JQuery easily, but I need it on the code behind during load.
Thanks,
I'm not familiar enough with JQuery to write a sample of it, but you could always register a startup script in your code behind to execute when the page loads.
string myJQueryString = ; //some jquery script to set your variables
this.ClientScript.RegisterStartupScript(typeof(MyPage), "key", myJQueryString);
As Steve said you can do it easily with JQuery javascript library
first your should add a refrence of this library to you aspx pages then
$("select[class=leftcolumn]").each(function(index, value) {
// You are selecting all of the dropdowns with the class attribute equal to (leftcolumn)
$(value).attr("yourCustomAttribute") = someValue;
});
I guess I didn't specify clearly in my question so I am answering my own so I can ask again in a completely new thread - but I want to do this in C# in the code behind, not using JQuery.
I'm posting data to a page called process.aspx that handles some business logic with the following code:
<%# Page Language="C#" %>
<%
MyData.process(Request);
Response.Redirect("")
%>
this page I will be calling from a variety of pages. Is there some way of knowing from which page my form was submitted? I was thinking something along the lines of writing:
<form id="frmSystem" method="post" action="process.aspx?page=<%= %>">
However I don't know what to write in between the <%= %> to get the current page name. Can anyone help please?
You can capture the calling page URL and hold it in Session or ViewState for later use.
For example, in Page_Load,
Session["PreviousPage"] = Request.Url.ToString();
And then in your final event (perhaps Savebutton_Click or CloseButton_Click), you can do a redirect in either of these ways:
Server.Transfer(Session["PreviousPage"].ToString(), false);
or
Response.Redirect(Session["PreviousPage"].ToString(), false);
You can also get the URL of the calling page this way:
Request.ServerVariables("HTTP_REFERER")
You could pass in it in via a property like the ReturnUrl similar how a sign in page works. This is kind of how you are doing it up there.
You could also try to use the HttpContext.Current.Request.UrlReferrer to see who referred you.
You can use Request.UrlReferrer.OriginalString to get the URI of the referring page.
I need to have a button on the master page.
Once that button is clicked I generate a string that represents URL.
test.apx is a content page I use and the string will look like something like this:
Example:
www.blah.com/test.aspx?user=blax&develop=extreme_all
Now all I need is to reload the page while content is redirected to the URL I generated.
I hope this makes more sense.
Thanks guys I am new to asp.net and really appreciate any help
Why dont you use Update Panel?
Have the page postback with the updated query string to change what is in your content area
Assuming your masterpage is set up correctly
within the <asp:content> tag of your aspx page that is using the masterpage you created add code to get the query string
Request.QueryString["key"]
example url: http://www.whatever.com?foo=bar&bar=foo
string tmp = Request.QueryString["foo"]
tmp will become "bar"
Now just check the "postback" option of the asp:control you're using to reload the content page or do whatever you to make the page refresh.
If I understand your question correctly, you want to reuse the same code to parse out your user and develop variables from different content pages that use the same master page.
It sounds like you need a strongly typed master page.
First, put your shared code in your master page. Then, expose the parsed data as properties of the master page. Next, simply add the following directive in your content pages:
<%# MasterType VirtualPath="~/mymasterpage.master" %>
Finally, in your content pages, you can reference your properties as such (assuming you created a property called MyUser):
string user = this.Master.MyUser;
You can also use inheritance if you want a different approach. Simply create class that inherits from Page. Then put your shared code in that class. Finally, make your content pages inherit from your new class, instead of Page.