I have some Javascript code that assigns a value to a text box:
document.getElementById('longitudeTextBox').value = location.coords.longitude;
What I want to do is get the value of that text box over to Visual Basic:
Dim Logitude As String = longitudeTextBox.Text
This works if the code is inside a button, but if I use it on a page event like Page_Load, I'm not receiving any data.
Try using it when pageload.complete = true
Related
i am hoping someone will help me, i am pulling out my hair for the last few hours...!
I am using asp.net web forms with c#
I have the following button in my menu , this button opens up a page that lists selected properties for realtor…
htmlCode.Append("<button type='button' class='btn btn-basic' id='btnEnquire' onclick='openEnquiryPage()'> <span class='glyphicon glyphicon-th-list'></span> View my list </button>");
function openEnquiryPage() {
location.href = "EnquiryPage.aspx";
//get the list from the storage into the hidden field
//getListOfProperties();//not in use...
}
The list is stored in localstorage as Json, I can see the correct data in the text box on the page
var retrievedData = localStorage.getItem("propertyArray");
proplist2.value = retrievedData;
declared as follows in asp.net control
<asp:TextBox ID="TextBox1" runat="server" Text="initial"></asp:TextBox>
BUT in the hidden field, the data is always = ""
<asp:HiddenField ID="txtPropList" runat="server"></asp:HiddenField>
javascript to populate the hidden field...
function getListOfProperties() {
document.getElementById("body_content_txtPropList").value = retrievedData;
var proplist2 = document.getElementById("body_content_txtPropertyList");
proplist2.value = retrievedData;
}
when I debug the following lines in the code behind ...
Page.ClientScript.RegisterStartupScript(this.GetType(),"prop", "getListOfProperties();",true);
_propertyListJson = txtPropList.Value;
propertyListJson I always = “”
Thank-you in advance.!
If you want to use the hidden field then you should use
<%=txtPropList.ClientID%>
as on when you render the page and see it in inspect element server would render the different Id so it would not store the value in hidden field simply so it is feasible to use ClientID to create the static mode. But as on when you call the javascript function from the c# code then it would call the javascript function but it would not wait for it and execute the next line of code as on at that time the value is not store in hidden field so it would return the null value.
If you want then call the function of javascript on page load event and then click the button and retrieve the value it would successfully retrieve the value but i dont think that these would be the feasible code from my opinion
I suggest to create the WebAPI which would simply get the the data from the localstorage and perform the operation
And webAPI can be easily called from javascript using ajax call
I hope this would be helpful and answer is satisfactory 😊☺
Thank You ☺🙂
I have a text field that should be filled before custom saving through HTML custom button.
When a user fills this field and tries to save through custom button, the text inside this field is null even if the user fills it.
Any suggestions Please?
Many Thanks for replying to my query. Actually, I am calling the below function from custom HTML button and I saw the result from alert message as NULL value until I leave the text box. Once I click some other Field then I am getting right value and saving the record successfully. I have posted my code below please have a look and suggest me how I can achieve it. So how to get the text value if a user doesn't leave the text box and click on the Save button?
function createRecord() {
var oDescription = Xrm.Page.getAttribute("new_description").getValue();
if (oDescription != null) {
var callentity = {};
var activityId;
var currentUserId = Xrm.Page.context.getUserId();
var oLeadId = Xrm.Page.getAttribute("new_lead").getValue()[0].id;
callentity.Subject = "Call Activity";
callentity.Description = oDescription ;
XrmSvcToolkit.createRecord({....Some more functions here...
})
}
HTML Button code for calling above function
<input id="SaveCall" onclick="parent.createRecord()" type="button" value="Save Phonecall"></p>
Xrm.Page.getAttribute(arg).getValue(val) won't return a value until focus is lost from the arg attribute (as you've found out).
Some options you could try:
document.getElementById('new_description_i')[0].value
Removing focus from "new_description" on click of your button.
I have one ASP.net text box on my page. I am setting text property of this text box using JavaScript. Now, I want to access this text value from back-end (using C#). However, whenever I try to access it, I am getting the old value (the one set up during page load) and I am not able to get the value set by JavaScript code.
Am I missing something here?
ASPX markup of the text box -
<asp:TextBox ID="txtMessage" runat="server"></asp:TextBox>
JavaScript to edit this text box -
var txtMessage = document.getElementById("txtMessage");
txtMessage.Value = "New Value";
C# code to access text box text -
string strMessage = txtMessage.Text; // This does not return value set by above JS function
You just have to change this line in the JavaScript:
txtMessage.value = "New Value"; // the "v" in ".value" needs to be lowercase
UPDATE: I set up an ASP.NET page to make sure that the OP's code did in fact work with that one change, just to make sure I wasn't missing any other little typo. However, as Igor pointed out in his comment, it's a good practice to use the control's ClientID property to account for any prefixes that the .NET runtime might add to an HTML element's id.
So the two lines in your JavaScript should be changed to this (also confirmed to work correctly):
var txtMessage = document.getElementById("<%= txtMessage.ClientID %>");
txtMessage.value = "New Value";
When ASP.Net delivers the page to be render, the name of the controls are changed by prepending a suffix that identifies the container that hold them. From JavaScript, you must ensure to get the correct ID of ASP controls by accessing the .ClientID property.
//gets the element by its ID according to ASP rules
var txtMessage = document.getElementById("<%=txtMessage.ClientID%>");
//the "value" property is lowercase
txtMessage.value = "New Value";
My aspx page contains list of products along with dynamically generated textboxes and one order button with each product.
Textboxes and buttons are generated at runtime with ids like txt110234,txt110235...so on for textboxes and btn110234,btn110235...so on for buttons.
Each time user have to enter quantity in the textbox and press order button associated with any product to place any order.
Every thing is working fine but now i want to do it using ajax,so i need to get the value entered by user in text box.I want to do something like this-
var quan = document.getElementById('<%= txt' + id + '.ClientID%>').value;
But its giving me the following error.
Compiler Error Message: CS1012: Too many characters in character literal Source Error:
How can i get the value of textbox?Any suggestion will be appreciated..
The error you got is because you can't involve javascript inside the "<%= .. %>" block. Also this doesn't look possible since the "<%= .. %>" expression is evaluated in server before the page is rendered, but your "id" is a client side variable.
You can set the script in server side like that:
client side code:
function foo(ctlID)
{
var quan = document.getElementById(ctlID).value;
}
server side code:
TextBox txt = new TextBox();
txt.ID = "SomeID";
Form.Controls.Add(txt);
Button btn = new Button();
btn.ID = "someID";
btn.OnClientClick = "foo('" + txt.ClientID + "')";
Suggestion:
One way of doing this is to use jQuery css selector. You can assign a particular cssclass to all your input textbox and retrieve all of them via jQuery selector.
For example, on generating textboxes dynamically, you can assign them CssClass =".productQuantity"
and then later use jQuery selector something like $('.productQuantity')
I personally prefer this approach If I would like to traverse to multiple elements. This saves me from dealing with Ids etc.
I have a texbox inside a templatefield in a gridview. For this textbox I have defined an autocompleteextender with its TargetControlID set to "myTextbox" which is working just fine. At the same time, for the OnClientItemSelected property I have defined a javascript function which should set the value of my textbox, but my question is how can I get the the name of this textbox using javascript?
My control snippet like this:
ajaxToolkit:AutoCompleteExtender TargetControlID="txtValue" onClientItemSelected="SetValue"
And my code looks like this:
function SetValue(sender, eventArgs){
var TitleValue = eventArgs.get_value();
/* do smth with this value */
/* set the new value to my textbox ? */
}
Your suggestions and ideas are very much appreciated. Thank you a lot!
You should be able to get the textbox control using:
sender.get_element()
For extenders, get_element() returns the targeted control, for script controls, it's the element that represents that control.