I have a static dropdown with the following html.
<asp:DropDownList width="100px" ID="dropDownActive" Runat="server">
<asp:ListItem Text="Inactive" Value="0"/>
<asp:ListItem Text="Active" Value="1" />
</asp:DropDownList>
I try to populate the selected value based off of data, but it has the the value of 0 before I get to it and will not accept a new value.
dropDownActive.SelectedValue = (support.Active)? "Active": "Inactive";
The values of your dropdown are "0" and "1", not "Active" and "Inactive"
dropDownActive.SelectedValue = (support.Active)? "1": "0";
Your dropdown list uses 0 & 1 to represent inactive and active values. So when setting the value, try using those, not their text counterparts.
dropDownActive.SelectedValue = (support.Active)? "1": "0";
Try this:
dropDownActive.SelectedValue = (support.Active)? 1: 0;
The SelectedValue property of the ASP DropDownList control is based off of the Value attribute of the markup.
You need to change your code to...
dropDownActive.SelectedValue = (support.Active) ? 0 : 1;
Related
hi i have gridview that when a row is selected, it populates into textboxes that are used to populate the gridview itself. the last field is a dropdownlist and its not displaying when the gridview is clicked. i set a breakpoint and see that its stuck on the first - 0 index. i dont know why it isnt moving forward... here is the code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
....
if (DropDownListCurrency.Items.FindByValue(row.Cells[7].Text.ToString().Trim) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[7].Text.ToString().Trim();
}
....
}
<asp:DropDownList ID="DropDownListCurrency" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
why you want take the value from textbox. Will better use DataKeyNames like this inside that event
GridViewRow row = GridView.SelectedRow;
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);
this work if you have only one value in DataKeyName if you look not there a index if you want have more than one value use this
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Values["FirstValue"]);
string name = Convert.ToString(GridView.DataKeys[row.RowIndex].Values["SecondValue"]);
Does the DropDownList contains the words that you wanna show displayed?
You need to set the AutoPostBack property to True:
it should be :
<asp:DropDownList ID="DropDownListCurrency" AutoPostBack="True" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
made a simple error...
called the wrong field was supposed to be value (Currency_ID) and not the text (Currency)
DropDownListCurrency.SelectedValue = GridView1.DataKeys[row.RowIndex].Values["Currency_ID"].ToString().Trim();
I am having trouble getting a radio button from a Radio Button List to become selected in an IF statement. Everything else is working correctly when the IF is true, except for this. Request Pending is the default value, but I need to be able to have the "Waiting for Approval" Button selected when the IF is True.
My HTML code is:
<asp:RadioButtonList ID="rbGiftStatus" RepeatDirection="Horizontal"
runat="server" TabIndex="3">
<asp:ListItem Text="Request Pending" Selected="True" Value="1"></asp:ListItem>
<asp:ListItem Text="Check Pending" Value="2"></asp:ListItem>
<asp:ListItem Text="Completed" Value="3"></asp:ListItem>
<asp:ListItem Text="Waiting for Approval" Value="4"></asp:ListItem>
</asp:RadioButtonList>
My C# is this:
rbGiftStatus.SelectedIndex = 4;
and I have tried other ways suchs as:
rbGiftStatus.Text = "4";
rbGiftStatus.SelectedItem = "4";
rbGiftStatus.SelectedValue = "4";
None of them seem to work, and I can't figure out why
SelectedIndex is the correct way: Set Radiobuttonlist Selected from Codebehind
But, you are using SelectedIndex of 4, which is out of range of the array. C# is 0-based indexing, so the first item is index 0. That would make your 4th item index 3.
rbGiftStatus.SelectedIndex = 3; should do it.
You can try with
rbGiftStatus.SelectedIndex = 3; //index max is 3 for 4 elements
The index starts with a 0 and not a 1. So if you have 4 items in your RBL then the index range will be 0-3. In this case you are calling an index of 4 which does not exists.
Try rbGiftStatus.SelectedIndex = 3;
I have a form containing two DropDowns lists:
Marital Status
No. of Children
Now I would like to enable the No. of Children DropDown upon selection of following items in the Marital Status DropDown:
Widow
Divorced
Awaiting Divorce
How can I do it?
In the MaritalStaus DropDownList Selected Index changed event, If the selected values match your options then enable the NoOfChild DropDownList.
protected void MaritalStaus_SelectedIndexChanged(object sender, EventArgs e)
{
//Match the selected value here : for Example:
if (MaritalStaus.SelectedValue.Equals("Divorced") || /*Other Comparisions */)
{
NoOfChild.Enabled = true;
}
}
DropDown lists have ListItem collection in them. Each ListItem has a Text and a Value. Try setting the text as "Divorced" and value as "D" or better to an integer like "1", something similar to ID. You will get these Text/Value from a Database table if you are retrieving it from the Database.
Make the No. of Children DropDown Enabled = false by default and then Enable = true as explained in the code snippet above by ebad86.
you can also use cascading dropdown from ajaxcontroltoolkit
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
.aspx
<asp:DropDownList ID="ddlMaritalStatus" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlMaritalStatus_SelectedIndexChanged">
<asp:ListItem Text="" />
<asp:ListItem Text="Widow" />
<asp:ListItem Text="Divorced" />
<asp:ListItem Text="Awaiting Divorce" />
</asp:DropDownList>
<asp:DropDownList ID="ddlNoOfChildren" runat="server" Enabled="false">
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
<!-- and so on -->
</asp:DropDownList>
aspx.cs
protected void ddlMaritalStatus_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlMaritalStatus.SelectedItem.Text == "Widow") // if widow is selected
ddlNoOfChildren.Enabled = true;
else
ddlNoOfChildren.Enabled = false;
}
I have a dropdownlist1 that has has 6 collection items including Select One. What I want is this ddl is set to Selectedvalue = null. But what I am getting is my ddl always selecting Select One as its initial value. My ddl properties for Select One is selected:false. How to set this ddl to initial selected value = null?
<asp:DropDownList ID="DropDownList1" runat="server" Visible="False" Width="146px">
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
</asp:DropDownList>
if (String.IsNullOrEmpty(txtSearchProductname.Text))
{
if (DropDownList1.SelectedValue == null)
{
txtProductName.Text = "";
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue.ToString();
}
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text;
}
You can add an 'Empty' value item:
<asp:DropDownList ID="DropDownList1" runat="server" Width="146px">
<asp:ListItem Selected="True"></asp:ListItem>
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
</asp:DropDownList>
Then you would check for
if (string.IsNullOrEmpty(DropDownList1.SelectedValue))
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="(Select a State)" Value="" />
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
</asp:DropDownList>
SelectedValue of a dropdownlist would never be null. It could be empty string, but it can never be null...
It would be better to set the Items in this fashion
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
Also SelectedValue of the Dropdown is a string property so better check the same using string.IsNullOrEmpty as it cannot be null, leave the value blank where you want to consider it as null and repeat the same values in the Text and Value part for others
Hera add a default value in the lsit...
<asp:DropDownList ID="DropDownList1" runat="server" Width="146px">
<asp:ListItem Selected="True">Select One</asp:ListItem>
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
but try something like seleted item instead of selectedvalue beacuse u have'nt defined values in the list items..
if (string.IsNullOrEmpty(DropDownList1.SeletedItem))
For textbox also
txtProductName = DropDownList1.SeletedItem).ToString();
Use CancelSelectOnNullParameter = False on the SqlDataSource
Make the select value="" that is empty and pass this to your stored procedure like this
dt = ta.GetData(
String.IsNullOrEmpty(DropDownList1.SelectedValue)? null : DropDownList1.SelectedValue,
String.IsNullOrEmpty(DropDownList2.SelectedValue) ? null : DropDownList2.SelectedValue,
String.IsNullOrEmpty(DropDownList3.SelectedValue) ? null : DropDownList3.SelectedValue, null);
neI have a DropDownList with a list of value.
I need in my code USE the value selected by the User in the DropDownList AS INT DataType (numeric).
At the moment I am using this code below (CONVERTING DATATYOES).
Script work just fine.
But I would like to know if exists an alternative way to do it.
Any ideas it is very appreciate. Thanks for your time.
<asp:DropDownList ID="uxPageSizeUsersSelector" runat="server"
AutoPostBack="True"
onselectedindexchanged="uxPageSizeUsersSelector_SelectedIndexChanged">
<asp:ListItem Value="1" Selected="True">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownList>
int myPageSize = Convert.ToInt16(uxPageSizeUsersSelector.SelectedValue.ToString());
PageSize = myPageSize;
You can use the try parse method. This will make sure that your value is an int, and if not, allow you to handle the problem instead of throwing an exception:
int myPageSize;
if(int.TryParse(uxPageSizeUsersSelector.SelectedValue, out myPageSize))
{
//SUCCESS
}
else
{
///handle problem here.
}
You can do it for Int16 DateTime etc. too.