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 = "";
Related
I have a dynamically generated grid with x number of textboxes that will be in it. As each textbox is generated, I give it an OnChange event that is a set function.
Html.TextBox(... new { #onchange = "ChangeItemQuantity(" + vm.ID + ", " + fk.id + ")" ...
So when it's rendered, it looks like this:
<input ... type="text" onchange="ChangeItemQuantity(1939, 3)" />
Then, in the script section:
function ChangeItemQuantity(ItemId, ForeignKeyId) {
...
}
In the ChangeItemQuantity() function, how would I also capture the new value of the textbox? I don't really want to use an id on the textbox, because it is part of a grid with many textboxes.
Should I pass it in as a parameter? If so, what would the syntax be of the code that renders the textbox?
Or, is there a way to capture is inside the javascript function?
Thanks!
If you want to store data in the html element why not use data- attributes?
Set them like so
#Html.TextBox(.... new { #class="someClass" data-vmId="vm.ID", data-fkId="fk.id" })
Then set a listener on that class
$('.someClass').change(function() {
var value = $(this).val();
var vmid = $(this).data('vmid');
var fkid = $(this).data('fkid');
}
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;
I took a check box list control in c# and I data-bound it with a table and now I am trying to access its selected items in a string variable using this code and I took a label too so that it can show which values are selected.
string val = this.CheckBoxList1.SelectedItem.ToString();
Label1.Text = val;
on debugging this code I receive no values in string and therefore label doesn't prints anything out of the selected items of the databound checkboxlist.
string text = this.CheckBoxList1.SelectedItem.Text;
string value = this.CheckBoxList1.SelectedItem.Value ;
Have a look at ListControl.SelectedItem
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 am using mysql and asp.net with c#. I have a grid view which will display dynamically selected table data. I am able to display the data of selected table. In the first column i have added a check box and a Button outside Grid view. When user selects Check box and clicks on button, the selected rows must turn into text boxes. I am able to find the seleted check box, but i'm unable to convert the cells into text boxes. Here's my code:
int n = GridView1.HeaderRow.Cells.Count;
for( int i=0; i < GridView1.Rows.Count;i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
{
for( int j=0;j<n;j++)
{
TextBox txt = ((TextBox)GridView1.Rows[i].Cells[j]).Text;
}
}
}
At this line: TextBox txt = ((TextBox)GridView1.Rows[i].Cells[j]).Text;
i get a warning :
cannot convert 'System.Web.UI.Controls.TableCell' to type 'System.Web.UI.Controls.TextBox
I am unable to resolve this. Please help. Thank you
Try this.
You can remove one or few lines based on your hands-on with C#.
Concept is, you should create a TextBox, assign cell text that textbox and then Add that newly created textbox to child controls of Grid Cell of particular row.
Mark this solution if you found useful.
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if(isChecked)
{
for( int j=0;j<n;j++)
{
TextBox tbForCell = new TextBox();
tbForCell.Text = GridView1.Rows[i].Cells[j].Text;
GridView1.Rows[i].Cells[j].Text = "";
GridView1.Rows[i].Cells[j].Controls.Add(tbForCell);
}
}
If you want to avoid the TextBox to appear in CheckBox Column please initialise loop variable j with 1 instead of 0.
for( int j=1;j<n;j++)
Your method call is finding the cell. The Textbox is a control contained within the cell. Try something like this instead:
TextBox txt = ((TextBox) GridView1.Rows[i].Cells[j].FindControl("textbox name")).Text;
The FindControl method is documented here.
Also take #Bartdude's advice about using editable gridviews. If one will work at least as well as what you're trying to hand-roll, it's worth the time learning how to use it.