|
You already posted this in the C# forum; please post in one forum only.
I must get a clever new signature for 2011.
|
|
|
|
|
Hi All,
Is there a way to read the Timeout value from the web.config file within JavaScript?
Thank you,
Mel
|
|
|
|
|
The web.config file is restricted by the ASP.NET engine and is not available from client-side script or from directly browsing for it.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Thank you for your reply Mark.
Should I therefore be able to store the timeout in to a C# variable and then access that variable in JavaScript?
I'm unable to carryout the following at the moment:
1. Access the timeout value from the web.config file in C#
2. Access a C# variable in JavaScript.
Any ideas on how I can do this?
For #2 I thought the following shoudl work:
C# code:
public string strTest = "Test String";
JavaScript:
alert('<%=strTest%>');
However, the alert box prints out the text "<%=strTest%>".
Any ideas?
Thank you
|
|
|
|
|
MWRivera wrote: However, the alert box prints out the text "<%=strTest%>".
That is correct. You are wrapping it in single quotes thus making it a string literal.
Try this. Assuming it is in your aspx file and not a separate javascript file.
alert(<%=strTest%>);
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
It is a separate JavaScript file and so when removing the single quotes I get a syntax error when the page loads.
Anyway I can reference this variable from a separate JavaScript file?
Thank you
|
|
|
|
|
As it's a separate JavaScript file, it's not rendered out by the ASP.NET pipeline, so the rewrite tag doesn't work. You could, put the value into a page level variable and use that as the basis.
|
|
|
|
|
Hi Pete,
Thank you for your reply. Have you got an example of JavaScript accessing a page level variable, that you could share?
Thank you
|
|
|
|
|
In your aspx, just declare your javascript variable and assign a value to it. As the other javascript file runs inside the same page instance, it can access the variable with ease.
<script language="javascript" type="text/javascript">
var myvalue = <%= DateTime.Now %>
</script> Then, in your javascript file you can do
alert(myvalue);
|
|
|
|
|
Thanks Pete, I have just worked it out the same way and then seen your post.
Amazing thanks.
Just a few notes though, even though it works fine I get a warning saying (using your variable names):
"Error undating JScript IntelliSense: myJavaFile.js: 'myvalue' is undefined"
Any ideas why I would be getting this warning? I understand that myvalue isn't defined in the .js file but it's still accessable, so not sure why the warning would appear.
Thanks again.
|
|
|
|
|
I have found a solution to the first part of my problem, retrieving the timeout value from the Web.config file. I have included the code below in case anyone else runs into this issue:
string configPath = "/VirtualDir";
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
System.Web.Configuration.SessionStateSection configSection = (System.Web.Configuration.SessionStateSection)config.GetSection("system.web/sessionState");
Response.Write("Session timeout is: " + configSection.Timeout);
Any ideas on how I can make a C# variable accessible in a separate JavaScript file?
Thank you
|
|
|
|
|
Use Session.Timeout to find out the session timeout in minutes. Use a hidden field to exchange data between code behind and JavaScript.
|
|
|
|
|
Hello,
I am implementing publisher/subscriber pattern with the use of .NET Remoting.For this i don't want to take URI's from configuration file every time a new subscriber want to subscribe to publisher, As physically there's only a single process that will behave as subscriber. But when publisher want to publish the content to subscriber, it will search for its URI from its data structures.
e.g
let say there are 4 seperate processes having instances(XYZ_instance1,XYZ_instance2,XYZ_instance3,XYZ_instance4) launched of "XYZ.exe". Now publisher want's to publish the contents on XYZ_instance3(XYZ_instance3 would have its different uri).For this every time XYZ's instance is subscribing it must carry its unique URI, which will be added in publisher's data structure, describing about its identity as uri. Whenever some data needs to publish, publisher will use this URI.
The problem is every time a new instance is launching and subscribing, it will read its uri from app.config, which will be same all the time new instance of XYZ is started(as its a same process and will access the same app.config, which's just having single uri). FOR this i have to generate a unique URI every time, instance is started, and that URI whould be known by Remoting envirnment.
|
|
|
|
|
Hello experts,
how is it possible to create a class with a "tag" of any type that is still type-safe at runtime? That's what generics are for, I suppose.
Instances of that class need to have a reference to another instance of that class.
But then, the referenced instance should be allowed to host a tag of different type than the referencing instance.
I tried to build that in C#:
using System;
using System.Collections.Generic;
using System.Text;
namespace Test_Foo
{
public class Foo<T>
{
public T Tag;
public Foo<T> _innerFoo;
}
public class Bar
{
public void Baz()
{
Foo<int> intFoo = new Foo<int>();
intFoo.Tag = 5;
intFoo._innerFoo = new Foo<double>();
intFoo._innerFoo.Tag = 1.25D;
}
}
}
The marked declaration is where the linked class should go. What would possibly go between < and >?
The line marked as error-causing causes an error due to double not being implicitly convertible to int. This, I don't want to do anyway. I do want to have a double there.
How can the inner instance have a differently-typed tag than the outer one?
Ciao,
luker
|
|
|
|
|
Since your first instance of Foo<T> was made with an int the _innerFoo also is of type Foo<int>. The generic declaration of the class binds T for the whole scope of the class and thus also the definition of _innerFoo.
Cheers!
|
|
|
|
|
Interesting little problem. You can't do it the way you've written because once you declare what T is (an int), the same T is used throughout the entire class.
AFAICT, InnerFoo would have to be declared as a seperate class using it's own type place holder. Something like this:
public class Foo<T>
{
public T Tag;
public InnerFoo<S> _innerFoo;
public class InnerFoo<S>
{
public S Tag;
}
}
class Program
{
static void Main(string[] args)
{
Foo<int> intFoo = new Foo<int>();
intFoo.Tag = 5;
intFoo._innerFoo = new Foo<int>.InnerFoo<double>();
intFoo._innerFoo.Tag = 1.25D;
}
}
|
|
|
|
|
public class Foo<T>
{
public T Tag;
public class InnerFoo<S> : Foo<S>
{
}
public InnerFoo<S> _innerFoo;
}
Declaration of the InnerFoo class with its own tag type S seems to work.
But declaration of _innerFoo (marked line) fails due to S being not known yet. Declaring _innerFoo without a type parameter doesn't work either.
Is there another way to declare _innerFoo without knowing what type it is going to encapsulate?
Ciao,
luker
|
|
|
|
|
I tried that code on .NET 4.0 and it worked perfectly.
Since this method can only go one level deep, it's not going to work for you anyway.
Off the top of my head, the only other way I can find that would allow this removes all type checking on InnerFoo and introduces a performance penalty. That would be to declare InnerFoo As Object.
It appears as though you're putting together a chain of values of different types. You might want to look into a LinkedList to see if that helps.
|
|
|
|
|
Maybe it's a limitation of .NET 2.0, which I use. From what I read, it's the latest .NET version running on Windows 2000, which is mandatory for this project.
But the two-level limit should not be a problem for this design. Since InnerFoo<S> is derived from Foo<S>, it again should contain an inner foo class of different tag type.
Removing all type checking would equal my current design using object as tag type.
The linking is just one aspect of this class. LinkedList therefore doesn't serve its other needs.
However, I'm going to try the .NET 4.0 approach. Thanks for the hint.
Ciao,
luker
|
|
|
|
|
My bad - it doesn't work. I went back and replicated this and found that I did something wrong and it gave me the illusion that it worked!
I can't get this to work using Generics, but it does work like this:
namespace ClassLibrary1
{
public class Foo
{
public Type valueType;
public Object Tag;
public Foo innerFoo;
}
class Program
{
static void Main(string[] args)
{
Foo intFoo = new Foo();
intFoo.valueType = typeof(int);
intFoo.Tag = 5;
intFoo.innerFoo = new Foo();
intFoo.innerFoo.valueType = typeof(double);
intFoo.innerFoo.Tag = 1.25D;
}
}
}
Obviously, since Object is being used to store any type, there is a performance penalty to unbox the type and get the correct value.
|
|
|
|
|
Inherently you can't use generics like this. As you are aware, the class is strongly-typed, so _innerFoo is bound to the type of the class, so sorry but there is no way to do this.
|
|
|
|
|
Hi,
this seems to do what you want, somewhat:
namespace Test_Foo {
public class Foo<T, T2> {
public T Tag;
public Foo<T2, T2> _innerFoo;
}
public class Bar {
public void Baz() {
Foo<int, double> intFoo = new Foo<int, double>();
intFoo.Tag = 5;
intFoo._innerFoo = new Foo<double, double>();
intFoo._innerFoo.Tag = 1.25D;
}
}
}
However I don't know what purpose nesting those Foo's would serve as the inner Foo has its own inner Foo, etc.
My approach only works well for a nesting depth of 2, if your inner inner foo needs yet another type, you need to add another type parameter, etc.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Since the nesting depth is potentially limited only by available memory, and the sequence of tag types, if used this way, would have to be known at declaration time of the outermost foo, this approach seems not suitable yet.
For the purpose of nesting Foo's: It's an attempt to merge method return value and possible error handling without the performance impact of exception handling in case of an error.
And I personally don't like out-parameters too much.
Ciao,
luker
|
|
|
|
|
Even if technically possible, what's the logical need of accomplishing it?
Generics do allow specific types to be used, but this need is more like having a tree with each branch capable of bearing a different fruit
Code can solve a problem identified; but first identify the problem itself
|
|
|
|
|
As I said in the post you repied to, it's to combine result value and error handling.
The tree you mention is a good analogy to what I'm trying to create: there is a task to do with several sub-tasks which themselves have sub-sub-tasks. Each of those could fail and therefore return an error. Each could as well return some result just as expected.
The tree structure is built of error messages, including "There was no error. Everything went fine". Besides the error, especially in those "Everything fine" cases, there shall return a result value. It is of type object at the moment. I would like to have it type-safe. Therefore the need to tell the error object what type the nested return value should have.
Ciao,
luker
|
|
|
|