Output a javascript string to a page using C# .net [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Fairly new to C# and am sure there is an easy to solution to this but am just not seeing it.
Basically I have some javascript stored in a variable that I would only like to output to the page if a certain condition has been met.
This is what I've tried so far:
<%= conditionMet ? rawJavascriptString : "" %>
This however won't work, I'm sure this is a simple one but would appreciate any help. So the question is how can I render a script on the response if some condition is met and omit it otherwise?

Instead of registering the script in the ASPX directly, have a look at the ClientScriptManager class. It provides methods to output JavaScript (or similar client script blocks) programmatically. You can check your condition and use it in the CodeBehind file (e.g. in the Page_Load-event) to output the script:
if (conditionMet)
Page.ClientScript.RegisterClientScriptBlock(GetType(),
"MyKey",
"<script type=\"text/javascript\">alert('Test');</script>", false);
Especially the RegisterClientScriptBlock and the RegisterStartupScript methods might be useful. In order to uniquely identify a script, you provide a type and a key. These can also be used to remove or replace the script.

I think you need to do something like
<span id="content"></span>
<script type="text/javascript">
var rawJavascriptString = 'whatever'
var serverSideValue = '<%= conditionMet %>';
if (serverSideValue ) {
$("span#content").text(rawJavascriptString);
}
</script>
Basically you can get the value of the server-side variable outputted to a JavaScript variable, then use that variable to work out what to set the content to.
Naturally I don't know what your HTML looks like, but the above is a simply example which you can work to suite your needs. Also, overall, I think it's a bit cleaner, but that's a subjective opinion :)

Related

Access web.config appsettings from Angular js 2 Typescript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to angular js 2 type script programming. I am trying to find out a way to access app settings keys of web.config from typescript. I wouldn't want to hardcode the data in my type script as they vary across environments. TIA
Typescript is compiled to JavaScript which later gets executed within the context of the application consuming the web.config file. Because of this, you won't be able to read the configuration at the TypeScript level.
What you need to do instead is make the configuration available at runtime. In other words, whatever code is generated by TypeScript needs to be aware of some object available while the page is running that will feed it any configuration it may need.
There a few ways to do this.
You could make your JS code aware of an object that should always exist in the global context (e.g. appConfig in JS when the page is running). You can then populate this object with the settings. Example asuming Razor:
var appConfig = [];
appConfig.adminEmail = '#ConfigurationManager.AppSettings["adminEmail"]';
Then your TypeScript code can rely on variable appConfig at runtime.
Alernatively, you can device a way for your page to inject the configuration into the code generated by your TypeScript:
var adminEmail;
function SetAdminEmail(val: string)
{
adminEmail = val;
}
function DisplayAdminEmail()
{
alert(adminEmail);
}
Then in your page:
<script type="text/javascript">
SetAdminEmail('#ConfigurationManager.AppSettings["adminEmail"]');
DisplayAdminEmail();
</script>

Call a C# method from an html a tag [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have the following C# method, how can I call it from an HTML a tag?
CLICK
public void MyFunction()
{
Response.Write("<script>alert('Hello');</script>");
}
You can use jQuery ajax to MVC action:
function CallMyCShartAtion(id)
{
$.ajax({
url: 'urlToController', //path to the contoller's action
data: { id: id }
}).done(function() {
alert('Added');
});
}
And then
CLICK
http://api.jquery.com/jQuery.ajax/
This isn't directly possible (at least not in the way you're trying to do it). First of all, JavaScript is typically a client-side language, which can be run in your browser, as opposed to C# (or Java or PHP or...) which is typically run on the server side.
In order to accomplish this task like you're asking for here you need to
have some kind of backend in place (say, in C#)
issue a request from the client side (frontend) to the backend (e.g. using AJAX)
Use direct javascript for that functionality, no need to use c#
CLICK

Getting data of HTML DIV tag without using Regular Expressions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hello to all respected Experts,
I've one question regarding to C#.net. What i wanna do is that basically i have one HTML page
and i wanna extract data from it's DIV tag this is the sample of HTML :
<div class="clr fleft">
<strong class="xx-large">033 111 22222</strong>
</div>
Now I wanna Get those numbers which are inside of "xx-large" Tag.
I want some help in doing it.
You can use HtmlAgilityPack
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlstring);
using XPATH,
var data = doc.DocumentNode.SelectSingleNode("//*[#class='xx-large']").InnerText;
using Linq,
var data = doc.DocumentNode.Descendants()
.Where(x => x.Attributes["class"] != null && x.Attributes["class"].Value == "xx-large")
.First()
.InnerText;
As i know, you cant access them just by c# (your server-side codes). You must write some javascript codes to do this. (your javascript code can have no regex)
All you need is a library with predefined parsers. You can use Beautiful Soup parser (originally written in python, can be interfaced with C#) see how it's done http://ashomtwit.espace-technologies.com/4499480-BeautifulSoup_and_ASP_NET_C_.html or you can choose an alternative package. These library has the predefined regular expression and has methods to open web pages to collect the information. It is so simple to use this.

reading div after javascript load [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having problems parsing information from a forum.
Heres some examples:
Easy
Hard
It would be really easy to get the information as they are displayed in the div where id = "poe-popup-container".
The problem is that that div is only populated when the browser allows you to see the information. That can be easily reproduced by making your browser height really small and looking in the HTML code for the . However, the div will be empty, but as soon you scroll down to see the item it will change.
I'm trying to read the nodes inside the with htmlagillitypack. The problem is that, as i explained, it only has information when the browser says that you need that information.
So, when you try to download the html, the div is empty.
I've tried to download the page with the web browser too, but the same thing happens.
I'm trying to use the following code:
string page = System.Text.Encoding.UTF8.GetString(Webclient.DownloadData("http://www.pathofexile.com/forum/view-thread/966384"));
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[#id='poe-popup-container']");
MessageBox.Show(node.InnerHtml);
You're trying to do impossible. Javascript is executed in browser. HtmlAgilityPack is library just for parsing static html - it can't execute javascript.
So why don't you look into browser automation instead ? Try for example http://watin.org/

Creating an application for Math Practice with ASP.NET [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am trying a develop a Math Practice web application using ASP.NET (not MVC), which allows Users to perform Multiple choice or simply type an answer, click a button and see answer.
The first idea that comes to mind is simply get permission to use a book, grab all the questions and answers using a DB and display the right answer. But how would I display the answer or the show me how the answer was derived.
My confusion is; how does this work?:
Do I have to write all the formulas and answer myself?
Are there programs for this?
I do not even know where to start.
What I would like to do is have the user select an answer, if the answer is wrong, they can click the show me button.
I am looking to either write this in C#, Visual Basic using JQuery on the front end.
Just need some direction.
Thanks my friends.
When I was in college I had to do something very similar.
What I did is stored expressions 4 + 5; 1 * 6 in database and then pushed those expressions to evaluate on run-time because it is pretty easy in C#.
It takes expression from database 2 + 2
It evaluates the expression on run-time producing result 4
You get your result and show it to user or do whatever with it
More info on how exactly to do that: http://www.c-sharpcorner.com/UploadFile/mgold/CodeDomCalculator08082005003253AM/CodeDomCalculator.aspx
I don't think you need to evaluate a stored expression. Since you just want the user to enter multiple choice or the answer itself, just set up your database as follows:
QuestionAnswers:
ID (uniqueidentifier) (primary)
QuestionText (varchar)
QuestionAnswer (varchar)
ShowMeHowEquation (varchar)
Then, display the ShowMeHowEquation via jQuery like so:
Show Me How
<div class="showMeHowEquation">Database driven contents here...</div>
<script>
$(document).ready(function() {
$('.showMeHow')click(function() {
$(this).next('.showMeHowEquation').slideDown();
});
});
</script>

Categories