get value of hidden input field in c# - c#

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.

Related

Read and wirte Data without use forms

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

Redirecting to a page hiding query string parameters asp.net

I am working for a project which has web garden scenario and cannot keep any data in session/inmemory.
The asp.net page opens from Appian(bpm tool) and we pass id through query string.
Now, client is asking me to hide the query string parameter after reading it. But, in that case say, landing page is http://a.aspx?id='123' and after reading that value we have to redirect to b.aspx without exposing the id(query string).
Please suggest me a suitable way to achieve this. I am not really getting any idea for this.
you can add key/value pairs to the header, it won't be visible in the querystring.
HttpContext.Current.Response.Headers.Add( key, value);
and
string headerVal = HttpContext.Current.Request.Headers[key];
You can use session variables on the server side or http Post instead of GET.
Session["id"] = id;
And on load of b retrieve it.
To use Post you can use a hidden field and a form.
//You can set it like this
<form name='IdForm' action='b.aspx' method='post'>>
<asp:HiddenField id="WhateverId" runat="server" value='<%= Request.QueryString["whateverID"] %>' />
</form>
On redirect use javascript to post
function Redirect() {
document.forms["IdForm"].submit();
}
You must use this js script wherever the redirection happens.
And finally on b.aspx code behind
HttpContext.Current.Request.Form["WhateverId"]

Inserting data into a database from forms

I am trying to take data which has been typed into input boxes and then save them back to my database. I am connecting to a dataset called password with a table called password. This is my connection
passTableAdapters.passwordsTableAdapter pass = new passwordTableAdapters.passwordTableAdapter();
password.passwordsDataTable passtable = pass.GetData();
This allows me to connect to the dataset but from there I am really stuck. I want to take the information from the textbox and then save them back to the dataset.
I have been using msdn but still no luck. If someone can show me how to write back to the dataset I would be very grateful.
To get the data from your HTML Controls you may try this logic. I am assuming that you have Two Control like
HTML:
<input id="username" name="username" type="text" />
<input id="password" name="password" type="password" />
Code Behind:
var username = Request.Form["username"];
var password = Request.Form["password"];
Note:
Never forget to Give the inputs a name as well as an id otherwise Request.Form will not work
By using this you may get the value from input tag and then pass these values to your function as a parameter in which you are using Insert Query.
Hope you understand.
It looks like you are using type data adapters for accessing the database. If this is the case you should have insert methods. If not you need to reconfigure this and tell XDS to generate those methods for you.
Retrieving data from your form is as easy as TextBoxName.Text .

Collect Form Data Posted from Any Page

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.

define an array of input control in aspx.cs

i have an input control of type file.
<input id="FileUpload1" type="file" runat="server" size="35" />
there are four input controls like this and in aspx.cs file i m trying to make an array of these ids..
i have made an array
HtmlInputFile[] fl = new HtmlInputFile[4] { FileUpload1, FileUpload2, FileUpload3, FileUpload4 };
but it gives me an error..how can i get the value of these inputs.
The
FileUploadX.PostedFile
Property is null if no file was uploaded. You need to check wether
FileUploadX.HasFile == true
before attempting to access it.
In case you are using ASP.Net AJAX updatepanels, you'll run into all sorts of proplems with FileUpload. See Link for possible solutions

Categories