I have a aspxcombobox with ID="comboSubjectCategory", can I get the selected value/text using javascript function? thanks lot...I tried this but it does not works.
function getValue() {
var combo = document.getElementById('comboSubjectCategory').value;
alert(combo);
}
The ASPxComboBox is not a simple HTML element. In the browser, it is represented by the ASPxClientComboBox Class. Use its GetValue, GetText or GetSelectedItem methods to obtain the selected value.
You can obtain the ASPxClientComboBox instance by the name assigned to the ClientInstanceName property of the ASPxComboBox control.
See Also: Client-Side Functionality
var combo = document.getElementById('<%= comboSubjectCategory.ClientID %>');
var val = combo.options[combo.selectedIndex].value;
Related
I have the follwing dropdown list and to get the selected value with javascript is easy enough.
<select id="FirstDropDown" runat="server" onchange="ValidatePrimaryDropDown();" >
<option>[Please Select Yes Or No]</option>
<option>Yes</option>
<option>No</option>
</select>
var e = document.getElementById("FirstDropDown");
var dropDownFirst = e.options[e.selectedIndex].value;
I prefer to use this dropdown as apposed to 'asp:DropDownList'.
How can I retrieve the selected value in code behind C#?
There are FindByText and FindByValue function available.
ListItem li = Select1.Items.FindByText("Three");
ListItem li = Select1.Items.FindByValue("3");
li.Selected = true;
Link to source
Is that an aspx page? If so, add a name attribute to your select and use
Requets.Form["elementName"];
in aspx.cs.
Btw: to your javascript code: is there any particular reason why are you using DOM selection instead of jquery? In jquery you wold just use
var selectedItem = $("#FirstDropDown").find(":selected").text()
I have a C# MVC4 application in which I am writing a JQuery function to grab some values, post to an ActionResult and then refresh a partial view. All functionality is working except for setting a new var equal to the value of a variable within one of my div elements.
The pre-existing variable is called myName and is located in a div with an id of NameDiv.
Ive tried these four versions of code and each results in: Reference Error myName is not defined.
var origname = myName;
var origname = myName.value();
var origname = myName.val();
var origname = $('#NameDiv').valueOf(myName);
When running the application and inspecting element, I see that myName is populating with the correct value.
Use:
var origname = $('#NameDiv').find('input[name="myName"]').first().val();
// console.log(origname);
This will find the element on the page with the id of "NameDiv". Then it gets the input elements on the page with the name of "myName". Then it gets the first one found. It will then get the value of it (by using .val()), and store that value in the variable origname.
I have an ASP.NET user control that contains a textbox which has an AJAX CalendarExtender control which allows users to select a date which then populates the textbox.
I can clear the textbox value using a submit button and setting the value to null in the code behind, but I need to set the value to "" in the textbox using Javascript.
I have tried the following javascript but it didn't work:
function ClearTextBox()
{
document.getElementsByName('TextBox1').Value = "";
}
What javascript should I use?
Instead of Value => value. JavaScript is case sensitive...
getElementsByName returns an array of elements so select the first element with [0]
document.getElementsByName('TextBox1')[0].value = "";
or select the element by id:
document.getElementById('TextBoxId').value = "";
I'm at a bit of a loss on this one and haven't been able to find anything helpful in my searches so I'm hopeful someone can help me out here.
I've got a RadioButtonList that I'm adding a List of dynamically created ListItems, where I set both the text and the value for each item. On DataBind for the RadioButtonList the Value for the ListItem gets replaced by the Text, which just doesn't seem to make sense to me.
I can see on the client side when I look in Firebug that the label and the value on the input are the same, and the value is nowhere to be seen.
Has anyone else had any experiences like this, or does anyone know where I might be going wrong?
var rbList = new List<ListItem>();
var radioButtonList = new RadioButtonList();
foreach(var object in objects) {
var li = new ListItem {Text = object.Name, Value = object.Guid};
rbList.Add(li);
}
radioButtonList.DataSource = rbList;
radioButtonList.DataBind();
Should you be using Databinding here? Can you not just add your ListItems to the radio button list directly?
I would imagine that the Databinding is getting confused about how to bind your list so is just using ToString on each of your elements which seems to just return the Text Property. This is then being used as both the Text and the Value.
You probably just want to create your items and add them straight to your Radio button control as follows:
var radioButtonList = new RadioButtonList();
foreach(var object in objects) {
var li = new ListItem {Text = object.Name, Value = object.Guid};
radioButtonList.Items.Add(li);
}
For those, whose are still struggling - do not forget to fill DataValueField, if control has it. Simply provide string name of your value property and you will be fine.
As Chris mentioned - Databinding was in fact confused and therefore DataValueField exists. My problem was with basic asp:DropDownList.
I'm trying get values from a GridView using the following code:
foreach (GridViewRow row in this.dgvEstudios.Rows)
{
var xy = row.Cells[1].Text;
}
Always get a an empty string ("") as the value returned from .Text, why does this happen? I have set EnableViewState to true
If there are controls in each cell you will need to get the value like this:
Label lblEmailAddress = GridView.Rows[e.CommandArgument].FindControl("lblEmailAddress");
string Email = lblEmailAddress.Text;
in that case it it the control in the cell that has the value not the cell iteslf.
The cell might have controls inside it (e.g. LiteralControl or an HyperLink). This is what you should be looking for.
row.Cells[1].Controls collection, you should look for.
it could depend on many things.. Where is this code fired in relation to when the GridView is populated (Databind() called)?
Without any context, its hard to say what else it could be.