Choosing dropdownlist value - c#

I have created event for select index changed for dropdownlist like below but the event is not firing.I dont know what is going wrong?
<asp:DropDownList ID="ddlcurrency" runat="server" OnSelectedIndexChanged="ddlcurrency_SelectedIndexChanged1" >
<asp:ListItem Value="Nrs" >Nrs</asp:ListItem>
<asp:ListItem Value="$" >$</asp:ListItem>
</asp:DropDownList>
protected void ddlcurrency_SelectedIndexChanged1(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ddlcurrency.Items.FindByText("$").Selected == true) //keeping the currency value in session
{
Session["Curr"] = "Dol";
}
else
{
Session["Curr"] = "Nrs";
}
}
}

try this:
<asp:DropDownList ID="ddlcurrency" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlcurrency_SelectedIndexChanged1" >
<asp:ListItem Value="Nrs" >Nrs</asp:ListItem>
<asp:ListItem Value="$" >$</asp:ListItem>
</asp:DropDownList>
Have you tried setting a break point at the SelectedIndexChaged event of the DDL to check if HITS there?

<asp:DropDownList ID="ddlcurrency" runat="server" AutoPostback="true" OnSelectedIndexChanged="ddlcurrency_SelectedIndexChanged1" >
<asp:ListItem Value="Nrs" >Nrs</asp:ListItem>
<asp:ListItem Value="$" >$</asp:ListItem>
</asp:DropDownList>
protected void ddlcurrency_SelectedIndexChanged1(object sender, EventArgs e)
{
if (ddlcurrency.Items.FindByText("$").Selected == true) //keeping the currency value in session
{
Session["Curr"] = "Dol";
}
else
{
Session["Curr"] = "Nrs";
}
}

Set autopost back property of dropdown control to true
<asp:DropDownList ID="ddlcurrency" runat="server"
OnSelectedIndexChanged="ddlcurrency_SelectedIndexChanged1" AutoPostBack="true" >

Related

Choose 1 checkbox out of 2

I got several checkbox, but I only want the the text "New Desktop" and "New Laptop" can only choose 1 out of 2. I hope it could be done in C#.
<asp:CheckBoxList ID="Service1" runat="server"
Width="251px" >
<%--onselectedindexchanged="checkBox1_CheckedChanged"--%>
<asp:ListItem text="New Login ID & Email Address" ></asp:ListItem>
<asp:ListItem text="New Desktop" Value="2" oncheckedchanged="checkBox1_CheckedChanged" ></asp:ListItem>
<asp:ListItem text="New Notebook" Value="3" oncheckedchanged="checkBox2_CheckedChanged"></asp:ListItem>
<asp:ListItem text="New Mouse"></asp:ListItem>
<asp:ListItem text="New Keyboard"></asp:ListItem>
<asp:ListItem text="New Printer"></asp:ListItem>
</asp:CheckBoxList>
//.cs pages
protected void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (Service1.Items[2].Selected == true)
{
Service1.Items[3].Enabled = false;
}
}
protected void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (Service1.Items[3].Selected == true)
{
Service1.Items[2].Enabled = false;
}
}
I am giving you one example below but still this may not be a feasible way. You need to add CheckedChanged event to each CheckBox to remove check from other CheckBoxes
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if(CheckBox1.Checked)
{
CheckBox2.Checked =false;
CheckBox3.Checked =false;
CheckBox4.Checked =false;
}
}
You should repeat the above evevt for CheckBox2 , CheckBox3 and so on. You can extend it as per your requirement.
But in this scenario I would recommend you to use the ASP.NET Radio Button Control. Please refer this link for more information on Radio Button Control.
You can use OnSelectedIndexChanged event for CheckBoxList with AutoPostBack="True" so your control send request to server and selection chenged event will be call
I.e : For design
<asp:CheckBoxList ID="Service1" runat="server" Width="251px" AutoPostBack="True" OnSelectedIndexChanged="Service1_SelectedIndexChanged">
<asp:ListItem Text="New Login ID & Email Address"></asp:ListItem>
<asp:ListItem Text="New Desktop" Value="2"></asp:ListItem>
<asp:ListItem Text="New Notebook" Value="3" ></asp:ListItem>
<asp:ListItem Text="New Mouse"></asp:ListItem>
<asp:ListItem Text="New Keyboard"></asp:ListItem>
<asp:ListItem Text="New Printer"></asp:ListItem>
</asp:CheckBoxList>
and in code: 'li' have all selected item and as per your requirement "New Desktop" and "New Laptop" only choose 1 out of 2 .
protected void Service1_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList li = (CheckBoxList)sender;
foreach (ListItem l in li.Items)
{
if (l.Value == "2")
{
if (l.Selected)
{
Service1.Items[2].Enabled = false;
}
else
{
Service1.Items[2].Enabled = true;
}
}
else if (l.Value == "3")
{
if (l.Selected)
{
Service1.Items[1].Enabled = false;
}
else
{
Service1.Items[1].Enabled = true;
}
}
}
}

RadioButtonList verifying index

I have a Radiobuttonlist and I want after the Item with value 1 is selected to do something. When I used RadioButtonListType.SelectedValue is returning "" and when I use RadioButtonListType.SelectedIndex is returning -1.
Where I am doing wrong ?
this is my code
aspx
<asp:RadioButtonList ID="RadioButtonListType" runat="server" OnSelectedIndexChanged="RadioButtonListType_SelectedIndexChanged" ValidationGroup="emergency" AutoPostBack="True">
<asp:ListItem Value="0">Normal</asp:ListItem>
<asp:ListItem Value="1">Emergency</asp:ListItem>
</asp:RadioButtonList>
.cs
protected void RadioButtonListType_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonListSF.SelectedIndex == 1)
{
MPEEmergency.Show();
}
}

How to load items into dropdownlist after selecting item from another dropdownlist in asp.net c#

I have four dropdownlistbox in my form, i want to load items into three dropdownlists according to the selection of items from the first drop down list , my first dropdown list has folloding items
Amount ,
PAC ,
Base UOM
Whatever i am selecting from first drop down list , i want to load the same selected item into remaining three dropdownlist
I have tried the following code but it is not working as expected
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Add(uom_Name);
ddl_UOM3.Items.Add(uom_Name);
ddl_UOM4.Items.Add(uom_Name);
}
pls help.
You need to use Ajax Control Tool kit's Cascaded Drop-down
Here is the like for it:
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/Walkthrough/CCDWithDB.aspx
and here is the demo:
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
here is a tutorial link:
http://www.dotnetfox.com/articles/ajax-cascading-dropdown-example-with-database-in-Asp-Net-1078.aspx
Hope this material helps.
May be you don't have set AutoPostBack property to true. Try below code sample :
ASPX:
<asp:DropDownList runat="server" ID="parentDropDown" OnSelectedIndexChanged="parentDropDown_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child1">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child2">
</asp:DropDownList>
Code Behind:
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = parentDropDown.SelectedValue;
child1.Items.Add(uom_Name);
child2.Items.Add(uom_Name);
}
If you want to remove existing item from child before you add then:
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = parentDropDown.SelectedValue;
child1.Items.Clear();
child2.Items.Clear();
child1.Items.Add(uom_Name);
child2.Items.Add(uom_Name);
}
You have to set AutoPostBack="true" to the first drop down list and clear other drop downs items before adding new item.
ASPX:
<asp:DropDownList ID="ddl_UOM" runat="server" OnSelectedIndexChanged="ddl_UOM_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Amount" Value="Amount"></asp:ListItem>
<asp:ListItem Text="PAC" Value="PAC"></asp:ListItem>
<asp:ListItem Text="Base" Value="Base"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_UOM2" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM3" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM4" runat="server"></asp:DropDownList>
ASPX.CS
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Clear();
ddl_UOM2.Items.Add(new ListItem(uom_Name, uom_Name));
ddl_UOM3.Items.Clear();
ddl_UOM3.Items.Add(new ListItem(uom_Name, uom_Name));
ddl_UOM4.Items.Clear();
ddl_UOM4.Items.Add(new ListItem(uom_Name, uom_Name));
}
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Clear();
ddl_UOM3.Items.Clear();
ddl_UOM4.Items.Clear();
ddl_UOM2.Items.Add(uom_Name);
ddl_UOM3.Items.Add(uom_Name);
ddl_UOM4.Items.Add(uom_Name);
}
After writing code in ddl_UOM_SelectedIndexChanged() , call ddl_UOM_SelectedIndexChanged() method from Page_Load()
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
ddl_UOM_SelectedIndexChanged(sender, e );
}
}
catch (Exception)
{
throw;
}
}
You should try to use PageMethods in JS to load the dropdowns any way you like any time you like.

how to maintain dropdownlist selected item after click of a button on the same page

im using an update panel whose update mode is set to conditional. i want to maintain the drop down list selection after I click a button which displays a form to enter information that pertains to that particular selected dropdownlist selection. how can this be done? i enabled view state of the dropdownlist itself to be tru but it isnt working... the list value always goes back to the original default value - 0
<asp:DropDownList ID="DropDownListTug" runat="server" DataSourceID="SqlDataSourceTugs"
DataTextField="Tug_Name" DataValueField="Tug_ID" AutoPostBack="True" AppendDataBoundItems="True"
OnSelectedIndexChanged="ShowNewRateBtn">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="NewTug" runat="server" Text="New Tug" OnClick="NewTug_Click"
CausesValidation="False" Width="74px" />
<asp:SqlDataSource ID="SqlDataSourceTugs" runat="server" ConnectionString="<%$ g %>"
SelectCommand="SELECT [Tug_Name], [Tug_ID] FROM [COMIS_tbl_TugMaster]"></asp:SqlDataSource>
protected void ShowNewRateBtn(object sender, EventArgs e)
{
BtnNewRate.Visible = true;
}
protected void BtnNewRate_Click(object sender, EventArgs e)
{
try
{
processTugs.Visible = true;
allButtons.Visible = true;
BtnSave.Visible = true;
BtnCancel.Visible = true;
// DropDownListTug.Focus();
// DropDownListTug.EnableViewState = true;
// DropDownListTug.SelectedValue = Session.... ;
// }
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}enter code here
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="True"></asp:ListItem>
If you see this line in your GridView code i think you are providing a default value to your dropdown everytime it binds .
Your selection will be lost because of this attribute :-
Selected="True"
i made a silly mistake quite really.. in my Cancel function which i called in the New Rate function, i cleared the selection in there. removed it and its working fine now... thanks alot

How do I bind a DropDownList after postback?

I have an aspx site with a DropDownList in it. I want to be able to choose between 3 variables there and then keep the chosen value for the postback. The page is made so that it loads 10 entries from the database and with this DropDownList I want to be able to choose between 10, 20, 30 entries.
DropDownList
<asp:DropDownList ID="dd1" runat="server" AutoPostBack="True" EnableViewState="True">
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="30">30</asp:ListItem>
</asp:DropDownList>
Here I am trying to set the value that is being sent to the database for the query that brings out the 10, 20 or 30 first entries.
public IEnumerable<XX> repOrder_GetData([ViewState]DateTime? UpdatedRows)
{
var ordrar = _facade.OrderGetForAttest(1, Convert.ToInt32(dd1.SelectedValue));
return ordrar;
}
How do I retain this value during a postback because the page will reload every time you choose something in the DropDownList resulting in only the first value ever being chosen.
protected void Page_Init(object sender, EventArgs e)
{
try
{
_masterpage = this.Master as XX.resource.masterpage.Site;
}
catch (Exception)
{
throw;
}
}
protected void Page_Load(object sender, EventArgs e)
{
// Kolla behörighet första gången.
KollaBehorighet();
_masterpage.ClearMessage();
if (Page.IsPostBack)
{
}
else
{
Page.DataBind();
// Första gången..
PageInit();
FillPage(null);
//FIXME: xxx.Focus();
}
}
You should set the 'OnSelectedIndexChanged' event.
<asp:DropDownList ID="dd1" runat="server" AutoPostBack="True" onselectedindexchanged="ddlItemSelected" EnableViewState="True">
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="30">30</asp:ListItem>
</asp:DropDownList>
protected void ddlItemSelected(object sender, EventArgs e)
{
//Add your selected value to viewstate or session or whatever. Then check this value when binding on postback.
Viewstate["myValue"] = dd1.SelectedValue;
}
You can try to store it in Session state and then load it from session
Some like this:
Session["Selected"] = dd1.SelectedIndex;
And in Load Event you can use:
dd1.SelectedIndex = Convert.ToInt32(Session["Selected"]);

Categories

Resources