Pass values from default page to asp.net handler - asp.net

I have a simple html hidden field as shown below
<input id="currentDirectory" type="hidden" runat="server" value="123"/>
Now I need to pass this hidden field value using query string to asp.net handler ans I have done as shown below but it dosen't pass the value.
In Default.aspx page:
var value = $("#currentDirectory").val();
url: 'Uploader.ashx?myvalue=' + value,
Accessing in Handler.ashx:
Dim myvar As String= System.Web.HttpContext.Current.Session("myvalue")
Can anyone say me where am I going wrong?

Related

ASP.Net Server.Transfer and Hiddenfield

I try to save on Onclientclick to my hiddenfield a string value but it doesnt work I think because of Server.Transfer();
In my is only one control for postback(Dropdownlist) if I change dropdown value I get work Server.Transfer at last in my code.
Here is the code :
$(document).ready(function () {
alert($("[id*=hdnSelectedDiv]").val());
});
function SaveDiv(value) {
$("[id*=hdnSelectedDiv]").val(value);
}
<asp:LinkButton runat="server" CssClass="card-link" ID="lnkGoToLogin" OnClientClick="SaveDiv('dvLogin');return false;" meta:resourcekey="lnkGoToLoginRes"></asp:LinkButton></p>
Here is my OnSelectedIndexChanged :
CurrentSession.SetCurrentLanguage(ddl.SelectedValue);
ddl.SelectedValue = CurrentSession.CurrentLanguage.IetfLanguageTag;
Thread.CurrentThread.CurrentCulture = new CultureInfo(ddl.SelectedValue);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddl.SelectedValue);
Server.Transfer(Request.Url.AbsolutePath);
On document ready alert is my value always empty.I try to set value on my document and alert the value it is working.but if I click linkbutton and select any option in my dropdown after postback is my value empty.
How can I solve this I dont want to use Response.Redirect();
That hidden field must have name property.
When the postback occurs, within the server code, is the moment when you can retrieve posted values. This is the time to process them - because, after Server.Transfer starts executing, source context is gone. If you need those values within the target request (the handler transferred to), you need to pass them to it. Your options are: persisted storage (e.g. database), session (might be the best in your case), query.

Is it possible to transfer data retrieved using facebook Graph API to a database in ASP.NET?

I have an ASP.net Web project that includes a form and a Database.
When a user register to the site (not from facebook) he has a username, and then when he fills the form, I can add this username to the 'username' column in the database (using User.Identity.Name). When he login using facebook, I can't do it. So I thought to use his facebook ID, since any ID is different, but I can't find a way to do it. I tried to retrieve the ID using response.id, set the value in a Label, and then to get the Label content from the codebehind to transfer it to the DB, but it didn't work. here is the code I tried:
Set the ID into the label:
function testAPI() {
FB.api('/me?fields=name,email,gender,age_range,picture.width(45).height(44),location', function (response) {
console.log('Successful login for: ' + response.name);
document.getElementById('HiddenFacebookID').innerText = response.id;
});
}
The Label:
<asp:Label ID="HiddenFacebookID" runat="server"></asp:Label>
The code-behind:
conn.Open();
string insertQuery2 = "INSERT INTO UserData (username) values (#username)";
SqlCommand com2 = new SqlCommand(insertQuery2, conn);
com2.Parameters.AddWithValue("#username", HiddenFacebookID.Text);
com2.ExecuteNonQuery();
The Label content is really the facebook-ID, but the database gets NULL. Please Let me know if I wasn't clear.
I will appreciate any help, thanks!
The value you're setting in JavaScript isn't being posted back to the server. Only form values are posted to the server. And an asp:Label doesn't render as a form element.
Use a hidden form field instead:
<asp:Hidden ID="HiddenFacebookID" runat="server"></asp:Hidden>
And set its value in JavaScript:
document.getElementById('HiddenFacebookID').value = response.id;
Basically, regardless of the lies that WebForms has been telling for years, HTML content is not posted to the server when submitting a form :) Only form values are.

Classic ASP Form Fields

I have an ASP script with aspcaptcha which has comments form field, and sends an email to the webmaster. However if the visitor does not enter the captcha code correctly, they are taken back to the form page, a message states that they did not fill out the captcha code correctly BUT the form field is blank....
I am wanting to keep the comments in the form field and ask the visitor to re-fill out the captcha code.
Here is the the ASP script:
if Request.ServerVariables("REQUEST_METHOD") = "POST" then
captha = Trim(Request.Form("captha"))
if CheckCAPTCHA(captha) = true then
' Process form...CAPTCHA IS VALID!
else
response.redirect "myform.asp?c=f"
end if
else
response.redirect "myform.asp?v=f"
end if
The variable v=f tells the form that the captcha was not filled out, and to throw back an error. How can I populate the form fields with what the visitor already entered without asking them to type their comments all over again?
I don't know aspcaptcha but two possabilies:
First, does the form post to itself? That is, is the processing script on the same page as the form? If so then just use Request.Form("name")
<input name="frmName" type="text" id="frmName" value="<%
If Request.Form("frmName") <> "" Then Response.Write(Request.Form("frmName"))
%>" />
Second, If the form submits to a second page for processing and then returns the user to the original page, use session variables:
if CheckCAPTCHA(captha) = true then
' Process form...CAPTCHA IS VALID!
else
Session("frmName") = Request.Form("frmName")
response.redirect "myform.asp?c=f"
end if
Then
<input name="frmName" type="text" id="frmName" value="<%
If Session("frmName") <> "" Then Response.Write(Session("frmName"))
%>" />
Hope that helps...
A function usually helps in these cases:
Try Something like:
'Select between Session Value / Form Value
function SelectValue(sessionValue, frmValue)
result = ""
if isnull(frmValue) or frmValue = "" then
result = sessionValue
else
result = frmValue
end if
SelectValue = result
end function
Your form can then use the function as needed:
e.g.
<input name="frmName" type="text" id="frmName" value="<% SelectValue(Session("frmName"), Request.Form("frmName"))%>" />

Request.Form "Collection is only readonly" when trying to set text box content

Saving the values in the Lists & works fine.
abc= (ABC)(Session["xml"]);
string ctrlStr = String.Empty;
foreach (string ctl in Page.Request.Form)
{
if (ctl.Contains("something"))
{
ctrlStr = ctl.ToString();
abc.student[0].marks[j].science.something.Value = Convert.ToDecimal(Request.Form[ctrlStr]);
}
Want to retrieve the values from the saved object when I click on edit button back on the respective dynamic textboxes....
foreach (string ctl in Page.Request.Form)
{
if (ctl.Contains("studentname"))
{
ctrlStr = ctl.ToString();
(Request.Form[ctrlStr]) = abc.student[0].marks[x].science.studentname.ToString();---Gives an error stating the collection is only readonly
}
}
Request.Form — like the Request object generally — is read-only, reflecting the fact that, by the time you are responding to a request, the request itself cannot be changed. ASP.NET uses the values from the form POST to create server controls on the Page, and these allow you to control the values of the input and other form elements that are written to the Response object.
In your case, the TextBox controls are being generated dynamically, so they are not automatically bound to form values — hence your problem. You will need to keep references to the controls when they are created (or find them afterwards using the FindControl() method) and set their Text property.
(original answer follows)
The Controls collection becomes read-only at a certain point in the construction of the page. You have to do manipulation before that point. I don't remember offhand when it is, but you're safe with OnLoad through OnPreRender.
Where is your code firing from?
Update: Okay, I see what you're trying to do. This will be easiest if you're dealing with server-side controls (that is, controls generated by ASP.NET. That would look like this in your aspx (or ascx):
<asp:TextBox runat="server" ID="studentname"/>
Then you could update the value like this:
abc = (ABC)(Session["xml"]);
studentname.Text = abc.student[0].marks[j].science.something.Value.ToString();
That will set the value of the studentname text box automatically without needing to search through all of the Request.Form items. (Assuming you set j somewhere... I don't know the context for that.)
I can't tell for sure from your code, but it looks like you may just have a "plan HTML" input, which would look more like this:
<input type="text" name="studentname"/>
In that case, there is no simple way to update the value from your page's code, so I'd start by making sure that you're using server-side controls.
You can set the Request.Form values with reflection:
Request.Form.GetType().BaseType.BaseType.GetField("_readOnly", BindingFlags.NonPublic | BindingFlags.Instance)
.SetValue(Request.Form, false);
Request.Form["foo"] = "bar";
What you are trying to achieve?
The Form collection retrieves the values of form elements posted to the HTTP request body, with a form using the POST method. - http://msdn.microsoft.com/en-us/library/ms525985(v=vs.90).aspx

Retrieval of input type=password Request.Form["strPassword"] gives null

Are there any special considerations trying to read up data from an HTML form where the element is an input type="Password"? When a ColdFusion page POSTs my handler with form data I am getting null for the password instead of the typed value.
Here is the key line from the larger block below:
string password = context.Request.Form["strPassword"];
I have an HTTPHandler.ashx code file that performs an upload of a file when posted. Here is the key snippet of this code:
string username = context.Request.Form["strUsername"];
if (String.IsNullOrEmpty(username))
{
IdentifyInvoker = GetUserInfo();
brokerService = new Broker.FileService();
}
else
{
string password = context.Request.Form["strPassword"];
string domain = context.Request.Form["strDomain"];
IdentifyInvoker = GetInvokerInfoFromForm(username, password, domain);
brokerService = new Broker.FileService(username,password,domain);
}
The form from which the above code is posted (from ColdFusion) looks like this:
<b>User Name</b> <input type="text" name="strUsername" id="strUsername" size="13" />
<b>Password</b> <input type="Password" name="strPassword" id="strPassword" size="15" />
<b>Domain</b> <input type="text" name="strDomain" id="strDomain" size="13" value="cbmiweb" />
I was able to trap this with the debugger and was shocked to see that after this:
string password = context.Request.Form["strPassword"];
... password = null
In the immediate window, sure enough:
?context.Request.Form["strPassword"]
null
If I examine the entire Form collection in the debugger, I see the proper values laid out (separated by &) and none of the important data elements is null (but strangely the data contains a plus sign in front of the equal sign)! Here is a snippet from the immed window:
&strUsername=johna&strPassword+=xxxxxxxx&strDomain+=cbmiweb}
I have an ASP.NET client that POSTs to this same HTTPHandler and that works fine. Here the same form data shows without the interfering PLUS signs:
&strUsername=johna&strPassword=xxxxxxxx&strDomain=cbmiweb}
Any ideas on what causes this and how to retrieve the form data when it's formatted with the intervening PLUS signs?
EDIT:
Both the ASP.NET form and the ColdFusion form specify enctype="multipart/form-data" yet the latter embeds these PLUS signs.
Plus sign is the problem, it should not have been there, is your coldfusion forwarding request to your page or it is using its internal http request engine to do so?
Plus sign appears due to white space, please check in your coldfusion if any string concatenation caused white spaces to be inserted in your posted data?

Resources