How to populate html input from server side - c#

I am not quite sure how to attack this, basically I have two html fields in my aspx page:
<input type="text" name="fname" />
<input type="text" name="lname"/>
Now i would like to populate them from the server side when the page loads based on some data collected from the database, basically this data is stored in two properties
public string FirstName { get; set;}
public string LastName {get; set;}
How can I pass the value from such properties into the html inputs On_Load ?
I would appreciate the help.

Here is one way, assuming Webforms:
<input type="text" name="fname" value="<%:FirstName%>" />
<input type="text" name="lname" value="<%:LastName%>" />
If using .NET before 4.0, replace the <%: with <%=.
Another option is to change the input types to be runat="server" and assigning the values directly on the server side.

Alternatively add runat="server" to your elements, then you could do something like
fname.Value = FirstName;
lname.Value = LastName;

The info I was looking for was like this:
protected void Page_Load(object sender, EventArgs e)
{
address.Value = Request.QueryString["lat"];
address1.Value = Request.QueryString["long"];
}
takes values from the URL string and puts them in an HTML input="text"
http://localhost:64375/Map.aspx?lat=detroit&long=windsor
Enter Address A: <input runat="server" name="address" id="address" type="text" />
Enter Address B: <input runat="server" name="address1" id="address1" type="text" />
thanks Ash and Oded for the combined answer

Related

Capture URL Before Form Post

I'm trying to capture the URL submitted when the page loads. When the page loads, it's appended with provided parameters to give me information i need save off. The issue is when I have the user fill in form fields and submit, the URL contains the handler method. What seems obvious is that I need to capture the URL before the form is submitted. I'm not familiar enough w/ Razor to understand if there's an event that fires on load, or how to accomplish this. Is there something simple like a page load event? Thanks!
1) Starts as: https://localhost:44384/sign
2) On submit takes the handler name: https://localhost:44384/Sign?handler=SubmitAndRedirect
<form method="post">
First name:<br>
<input type="text" name="firstname" autocomplete="on" maxlength="10" required><br>
Last name:<br>
<input type="text" name="lastname" autocomplete="on" maxlength="10" required><br>
Employee number:<br>
<input type="text" name="empnum" maxlength="10">
<br>
<br>I agree to the terms above:<br>
<input type="checkbox" name="check" required>
<button type="submit" asp-page-handler="SubmitAndRedirect">Submit and Redirect</button>
</form>
public ActionResult OnPostSubmitAndRedirect()
{
//Capture Form Data and Push to SQL DB
var firstname = Request.Form["firstname"];
var lastname = Request.Form["lastname"];
var empnum = Request.Form["empnum"];
bool isValidUser = ValidateUser(empnum, lastname);
if (isValidUser == true)
{
WriteToDB(firstname, lastname, empnum, url);
return Redirect(hardcoded);
}
else
{
return Redirect("/sign");
}
}
Model binding features of MVC might be what you're looking for. Take a look at this article.
See how it uses the EmployeeViewModel class and how the form gets posted to the action.
There is no protected void Page_Load(object sender, EventArgs e) in MVC as in Web Forms that I'm aware of.
Also check out jQuery's submit handler. Maybe this can allow you to capture some information and process it before submitting.
Thanks all. I was able to resolve adding a property on the C# side and then capture the request before the form was submitted by setting the property outside the form using inline code between paragraph tags.
<p>
#{
var QS = Request.QueryString.Value;
JT.query = QS;
}
#*<h1> #JT.query</h1>*#
</p>

Code echecks for DPM (Authorize.Net)

I am using asp.net, c#, and web forms (not MVC).
I was following this thread, but I am not sure what the code behind would be to implement this solution.
https://community.developer.authorize.net/t5/Integration-and-Testing/DPM-with-EChecks/m-p/33623#M181...
It states to use this for the form:
<input type='hidden' runat="server" name='x_login' id='x_login' />
<input type='text' readonly="readonly" runat="server" name='x_amount' id='x_amount' size='9' />
<input type='text' runat="server" name='x_fp_sequence' id='x_fp_sequence' />
<input type='text' runat="server" name='x_fp_timestamp' id='x_fp_timestamp' />
<input type='text' runat="server" name='x_fp_hash' id='x_fp_hash' />
<input type='hidden' name='x_method' id='x_method' value='ECHECK' />
<input type='hidden' name='x_bank_aba_code' id='x_bank_aba_code' value='?????????' />
<input type='hidden' name='x_bank_acct_num' id='x_bank_acct_num' value='123456789123' />
<input type='hidden' name='x_bank_acct_type' id='x_bank_acct_type' value='CHECKING' />
<input type='hidden' name='x_bank_name' id='x_bank_name' value='bANKnAME' />
<input type='hidden' name='x_bank_acct_name' id='x_bank_acct_name' value='aCCOUNTnAME' />
<input type='hidden' name='x_echeck_type' id='x_echeck_type' value='WEB' />
<input id="x_relay_url" name="x_relay_url" type="hidden" value="https://developer.authorize.net/tools/paramdump/index.php" />
<input type='hidden' name='x_relay_response' value='true' />
<input type='hidden' name='x_delim_data' value='false' />
<input type='submit' runat="server" id='buttonLabel' />
So my question is what is necessary in my C# code for this to work? I have been searching and haven't found resources on the topic for the DPM method. I only have seen resources for AIM.
For DPM there's not really anything you can do with fields such as "x_bank_aba_code", etc, in the code-behind because that would involve posting back to your own server, which would put you into the land of "PCI Compliance", which negates using DPM in the first place.
What I have done is:
1) Ask for all non-CC or ECHECK data on a previous page
2) Store this data and generate a GUID for the record
3) Load up all that data on your payment page from the code-behind (on PageLoad() - pass the guid on the URL so you can pull the appropriate record and display the data already entered)
4) either
name your actual input fields "x_bank_aba_code", etc. (I don't recommend this)
-or-
Attach an OnClientClick() event to your submit button (allows for validation.) Then...
5) Use the validation to load up the hidden variables.
// Routing Number
var routingNumber = $("#<%=txtBankRoutingNumber.ClientID%>").val();
var routingNumber2 = $("#<%=txtBankRoutingNumberConfirm.ClientID%>").val();
routingNumber = routingNumber.trim();
routingNumber2 = routingNumber2.trim();
if (routingNumber == '') {
alert('Please provide your Routing Number.');
return false;
}
if (routingNumber2 == '') {
alert('Please confirm Routing Number.');
return false;
}
if (routingNumber != routingNumber2) {
alert('Routing Numbers don\'t match.');
return false;
}
// THIS LINE WILL SET THE HIDDEN VARIABLE
document.getElementsByName('x_bank_aba_code')[0].value = routingNumber;
I'll try to post more detail later.
Hope this helps - but the main point is that the banking/CC/ECHECK information cannot be posted pack to your server - that's where the javascript/jquery comes in by both validating the input and loading up the hidden variables.

Input control type Checkbox always true

I have searched all the topics discussing this issue and cant seem to find one that explains the issue using Asp.net C#, everything is either javascript or MVC or PHP my reasoning for opening a new question regarding this.
I have 2 input control type checkbox
<input runat="server" type="checkbox" name="chkChildSexMale" id="chkChildSexMale" />
<input runat="server" type="checkbox" name="chkChildSexFemale" id="chkChildSexFemale"/>
<asp:Button ID="btnSaveCheckBoxes" runat="server" Text="Save CheckBox" OnClick="btnSaveCheckBoxes_Click" />
*EDITED**
protected void SaveCheckBoxes()
{
if (chkChildSexMale.Checked)
{
do something
}
else if (chkChildSexFemale.Checked)
{
do something else
}
}
Then in my button click event I call this method
protected void btnSaveCheckBoxes_Click(object sender, EventArgs e)
{
SaveCheckBoxes();
}
You should be using input type radio, not checkbox, for two mutually exclusive selections:
<input runat="server" type="radio" name="chkChildSex" value="male" id="chkChildSexMale" />
<input runat="server" type="radio" name="chkChildSex" value="female" id="chkChildSexFemale" />
When you use the same name attribute for two checkboxes, the second overrides the first. When you use the same name attribute for two radio buttons, it associates them and the value is the value of the checked item.
Although the id attribute is different for both checkboxes the name attribute is the same.
Also, the suggestion about using radio buttons makes the most sense based on what it looks like you're trying to do.
<input runat="server" type="radio" name="rdoChildSex" value="Male" />
<input runat="server" type="radio" name="rdoChildSex" value="Female" />
Again, I would recommend going with the radio button example above when you need to pick between one of two options. But if you want to get radio-like functionality for checkboxes you can try this...
<input runat="server" type="checkbox" name="chkChildSexMale" value="Male" OnCheckedChanged="Check_Clicked" />
<input runat="server" type="checkbox" name="chkChildSexFemale" value="Female" OnCheckedChanged="Check_Clicked"/>
void Check_Clicked(Object sender, EventArge e)
{
var checkbox = ((CheckBox)sender);
if (checkbox.Value == "Male")
{
chkChildSexFemale.Checked = false;
chkChildSexMale.Checked = true;
do something...
}
else
{
chkChildSexMale.Checked = false;
chkChildSexFemale.Checked = ;
do something else...
}
}
The code above should REALLY cover all your bases. It is even somewhat overkill. But is should work.
Try changing your code to the following:
if (chkChildSexMale.Checked == true)
{
do something
}
else if (chkChildSexFemale.Checked == false)
{
do something else
}
My issue was on page load I was using my data reader to set the checkboxes based on certain values. Which when I clicked my button click it was over writing my selected values with values from the database. So I Had to add the if (!Page.IsPostBack)
Giving the information I provided it would have been really hard to figure out that so I thank all for trying to help

Get posted value In C# ASP.NET

I have a dynamic form that allow to add many fields dynamically,
I Know how to get the value of single field in aspnet using : Request.Form["myField"],but here i have more than field and i dont know the count of these fields since these are dynamic
the fields name is "orders[]"
ex:
<form>
<input type="text" name="orders[]" value="order1" />
<input type="text" name="orders[]" value="order2" />
<input type="text" name="orders[]" value="order3" />
</form>
In php,
i get the values as an array by accessing $_POST['orders'];
ex:
$orders = $_POST['orders'];
foreach($orders as $order){
//execute ...
}
how can I do this in c# ?
Use Request.Form.GetValues.
Request.Form is a NameValueCollection, an object that can store a collection of items under the same key and the ToString displays the values in CSV format.
Markup:
<input type="text" name="postField[]" />
<input type="text" name="postField[]" />
<input type="text" name="postField[]" />
<input type="text" name="postField[]" />
<asp:Button Text="text" runat="server" OnClick="ClickEv" />
Code behind:
protected void ClickEv(object sender, EventArgs e)
{
var postedValues = Request.Form.GetValues("postField[]");
foreach (var value in postedValues)
{
}
}
You would use Request.Form[]. Or if your form and fields had runat="server" and ids, you could just use the id in the codebehind and the .Text() method to access its value.
You can access everything that gets sent back to the server by using the Request object.
Request.Form.Items
Is a collection that will contain the item you are looking for.

C# ASPX - Form Submission Query

I have a C# aspx form in which I need to input it's data into an SQL database and then return a response saying successful or not. I have no idea how to get the form data that is sent from the Default.aspx page. My basic code structure is below:
Default.aspx
<form runat="server" name="aForm" action="Results.aspx" method="post" onsubmit="ValidateForm()">
<input name="firstname" type="text" />
<input name="surname" type="text" />
<input type="submit" value="Submit" />
</form>
Results.aspx.cs
public partial class AwardsForm : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!this.IsPostBack){
Response.Redirect("Default.aspx");
} else (this.IsPostBack) {
writeResults(FormSubmit());
}
protected boolean FormSubmit() {
// get form data and insert it into SQL
// return true/false based on success
}
protected void writeResults(boolean results) {
if (results == true) {
Response.Write ("Success");
} else {
Response.Write ("Failed");
}
}
}
You can get the posted form data through Request.Form["key"], or, if your form elements are decorated with runat="server" then you should be able to grab them by id right in your code behind
<asp:TextBox id="yourTb" runat="server"></asp:TextBox>
string postedText = yourTb.Text;
Or you can do (though this is much less common)
<input type="text" runat="server" id="yourOtherTb" />
string otherPostedText = yourOtherTb.Value;
Or if you're working with purely html form inputs:
<input type="text" id="clientTb" name="clientTb" />
string clientText = Request.Form["clientTb"];
You can try by the following code.
string firstname = Request.Form["firstname"]
string surname = Request.Form["surname"]
Since you are doing something like this
<input name="firstname" type="text" />
<input name="surname" type="text" />
<input type="submit" value="Submit" />
the name attribute of the input controls are posted back to the server(IIS). Hence you would do this.
If(IsPostBack)
{
string firstName = Request.Form["firstname"];
string surName = Request.Form["surname"];
if(string.IsNullOrEmpty(firstName))
{
Response.Write("Firstname is required this form");
}
}

Categories