I hope you can help me. I got a problem with reading and writing in and out from inputs in HTML.
Code:
<html>
<input id="test" value="" type="text"/>
</html>
in c# I access the value with:
test.value="test";
It works without any problems.
If i fill the Input on Page_load. I cant read the new value which was be entered by user.
I dont want to reload the Page.
Edit:
It dont solve my problem but it works too.
on Page_load
test.Attributes.Add("placeholder",value)
and on callback i read the new value with
test.value
Related
I am still new to the coding field and am having a bit of trouble with a part, hoping someone can help me. I am working on a MVC page where i am trying to move text, the user inputs, around the page to a few pre-set spots and without having to refresh the page. Do i need a type of script for this? And if i do what would be best? Thanks for the help.
You can do it this way.
Enter Text here... <input id="testText" type="text" onKeyUp="javascript:showText($(this), event);"/>
<br/>
<br/>
<br/>
See it here... <span id="testTextSpan"></span>
Then, add this script...
<script type="text/javascript">
var showText = function (el, e) {
$('#testTextSpan').html(el.val());
};
<script>
See it here in this JSFiddle...
http://jsfiddle.net/ZEcNp/
Be sure to include JQuery libraries in your project, or reference the CDN.
If you'd like me to include an example with raw javascript, with no JQuery, I can also provide that for you.
I would recommend jQuery to do all that client-side.
You would just need an ID for each element you would use as the predefined spots, using either .appendTo, .insertAfter, .html, or something else depending on your exact needs.
I have a charts program that works in js, but I need to update excels with the data using c#. So, what I've done so far is to create hidden input fields whose values are updated with the js variable:
<input type="hidden" name="baselineInput" id="baselineVariable" />
<input type="hidden" name="goalInput" runat="server" id="goalVariable" />
<input type="hidden" name="currentInput" runat="server" id="currentVariable" />
//do some things here
var MTDGauge = new Array(5,5,5);
document.getElementById("baselineVariable").value = MTDGauge[0];
document.getElementById("goalVariable").value = MTDGauge[1];
document.getElementById("currentVariable").value = MTDGauge[2];
The problem I'm having is reading these values back to the c# variable. Here's what I've tried (placed after the code above):
List<string> OverallData = new List<string>();
OverallData.Add(Request["baselineInput"]);
OverallData.Add(Request["goalInput"]);
OverallData.Add(Request["currentInput"]);
This is just adding null values (assuming so because the c# is loading before the javascript and the initialized values for the text fields are null?). I thought of creating a c# function that could be called using the onchange option in the input field, but that didn't get me anywhere. Is there something I'm missing? Do I have to do a post?
Thanks
Yes, to get your data back to the server (where your C# is running) you need to send it back.
POST is one way, or GET to add the values to the query string.
Javascript is only running in the browser, until you send something back to the server, your ASP.NET app will not have any information.
Of course, if you want to do it asynchronously you could use jQuery.ajax to send the data to a server URL without refreshing the whole page.
I am trying to get the value of a hidden input in code behind with the following code. I am trying to cast it but it cannot find it , any help ?
((HtmlControl)FindControl("contentId"))
I declare it in aspx with the following code:
<input id="contentId" type="hidden" />
I dont want to runat server because i have my own reasons
To access a HTML control at server side (in your C# code), you need to first add the runat="server" attribute. So, your markup should look like
<input type="hidden" id="contentId" runat="server"/>
Now, in the code behind you can use the control by its id contentId itself if the code behind got generated properly.
Please let us know why you are forced to use the FindControl in the first place as it can be accessed by using the id directly.
Update
As per the comment below, the user for some reason is not interested in making this input a server side control. Then the only possibility by which you can read the values at server side is as below. But this is not advised as any changes to the name goes unnoticed and breaks at runtime.
<input type="hidden" id="contentId" name="contentName" runat="server"/>
In Code
this.Request.Forms["contentName"] would return the hidden value.
Try to search it on the page this way
HiddenField hf = (HiddenField)page.FindControl("contentId");
To get the value:
HiddenField h = (HiddenField)Gridview.FindControl("HiddenFieldName");
Then with that you can put it into a string, if you wish to.
Use this code:
string s=((HiddenField)Panel1.FindControl("contentId")).Value;
Here panel is the container control. This may be a grid or anything else or even a master page. But if you are using FindControl, i think the control may be inside some container.
HtmlInputHidden hf = Page.FindControl("contentId") as HtmlInputHidden;
string hfValue = hf.Value;
Say I create a static .html page. Next I build a form with a bunch of input boxes. Then I decide I want to post all that form data to another page, call it process-form-data.aspx. My question is, since I posted the form data to an .aspx page, how can I use C# in the code behind screen to grab all that data?
I tried the following first:
NameValueCollection nvc = Request.Form;
string valTextBox1;
if (!string.IsNullOrEmpty(nvc["txtBox1"]))
{
valTextBox1 = nvc["txtBox1"];
Response.Write(valTextBox1);
}
And then I tried:
valTextBox1 = Request.Form["txtBox1"].ToString();
Response.Write(valTextBox1);
But neither method seems to work. I can only get those two methods to work if I submit the form using the runat="server" attribute on an .apsx page.
I'd like to avoid passing the variables through the URL.
Thanks for any help.
Thanks for all the suggestions! I thought when a form was submitted, the default method was to POST the data; turns out it's GET. (http://www.w3.org/TR/html401/interact/forms.html)
I just added the following attribute to the form tag: method="post".
<form action="process-form-data.aspx" method="post">
<input type="text" name="txtBox1" />
</form>
Both methods of collecting the form data mentioned in the original post work fine.
I am writing some code that connects to a website, and using C#, and System.IO, reads the html file into my application, and then I continue to parse it.
What I am wanting to do now is, there is a drop down (combobox) on this site, that has 2 static values. I am wanting to have my code pick the 2nd option in the combo box and then parse the resulting html on the post back.
Any Ideas?
Ya the 2 selects are always the same.
Spamming software? Uh... No. It parses a video game website for player stats and I have full permission from the vendor to do so.
Yes I agree about the webservices, and they dont exist. I have already written the HTML parser and it works great. However, I need to pop this drop down for more data
I'd use HtmlAgilityPack and the HtmlAgilitypPack.AddOns.FormProcessor for that.
Say the code looks like this:
What color is your favorite?: <br/>
<form method="post" action="post.php">
<select name="color">
<option>AliceBlue</option>
<option>AntiqueWhite</option>
<option>Aqua</option>
</select><br/>
<input type="submit" value="Submit"/>
</form>
You would want to POST to post.php the argument "color" with the value "Aqua" (or whatever select value you want).