asp.net mvc c# javascript web.config - asp.net

i want to have retrieve a "imagetype" from appsettings in my web.config in javascript . how can i do that?

You can use following code in your page markup:
<script language="JavaScript" type="text/javascript">
var type = '<%= ConfigurationManager.AppSettings["imagetype"] %>';
</script>

Use the following:
var value = System.Configuration.ConfigurationManager.AppSettings["imagetype"];
You may find that for it to work you need to add a reference to System.Configuration.dll if you don't already have one.
Create a new page, and in Page_Load put the line so that it all reads:
Response.Clear();
var value = System.Configuration.ConfigurationManager.AppSettings["imagetype"];
Response.Write(value);
Response.End();
You can now make an AJAX call to the page from Javascript, perhaps using ExtJs and the text will be returned to your javascript.
Alternatively, you could put the following into your page:
<script language="javascript" type="text/javascript">
var appSettingValue = '<%=System.Configuration.ConfigurationManager.AppSettings["imagetype"]%>';
// The variable "appSettingValue" will contain the string from your web.config
alert(appSettingValue);
</script>

Related

Create javaScript variable in code behind of asp.net

How do I register a Javascript variable on the server side (backend) and access it on the client side (Javascript file), without a hidden field, Literal, etc.?
You can use the RegisterClientScriptBlock-Function from the Page's ClientScriptManager.
Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "initMyClientVariable", "var myClientVariable=null;", True)
EDIT: according to your new informations, that you want to register a client array, use ClientScriptManager's RegisterArrayDeclaration Method.
VB.Net example:
Dim myArrayValue As String = """1"", ""2"", ""text"""
Page.ClientScript.RegisterArrayDeclaration("myClientArray", myArrayValue)
According to the new information in my comments that you need access to that variable from an external js-file: you should pass the js-array as argument to the function in the js-file. For example:
callFunctionInJsFile(checkBoxes);
You can put the following code in .aspx file ...
<script type="text/javascript" >
var date1 = "<%: DateTime.Now %>";
var date2 = "<%= DateTime.Now %>";
</script>
<%: %> works under ASP.NET 4
You can put a literal in the xml portion of the code and assign that literal some text:
myLiteral.Text = "<script language=\"javascript\">var myVar = 24;</script>";
This makes myVar globally available on the client side once it's rendered. You can also use the ClientScriptManager object to use Asp.Net to inject scripts and variables.
First place an <asp:Literal ID="Literal1" runat="server"></asp:Literal> tag in the <head> of your .aspx file. Then in the server side code in your .aspx.cs file, do something like Literal1.Text = "<script type=\"text/javascript\">var timer = 3600</script>" and you've got yout javascript variable called timer.
That's it. Have fun!

Implement javascript sorting in a asp.net grid view

How can I accomplish gridview sorting in client browser using javascript ? without using inbuilt gridview sorting method. I really dont want the gridview to go to the DB each time while sorting.
Try the jQuery plugin tablesorter
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
....
<script type="text/javascript">
var aspxElements = {
theGrid: '<%= myGrid.ClientID %>' //I'm not entierly sure this is the id of the table or some container element
};
$(document).ready(function() {
$('#' + aspxElements.theGrid).tablesorter();
});
</script>
(modified from the demo on this page)
Note that this will get weird if you're using pagination.

Asp.Net script in external js file

How can I use server-side script in an external js file to grab a reference to a dom element?
The following works as intended when used as inline-script, but is returning null when I move this to an external js file.
$("#<%= gridResults.ClientID %>");
You'll need to have an inline script block that creates a JavaScript variable. This block should be added before your external JavaScript file. Once you do this, you can reference that variable in your external JavaScript file.
<script type="text/javascript">
var grid = $("#<%= gridResults.ClientID %>");
</script>
<script type="text/javascript" src="path/to/my.js"></script>
If you but a unique class on the grid using the CssClass property, you should be able to access the grid without having to know what it's clientID is.
You can't put #<%= gridResults.ClientID %>, because the gridresults.ClientID is specific to that asp.net page.
You could do:
<stript src="yourfile" type="text/javascript"> <!--link to external js file-->
<script type="text/javascript">
var grid = $("#<%= gridResults.ClientID %>");
yourfunction (grid);
</script>
What you want to happen cannot. The external javascript file is not parsed by the ASP.NET page's code behind, so the functionality of ASP.NET is not available to it.

<%# server tags in jquery

I am very new to jQuery and have got a quick question.
I wish to use my server side classes in my jQuery code, something similar to this:
$(document).ready(function() {
var temp = <%# myClass.Id %>;
})
Is this possible? if so, how?
Thank you very much
This is the later question I refined my former question to:
I'm sorry, I think I didn't explain myself too well... I've got a class name User. It's a class I built in my business logic.
I've got a web page named UserProfile, inside it I've got the following property exposing the current logged in user:
public BL.User CurrUser { get { return (BL.User)Session["currUser"]; } }I want to be able to access this User class from my aspx page using Jquery. How do I do that?
The databinding syntax
<%# MyStaticClass.MyProperty %>
will only work if you call DataBind on the container (page). What you're after is most likely the following syntax:
<%= MyStaticClass.MyProperty %>
which will also give you access to you page / control members
<%= this.MyPageProperty %>
As was already mentioned you should really assign those values to java script variables and pass those variables to you JS functions.
This will only work if your javascript is embedded in your source files (e.g. the .aspx files):
<script type="text/javascript">
var id = <%# myClass.Id %>; // store as raw value
var id_string = '<%# myClass.Id %>'; // store in a string
</script>
As others have said, if the JavaScript is in your aspx page, then using server tags will work fine.
If you have your jQuery in an external script file, then you could put this in your aspx page
<script type="text/javascript">
var myClass = $('#<%= myClass.ClientID %>');
</script>
and then use the variable in your external script file
$(function() {
myClass.click( function() { ... });
});
For other options take a look at this question and answer - How to stop ASP.NET from changing ids in order to use jQuery

Javascript to access control in MasterPage

I have a textbox control Super1 in my MasterPage.
I am using javascript to access this control from my content page like this:
<asp:Content ID="ContentPage" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function Somethin() {
{
document.forms[0].elements['Super1'].value = "sdfsd";
//document.getElementById('<%=Super1.ClientID%>').value = "sdfsdf";
}
}
</script>
</asp:Content>
But while page load it says Super1 not found. How can I access Super1?
In your masterpage's onload add this code :
string script = #"<script>
function Somethin() {
document.getElementById('" + Super1.ClientID + #"').value = 'sdfsd';
}
Somethin();
</script>";
if (!Page.ClientScript.IsClientScriptBlockRegistered("somethin_script_block"))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "somethin_script_block", script);
}
this will add your script to the end of the page.
EDIT : I just realized, you use your controls ID directly in your javascript code. this may cause the exception. I update your code to fix it.
I hope this helps.
You have to make sure the document has loaded, make sure to call your functions that rely on the DOM being loaded onload. E.g.:
<script type="text/javascript">
window.onload = function() {
Somethin();
}
</script>
From the sample code you posted and since you said you are using a control, check the rendered id of the control you are trying to get at. In my experience the name is something crazy like ctl100_masterpagename_namingcontainer_controlname... that needs to show up in the js as well.
Super1 might be in a different naming container (the masterpage's control collection). You either need to render out the clientid of the control in a global javascript variable during the masterpage rendering so it can be accessed by javascript in the child page or you need to get a reference to the Masterpage, find the control there and write out the client Id in your child pages javascript...
Something like...
if the text box is in its own content place holder
var txtSuper1 = Master.FindControl("ContentPlaceHolderName").FindControl("Super1") as Textbox;
or if its not in a content place holder
var txtSuper1 = Master.FindControl("Super1") as Textbox;
3rd option might be to expose the control as a property of the masterpage (not sure) - my webforms is rusty.
On the master page, declare a javascript variable for the control, e.g:
<asp:TextBox id="Super1" runat="server"/>
...
<script type="text/javascript">
var txtSuper1 = document.getElementById('<%= Super1.ClientID %>');
</script>
It's important that you use the ClientID property, because the rendered control's ID (on the client) will be different from the server control's ID (due to naming containers).
Now you can access the textbox from javascript declared in the content pages:
<asp:Content ID="ContentPage" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function Somethin()
{
txtSuper1.value = "sdfsd";
}
</script>
click me
</asp:Content>
BTW: in your code there are duplicate curly-braces in function Somethin() {{ ... }}

Resources