Hey I have a radiobuttonlist and trying to set one of the radiobuttons to selected based on a session variable but proving impossible.
<asp:radiobuttonlist id="radio1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:listitem id="option1" runat="server" value="All"/>
<asp:listitem id="option2" runat="server" value="1" />
<asp:listitem id="option3" runat="server" value="2" />
</asp:radiobuttonlist>
I.e How can I set option2 to selected in code behind ?
The best option, in my opinion, is to use the Value property for the ListItem, which is available in the RadioButtonList.
I must remark that ListItem does NOT have an ID property.
So, in your case, to select the second element (option2) that would be:
// SelectedValue expects a string
radio1.SelectedValue = "1";
Alternatively, yet in very much the same vein you may supply an int to SelectedIndex.
// SelectedIndex expects an int, and are identified in the same order as they are added to the List starting with 0.
radio1.SelectedIndex = 1;
You could do:
radio1.SelectedIndex = 1;
But this is the most simple form and would most likely become problematic as your UI grows. Say, for instance, if a team member inserts an item in the RadioButtonList above option2 but doesn't know we use magic numbers in code-behind to select - now the app selects the wrong index!
Maybe you want to look into using FindControl in order to determine the ListItem actually required, by name, and selecting appropriately. For instance:
//omitting possible null reference checks...
var wantedOption = radio1.FindControl("option2").Selected = true;
Try this option:
radio1.Items.FindByValue("1").Selected = true;
We can change the item by value, here is the trick:
radio1.ClearSelection();
radio1.Items.FindByValue("1").Selected = true;// 1 is the value of option2
var rad_id = document.getElementById('<%=radio_btn_lst.ClientID %>');
var radio = rad_id.getElementsByTagName("input");
radio[0].checked = true;
//this for javascript in asp.net try this in .aspx page
// if you select other radiobutton increase [0] to [1] or [2] like this
Related
I have 3 ASP.NET textboxes and one HiddenField.
The value of the third textbox (this is disabled) depends on the values of the other two.
The formula is
txtPricepad = txtPrice/txtCarton
<asp:TextBox ID="txtPriceCase" runat="server" onblur="javascript:GetPricePerPad();></asp:TextBox>
<asp:TextBox ID="txtCarton" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPricePad" Enabled="false" runat="server" ></asp:TextBox>
<asp:HiddenField ID="hdPricepad" runat="server"/>
function GetPricePerPad()
{
var priceCase = document.getElementById('ctl00_content_txtPriceCase').value;
var cartons = document.getElementById('ctl00_content_txtCarton').value;
var res = Number(priceCase) / Number(cartons);
document.getElementById('ctl00_content_txtPricePad').value = res;
document.getElementById('ctl00_content_hdPricepad').value = res;
}
Assuming that the initial value of txtPricePad is 0 and txtCarton is 12.
When the value of txtPrice is changed to 1200, GetPricePerPad() will be called, thus txtPricePad will be 100.
Javascript successfully changed the txtPricePad's value to 100 but when I am calling txtPricePad from the codebehind, its value is still 0.
That's why I assigned also the result of the formula to a HiddenField. Are there other ways to do this? I do not want to use HiddenField again.
Liz, is it possible for you to make the text field readonly (ReadOnly=True) as opposed to making it Disabled=True? The reason being that when a text field is disabled is not submitted with the form in the POST request. See this question.
If you want to make it look as if it was disabled, I guess you can apply a CssClass to the button.
I would use one of 2 options
Perform the calculation again server side from the other inputs or
Use javascript to enable the txtPricePad field on form submit, see below
var pricePad = document.getElementById(<%=txtPricePad.ClientID%>);
pricePad.disabled = false;
I have a DataBound DropDownList that is loading fine, but i'd like to insert another item that says "--Select--" or something instead of having the first DataBound item automatically display.
Is there an easy way to do this or do I need to manually add a dummy item?
Here is my code:
MyContext fc = new MyContext ();
ddl.DataSource = fc.SomeTable;
ddl.DataBind();
Alternatively, add a default item in the markup and set the "AppendDataBoundItems" property to true.
<asp:DropDownList ID="ddl" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="---Please Select---"></asp:ListItem>
</asp:DropDownList>
After you do the databind do:
ddl.Items.Insert(0, "---Select---");
This will add it as the first item in the list.
Alternatively, you can add a new ListItem instead of a string, so you can have an actual value instead of a string as drop down list value.
So you can do something like:
ddl.Items.Insert(0, new ListItem("---Select---", Guid.Empty.ToString());
For my ASP.NET page, in the code behind I load various items/options into a DropDownList but I do not set a default by assigning SelectedIndex. I notice for the postback SelectedIndex is set to 0, even if I never set focus to or changed the value in the DropDownList.
If not otherwise specified in the code behind or markup, will a default value of 0 be used for the SelectedIndex in a postback with a DropDownList? If yes, is this part of the HTML standard for selection lists, or is this just the way it is implemented for ASP.NET?
This is normal behavior for the HTML SELECT control.
Unless the selected property is used, it will always start at the first item (index of 0).
In many cases, I don't want this in ASP.NET because I specifically want the user to select an option.
So what I do is add a blank item which I can then validate.
i.e.
<asp:DropDownList runat="server" DataSourceID="SomeDS" AppendDataBoundItems="true"> // Notice the last property
<asp:ListItem Text="Please select an option" Value="" />
</asp:DropDownList>
This way, I can set a validator which checks if the value is blank, forcing the user to make a selection.
For anybody looking for a way to do this from the code behind, you can add the extra list item there by using the Insert function and specifying an Index of 0, as mentioned in this SO Question. Just make sure to do so after you've called DataBind()
myDropDownList.DataSource = DataAccess.GetDropDownItems(); // Psuedo Code
myDropDownList.DataTextField = "Value";
myDropDownList.DataValueField = "Id";
myDropDownList.DataBind();
myDropDownList.Items.Insert(0, new ListItem("Please select", ""));
In ASP.NET CheckBoxList is there any way to determine of a particular checkbox is checked using jquery? I need to know if "All" is checked for example, either by value or label text.
<asp:CheckBoxList ID ="ToyotaModels" runat="server">
<asp:ListItem Value = "0">Corolla<asp:ListItem>
<asp:ListItem Value = "1">Matrix<asp:ListItem>
<asp:ListItem Value = "2">Tundra</asp:ListItem>
<asp:ListItem Value = "3">Prius</asp:ListItem>
<asp:ListItem Value = "4">All</asp:ListItem>
</asp:CheckBoxList>
This should get the checked checkboxes on a click:
$('input:checked').click().each(function() {
//process the checkbox stuff here..
});
EDIT: based on comment
function processChecks()
{
$('input:checked').each(function()
{
alert($(this).val();
});
};
You have to look at the HTML generated by ASP.NET's CheckboxList. The ID of the container for the inputs which are the check boxes is going to look like this in javascript:
<%= ToyotaModels.ClientID %>
Each checkbox input has _X appended to the ID where X is the numeric checkbox. So to find whether the first item (Corolla) is checked you can do this:
$('<%= "#" + ToyotaModels.ClientID + "_0" %>').is(":checked")
In jQuery you have .is(":checked") function that should do the trick.
I just want an ASP.NET DropDownList with no selected item. Setting SelectedIndex to -1 is of no avail, so far. I am using Framework 3.5 with AJAX, i.e. this DropDownList is within an UpdatePanel.
Here is what I am doing:
protected void Page_Load (object sender, EventArgs e)
{
this.myDropDownList.SelectedIndex = -1;
this.myDropDownList.ClearSelection();
this.myDropDownList.Items.Add("Item1");
this.myDropDownList.Items.Add("Item2");
}
The moment I add an element in the DropDown, its SelectedIndex changes to 0 and can be no more set to -1 (I tried calling SelectedIndex after adding items as well)... What I am doing wrong? Ant help would be appreciated!
Bare in mind myDropDownList.Items.Add will add a new Listitem element at the bottom if you call it after performing a DataSource/DataBind call so use myDropDownList.Items.Insert method instead eg...
myDropDownList.DataSource = DataAccess.GetDropDownItems(); // Psuedo Code
myDropDownList.DataTextField = "Value";
myDropDownList.DataValueField = "Id";
myDropDownList.DataBind();
myDropDownList.Items.Insert(0, new ListItem("Please select", ""));
Will add the 'Please select' drop down item to the top.
And as mentioned there will always be exactly one Item selected in a drop down (ListBoxes are different I believe), and this defaults to the top one if none are explicitly selected.
I am reading the following:
http://msdn.microsoft.com/en-us/library/a5kfekd2.aspx
It says:
To get the index value of the selected item, read the value of the SelectedIndex property. The index is zero-based. If nothing has been selected, the value of the property is -1.
In the same time, at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.selectedindex(VS.80).aspx we see:
Use the SelectedIndex property to programmatically specify or determine the index of the selected item from the DropDownList control. An item is always selected in the DropDownList control. You cannot clear the selection from every item in the list at the same time.
Perhaps -1 is valid just for getting and not for setting the index? If so, I will use your 'patch'.
It's possible to set selectedIndex property of DropDownList to -1 (i. e. clear selection) using client-side script:
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="A"></asp:ListItem>
<asp:ListItem Value="B"></asp:ListItem>
<asp:ListItem Value="C"></asp:ListItem>
</asp:DropDownList>
<button id="СlearButton">Clear</button>
</form>
<script src="jquery-1.2.6.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#СlearButton").click(function()
{
$("#DropDownList1").attr("selectedIndex", -1); // pay attention to property casing
})
$("#ClearButton").click();
})
</script>
I'm pretty sure that dropdown has to have some item selected; I usually add an empty list item
this.myDropDownList.Items.Add("");
As my first list item, and proceed accordingly.
The selectedIndex can only be -1 when the control is first initalised and there is no items within the collection.
It's not possible to have no item selected in a web drop down list as you would on a WinForm.
I find it's best to have:
this.myDropDownList.Items.Add(new ListItem("Please select...", ""));
This way I convey to the user that they need to select an item, and you can check SelectedIndex == 0 to validate
Create your DropDown list and specify an initial ListItem
Set AppendDataBoundItems to true so that new items get appended.
<asp:DropDownList ID="YourID" DataSourceID="DSID" AppendDataBoundItems="true">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
Please try below syntax:
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Select"))
or
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("SelectText"))
or
DropDownList1.Items.FindByText("Select").selected =true
For more info :
http://vimalpatelsai.blogspot.in/2012/07/dropdownlistselectedindex-1-problem.html