how to add 2 values of textbox in asp.net using javascript - asp.net

I have 2 textbox in my asp.net page and also have one hiddenfield in my asp.net page , my hiddenfield will always have numeric value like 123.00 , and in my one textbox also I will always have numeric value like 20.00 now I want to add this hiddenfield value and textbox value and display it into second textbox thru javascript
I wrote the following code to do this
var amt = document.getElementById("txtsecond");
var hiddenamt = document.getElementById("AmtHidden").value
var fee = document.getElementById("txtFirst").value;
amt.value = hiddenamt + fee;
this should give me result like 123.00+20.00 = 143.00 but this is concatnating hiddenamt value and fee value and giving me result like 12320.00 in my first textbox
can anybody suggest me what is wrong in my code and what is the right way to get desired value

amt.value = parseFloat(hiddenamt) + parseFloat(fee);

the value of an input is just a string - convert to float parseFloat(foo) in JS and you'll be fine
edited to make float as I notice it's probably important for you

Textboxes are strings, you need to convert from a String to a Number:
var hiddenamt = parseFloat(document.getElementById("AmtHidden").value);
var fee = parseFloat(document.getElementById("txtFirst").value);
Eric

you should parse the values to decimals first:
decimal amt, hiddenamt, fee;
Decimal.TryParse(document.getElementById("txtsecond"),out amt);
Decimal.TryParse(document.getElementById("txtfirst"),out fee);
hiddenamt = amt + fee;

Related

Textbox dont allows null value in ASP .Net

i have column in database with Int data type, but textbox don't allows null. it gives error "Input string was not in a correct format".
objinsert.VehGrpID = Convert.ToInt32(txtVehGroupID.Text);
Use TryParse instead.
int GrpID = 0;
int.TryParse(txtVehGroupID.Text, out GrpID)
if(GrpID > 0)
objinsert.VehGrpID = GrpID;
You can just special-case out empty values. I am, of course, assuming here that an empty value should map to null.
objinsert.VehGrpID = string.IsNullOrWhiteSpace(txtVehGroupID.Text) ? null : (int?)Convert.ToInt32(txtVehGroupID.Text);
You'll also want to adjust your code to set the textbox's text accordingly.
Your question is also a bit unclear, because you say the textbox won't allow nulls, but you haven't shown us any code that adjusts the textbox text.
You can also use DBNull.Value Field. If a database field has missing data, you can use the DBNull.Value property.
check this link http://msdn.microsoft.com/en-us/library/system.dbnull.value%28v=vs.110%29.aspx
try this
int VehGrpID = Convert.ToInt32(txtVehGroupID.Text);
OR
int VehGrpIDtxtVehGroupID.Text = int.Parse(txtVehGroupID.Text);
The Text property of your textbox is a String type, so you have to perform the conversion in the code.

how do i delete characters in a string/text of a textbox?

I have an asp.net 4 textbox control that has it's text being dynamically populated by some java script. A Google Maps call to be exact. It's giving me mileage from 1 point to another. When the text displays, it shows " 234 mi" I need to get rid of the "mi" part of this text because the text is being converted to an Int32 Updating a table in my DB.
Basically I can only have an INT. Nothing else in the text box. How do I get rid of the "mi" at the end of the text?
Thanks
C#
EB
On the postback, before you save it you could:
var saveValue = Int32.Parse(tbTarget.Text.Replace("mi", string.Empty).Trim());
If your working with a variable length of chars (say someone enters miles instead) then your must do a foreach against the string (an array of char) and check isnumeric on each char.
A simple String.Substring works also:
String leftPart = TxtMileAge.Text.Substring(0, txt.IndexOf(' '));
int mileAge = int.Parse(leftPart);
This retrieves the part of the String in the range of 0 - indexOfWhiteSpace and converts it to an int
Edit: Since the value can have decimal places (as you've commented), you need to parse it to double, round it and then cast it to int:
var txtEstDistance = new TextBox() { Text = "40.2 mi" };
String leftPart = txtEstDistance.Text.Substring(0, txtEstDistance.Text.IndexOf(' '));
double distanceMiles = double.Parse(leftPart, System.Globalization.CultureInfo.InvariantCulture);
int oDdstanceMiles = (int)Math.Round(distanceMiles, MidpointRounding.AwayFromZero);

how do I convert textbox value to string?

I know this is simple but i am lost as to how to approach it. Iam a new newbie. Please have it on me as i am new here.
I have a textbox field called ClientsBalance.
This balance can is usually in money order with a set amount. The client is also allowed to pay by Debit Cards.
Here goes:
Dim paymentType As TextBox
otherPyment As String = "Debit Card"
If paymentType.Text <> "1250" Then
paymentType = "OtherPayment"
else
paymentType = gridview1.FindControl("paymentType" & CStr(1))
end if
Everything was working before the validation.
Now, I get the following error:
Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.TextBox'
Is there a way I can cast this line:
paymentType = "OtherPayment"
??
Thank you so much experts.
you need to say
paymentType.Text = "OtherPayment".
You just left out the ".Text"
Try changing this:
paymentType = "OtherPayment"
To this:
paymentType.Text = "OtherPayment"

Obtain data from dynamically incremented IDs in JQuery

I have a quick question about JQuery. I have dynamically generated paragraphs with id's that are incremented. I would like to take information from that page and bring it to my main page. Unfortunately I am unable to read the dynamically generated paragraph IDs to get the values. I am trying this:
var Name = ((data).find("#Name" + id).text());
The ASP.NET code goes like this:
Dim intI As Integer = 0
For Each Item As cItem in alProducts1
Dim pName As New System.Web.UI.HtmlControls.HtmlGenericControl("p")
pName.id = "Name" & intI.toString() pName.InnerText = Item.Name controls.Add(pName) intI += 1
Next
Those name values are the values I want...Name1, name2, name3 and I want to get them individually to put in their own textbox... I'm taking the values from the ASP.NET webpage and putting them into an AJAX page.
Your question is not clear about your exact requirement but you can get the IDs of elements with attr method of jQuery, here is an example:
alert($('selector').attr('id'));
You want to select all the elements with the incrementing ids, right?
// this will select all the elements
// which id starts with 'Name'
(data).find("[id^=Name]")
Thanks for the help everyone. I found the solution today however:
var Name = ($(data).find('#Name' + id.toString()).text());
I forgot the .toString() part and that seems to have made the difference.

How to get multiple value from one textbox?

I have created web application and textbox as a textarea. I am using javascript for validation. When I enter value in text box so it should be number not alphabet I have use textmode is multiple line.
My problem is that how I get multiple value from textbox and store in array in javascript and check each value is number or not. I am using the web form. Please help me.
You can get the value from a textarea like
var txtvalue = document.getElementById("txtareaid").value
and if are using a separator then something like
var txtvaluearray = document.getElementById("txtareaid").value.split(';')
will get you all the values in an array if the seperator is ;
Edit
As per your update you can use \n as the separator and as pointed by #Sohnee you can do the validation.
As addition to rahul:
If you want the values in the textarea seperated by line, you can use \r\n as the splitter.
This is a starter for ten.
var textValues = document.getElementById("mytextarea").value.split("\n");
for (var i = 0; i < textValues.length; i++) {
if (isNaN(textValues[i])) {
alert(textValues[i] + " is not a number.";
}
}

Resources