How to show selected index on top in DropDownList? - asp.net

Actually i am displaying months in first list on selecting the month if the value of month is other than 31 it shows at top otherwise it shows jan at top how to show the other one?
Here is code:
Code behind:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
for (int i = 1; i <= int.Parse(DropDownList1.SelectedValue); i++)
{
DropDownList2.Items.Add(new ListItem("" + i));
}
}
Designer source:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="31">Jan</asp:ListItem>
<asp:ListItem Value="29">Feb</asp:ListItem>
<asp:ListItem Value="31">Mar</asp:ListItem>
<asp:ListItem Value="30">April</asp:ListItem>
<asp:ListItem Value="31">May</asp:ListItem>
<asp:ListItem Value="30">June</asp:ListItem>
<asp:ListItem Value="31">July</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
Problem is:
If i select march or may or any item with value 31 jan is shown at to while in other case the selected one is shown.

Values of ListItems should be unique.
Default selected item is Jan (value = 31), so everything will work fine when you click on items with another values (29 and 30).
When you click on Mar, May, July (value = 31), then Jan becomes selected.
To achieve the behavior you want, use another approach.
The best solution is:
using System.Linq;
int count = DateTime.DaysInMonth(
DateTime.Today.Year,
int.Parse(DropDownList2.SelectedIndex + 1)); // sic!
DropDownList2.Items.AddRange(
Enumerable.Range(1, count)
.Select(i => new ListItem(i.ToString()))
.ToArray());
So you don't need to hard-code anything! Everything is already in .NET FCL. Just determine month's number from it's index into the list.

I did some research and this is not an uncommon problem. On post-back asp.net will show the first item in the list with the selected value. The only way I've found around it is to make all of the values unique, such as:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="Jan-31">Jan</asp:ListItem>
<asp:ListItem Value="Feb-29">Feb</asp:ListItem>
<asp:ListItem Value="Mar-31">Mar</asp:ListItem>
<asp:ListItem Value="April-30">April</asp:ListItem>
<asp:ListItem Value="May-31">May</asp:ListItem>
<asp:ListItem Value="June-30">June</asp:ListItem>
<asp:ListItem Value="July-31">July</asp:ListItem>
</asp:DropDownList>
Then in your DropDownList1_SelectedIndexChanged event use a string.split on '-' to get the day count value.

Related

Missing something nested in FindControl but always get Null

Literally have read and tried all solutions in Stackoverflow to no avail.
I am trying to get the value of different Dropdownlists genereated from a Repeater and another one just generated on the go, plain and simple.
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
I am trying to get all the selectedValue's of all the dropdownLists genereated thru Repeater and the one that is plain and simply there.
<asp:DropDownList ID="reasonFamily" runat="server">
<asp:ListItem Enabled="true" Text="--------" Value="-1"></asp:ListItem>
<asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
which looking in the front end, I am getting something like...
<select name="ctl00$ContentPlaceHolder1$reasonFamily" id="ContentPlaceHolder1_reasonFamily">
Which looks good, since I did a button and tried to get this value...
DropDownList ctr = Page.FindControl("Content2").FindControl("reasonFamily") as DropDownList;
TextBox.Text = ctr.SelectedValue;
I could just get the value of reasonFamily.SelectedValue and get finished with... But the problem is, there is another section with a repeater that creates several DropDownList and I need to find all of them and get all their values and Send them to the DB. The DropDownList is all nested in...
<asp:Repeater ID="repStudents" runat="server">
<asp:DropDownList ID="reasonStd" runat="server">
<asp:ListItem Enabled="true" Text="--------" Value="-1"></asp:ListItem>
<asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
I tried getting finding all of the dropdownlist, but all I get is NULL.
UPDATE: I was able to get the one that is not in the Repeater, but other still eludes me, even tho I have almost tried all the possible choices.
<select name="ctl00$ContentPlaceHolder1$reasonFamily" id="ContentPlaceHolder1_reasonFamily"> // This I was able to access with:
DropDownList ctr = this.Master.FindControl("ContentPlaceHolder1").FindControl("reasonFamily") as DropDownList;
<select name="ctl00$ContentPlaceHolder1$repStudents$ctl01$reasonStd" id="ContentPlaceHolder1_repStudents_reasonStd_1"> //This I could not. Tried all of this:
DropDownList ctr = this.Master.FindControl("ContentPlaceHolder1").FindControl("reasonStd_1") as DropDownList;
DropDownList ctr = this.Master.FindControl("ContentPlaceHolder1").FindControl("repStudents").FindControl("reasonStd_1") as DropDownList;
DropDownList ctr = this.Master.FindControl("ContentPlaceHolder1").FindControl("repStudents")FindControl("reasonStd").FinControl("1") as DropDownList;
Ok, so the 2nd or some other repeater issue - we leave that separate for now.
that last answer SHOWS how we can grab those values - it really the same code.
So, you have a repeater. It has maybe 1 or 15 rows. So, we want to get/grab the drop down list from each row of the repeater.
So, say we have this markup in the repeater (I included your above dropdown list).
So, we have this simple markup:
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<div style="border-style:solid;color:black;width:250px">
<div style="padding:5px;text-align:right">
First Name: <asp:TextBox ID="txtFirst" runat="server" Text ='<%# Eval("FirstName") %>' Width="130px" />
<br />
Last Name: <asp:TextBox ID="txtLast" runat="server" Text ='<%# Eval("LastName") %>' Width="130px" />
<br />
<asp:DropDownList ID="reasonFamily" runat="server">
<asp:ListItem Enabled="true" Text="--------" Value="-1"></asp:ListItem>
<asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
</asp:DropDownList>
<br />
</div>
</div>
</ItemTemplate>
Ok, now it don't matter if this has 2, or 30 rows. Lets assume it has 3 rows of data. so the above now shows this:
So say when we click on the above button, then we need code (the SAME code in the last question). so the code to process each row, get the values (including the dropdown) could be like this for that button click:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem rRow in Repeater1.Items)
{
// get First Name.
Debug.Print(rRow.FindControl("txtFirst") as TextBox.Text);
// get LastName
Debug.Print(rRow.FindControl("txtLast") as TextBox.Text);
// get value of drop down box
string strDropDownChoice = rRow.FindControl("reasonFamily") as DropDownList.Text;
Debug.Print("Drop Down option picked = " + strDropDownChoice);
}
}
And output from above loop code would be this:
Output:
Albert
Kallal
Drop Down option picked = 1
Darcy
Caroll
Drop Down option picked = 2
Don
Grant
Drop Down option picked = 2
So, once again, I don't see any real issue or problem with looping over the rows in the data repeater, and then pulling out the values from text box, check box, or a drop down list - it is all quite much the same.

ASP.NET Set RadioButton to Select by a String Value

I have a RadioButton control with 3 buttons.
I am retrieving a value from SharePoint list, and to select the RR control according to the value retrieved.
Which of the following is the correct method to use?
ResignationTypeRBList.SelectedValue = _request.ResignationType;
ResignationTypeRBList.SelectedItem = _request.ResignationType;
ResignationTypeRBList.SelectedIndex = _request.ResignationType;
<asp:RadioButtonList ID="ResignationTypeRBList" RepeatDirection="Horizontal" runat="server" CausesValidation="false">
<asp:ListItem Text="Resignation" Value="Resignation" Selected="True"></asp:ListItem>
<asp:ListItem Text="NPL" Value="NPL"></asp:ListItem>
<asp:ListItem Text="Others" Value="Others"></asp:ListItem>
</asp:RadioButtonList>
Thank you
If you are getting Value from Sharepoint list, then assign that to SelectedValue
ResignationTypeRBList.SelectedValue = "NPL";

how to select multiple items in ListBox

How can I select multiple item in ListBox in VB.NET code.
Example:
I have item 1, 5, 8 - then I want to select those items in ListBox in page load
ListBoxSample item
Item “aa1” Value =1
Item “aa2” Value =2
Item “aa3” Value =3
Item “aa4” Value =4
Item “aa5” Value =5
Item “aa6” Value =6
Item “aa7” Value =7
Item “aa8” Value =8
You first need to ensure that multiple items can be selected(default is single-selection). Therefore use the SelectionMode-property:
<asp:ListBox id="listBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="1">aa1</asp:ListItem>
<asp:ListItem Value="2">aa2</asp:ListItem>
<asp:ListItem Value="3">aa3</asp:ListItem>
<asp:ListItem Value="4">aa4</asp:ListItem>
<asp:ListItem Value="5">aa5</asp:ListItem>
<asp:ListItem Value="6">aa6</asp:ListItem>
<asp:ListItem Value="7">aa7</asp:ListItem>
<asp:ListItem Value="8">aa8</asp:ListItem>
</asp:ListBox>
In an ASP.NET-ListBox there is no SelectedIndices property or something like that. So you need to loop them and set the ListItem's Selected property accordingly:
string[] selectedValues = { "1", "5", "8" }; // from session or whatever
foreach (ListItem item in listBox1.Items)
item.Selected = selectedValues.Contains(item.Value);
Remember to do that only if(!Page.IsPostBack) and not on every postback in Page_Load. Otherwise you're overwriting the user-changes and the SelectedIndexChanged-event won't fire.
To enable multi selection you need to set the selection mode to multiple in windows application
ListBox1.SelectionMode = SelectionMode.MultiSimple
ListBox1.SetSelected(1, True)
ListBox1.SetSelected(5, True)
ListBox1.SetSelected(8, True)
If it is ASP listBox then you can statically selected items as
<asp:ListBox id="drop1" rows="3" runat="server" AutoPostBack="True">
<asp:ListItem selected="true">Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem selected="true">Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
<asp:ListItem>Item 7</asp:ListItem>
<asp:ListItem selected="true">Item 8</asp:ListItem>
</asp:ListBox>
If you want to set it dynamically means you can use
drop1.SelectionMode = ListSelectionMode.Multiple
drop1.Items(0).Selected = True
drop1.Items(1).Selected = True
drop1.Items(2).Selected = True

How to remove appended items from DropDownlist on postback in ASP.NET and C#

I have two DropDownList controls in cascade (the second one is populated based on the selection made on the first one):
<asp:DropDownList ID="ddlProduct" runat="server" AppendDataBoundItems="true"
AutoPostBack="true" onselectedindexchanged="ddlProduct_SelectedIndexChanged">
<asp:ListItem Value="" Selected="True"> - Product - </asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" AppendDataBoundItems="true">
<asp:ListItem Value="" Selected="True"> - Category - </asp:ListItem>
</asp:DropDownList>
In the ddlProduct.SelectedIndexChanged event handler I have the following code:
ddlCategory.DataSource = _productService.GetCategoryByProductId(ddlProduct.SelectedValue);
ddlCategory.DataTextField = "CategoryName";
ddlCategory.DataValueField = "CategoryId";
ddlCategory.DataBind();
The first time a user choose a Product from ddlProduct the ddlCategory is populated correctly. The following times, the Categories in ddlCategory are appended to those that were selected the first time and so on. I tried to put in ddlProduct.SelectedIndexChanged:
ddlCategory.Items.Clear();
but the method removes also the hardcoded item
<asp:ListItem Value="" Selected="True"> - Category - </asp:ListItem>
How can I just delete the appended item from ddlCategory?
You could set AppendDataboundItems="false" and insert the default item manually, for example in DropDownList.DataBound event:
protected void ddlCategory_DataBound(object sender, EventArgs e)
{
if (ddlCategory.Items.Count > 0)
{
ddlCategory.Items.Insert(0, " - Category - ");
ddlCategory.Items[0].Value = "";
ddlCategory.SelectedIndex = 0;
}
}
}
Remove AppendDataBoundItems="true" and append empty item manually after DataBind method call
After removing category items you can add default category using code...
ddlCategory.Items.Add(new ListItem("- Category -", "0", true));

DropDownList - First Item in the List is not posting back properly but all the others work

I have this DropDownList:
<asp:DropDownList id="ddlSort" runat="server" EnableViewState="true" OnSelectedIndexChanged="ApplyFilters" AutoPostBack="True">
<asp:ListItem Value="0">Sort By Relevancy</asp:listitem>
<asp:ListItem Value="1">Sort By Product Code</asp:listitem>
<asp:ListItem Value="2">Sort By Product Description</asp:listitem>
<asp:ListItem Value="3">Sort By Price - Lowest</asp:listitem>
<asp:ListItem Value="4">Sort By Price - Highest</asp:listitem>
</asp:dropdownlist>
I am setting the option to be initially selected in the Page_Load in if (!Page.IsPostBack) which defaults to "0". If I select Sort By Product Code (or any other option) it will call ApplyFilters without a problem. If I then change the Drop Down to the first option, the AutoPostBack is fired but it doesn't go into the ApplyFilters code.
If I change the markup to this:
<asp:DropDownList id="ddlSort" runat="server" EnableViewState="true" OnSelectedIndexChanged="ApplyFilters" AutoPostBack="True">
<asp:ListItem Value="-1">First Option</asp:listitem>
<asp:ListItem Value="0">Sort By Relevancy</asp:listitem>
<asp:ListItem Value="1">Sort By Product Code</asp:listitem>
<asp:ListItem Value="2">Sort By Product Description</asp:listitem>
<asp:ListItem Value="3">Sort By Price - Lowest</asp:listitem>
<asp:ListItem Value="4">Sort By Price - Highest</asp:listitem>
</asp:dropdownlist>
then selecting Sort By Relevancy will work properly after selecting any other option BUT selecting First Option won't (as in the first example).
I am using ASP.NET 2.0.
I have checked the control hierarchy and the view state is enabled for all the controls.
I have tried explicitly setting EnableViewState="true" without any success.
Does anyone have any ideas as all the information I have seen on this points to the view state being false and in an update panel - Neither of which are true in my case.
EDIT: I have put a dummy entry in for now as the first entry but this is not ideal.
This is the expected behavior.
Your event is OnSelectedIndexChanged, if open the dropdown and select the already selected option, asp.net will not detect that as a change.
Only when you select a different value, will the OnSelectedIndexChanged be fired.
I suggest having a default value in place, like in your second example, just more readable:
<asp:DropDownList id="ddlSort" runat="server" EnableViewState="true" OnSelectedIndexChanged="ApplyFilters" AutoPostBack="True">
<asp:ListItem Value="-1">Select Sort order</asp:listitem>
<asp:ListItem Value="0">Sort By Relevancy</asp:listitem>
<asp:ListItem Value="1">Sort By Product Code</asp:listitem>
<asp:ListItem Value="2">Sort By Product Description</asp:listitem>
<asp:ListItem Value="3">Sort By Price - Lowest</asp:listitem>
<asp:ListItem Value="4">Sort By Price - Highest</asp:listitem>
</asp:dropdownlist>
If you have a default sort order, you can set it on page load
if (!Page.IsPostBack)
{
ddlSort.Text = 0;
}

Resources