Bound Drop Down List changes to first item in list - asp.net

I have two drop down list on a page. The first one list projects and the second list users.
The userlist is populated with an object datasourse that pulls a list of users for the selected Project.
Whenever the Project list selection changes the second ddl Userlist always reverts to the first person in the list instead the person that was selected before a new Project was chosen.
I want to be able to select a new project and not have the selected person in the UserList change.

You'll need to store the Id of the user that is currently selected before you do you databinding. One way would be to handle SelectedIndexChanged on your Project ddl so that you can grab the user id of the selected item in your User ddl then do the binding manually. Once the binding is done, then you can attempt to set the SelectedValue of the ddl to the User Id you had stored.
EDIT: Added an example:
In your aspx:
<asp:DropDownList ID="projectddl" runat="server" AutoPostBack="true" OnSelectedIndexChanged="projectddl_SelectedIndexChanged">
<asp:ListItem Text="Project 1" Value="1" />
<asp:ListItem Text="Project 2" Value="2" />
<asp:ListItem Text="Project 3" Value="3" />
</asp:DropDownList>
<asp:DropDownList ID="usersddl" runat="server">
</asp:DropDownList>
In your code-behind:
protected void projectddl_SelectedIndexChanged(object sender, EventArgs e)
{
string currentlySelectedUserId = usersddl.SelectedValue;
// Do your user databinding here based on project selected
usersddl.SelectedValue = currentlySelectedUserId;
}

Related

How to retreive values in DropwnList for all selected chekboxes, and return to default when unchecked?

Default dropdownlist values include locations for all books. When I select a checkbox the the dropdownlist should populate only the locations where that book is available.In back-end, My Stored procedure is working fine for single parameter and multiple values.
But in UI, I can get locations for only one checkbox, if I click second checkbox the dropdown list does not include the value for that second checkbox.
Also when I unclick all it should return to default.
Any hint or help will be appreciated.
<label>BooksLocations</label>
<asp:DropDownList runat="server" ID="drpdwnListLocationBooks" CssClass="formcontrol">
</asp:DropDownList>
<label>Books</label>
<asp:CheckBoxList runat="server" ID="chkboxListbookStatus">
<asp:ListItem Text="Book A"></asp:ListItem>
<asp:ListItem Text="Book B"></asp:ListItem>
<asp:ListItem Text="Book C"></asp:ListItem>
</asp:CheckBoxList>
Add below code
<asp:CheckBoxList runat="server" ID="chkboxListbookStatus"
AutoPostBack="True" OnSelectedIndexChanged="chkboxListbookStatus_SelectedIndexChanged">
Then in code behind
protected void chkboxListbookStatus_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList list = sender as CheckBoxList;
ListItem box = list.SelectedItem;
//add code to get data from SP to fill data in dropdown
}

'ddpJobType' has a SelectedValue which is invalid because it does not exist in the list of items

im trying to bind the data from database to dropdownlist and i want to set the default value as "select". but i got this error.
My Code is:
<asp:DropDownList ID="ddpJobType" runat="server" Width="226px" Height="26px"
onselectedindexchanged="ddpJobType_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="0" Text="--Please Select Type--"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ErrorMessage="Please Select JobType " Font-Size="Small"
ForeColor="Red" ControlToValidate="ddpJobType"
ValidationGroup="VGPJobPost"></asp:RequiredFieldValidator>
If you are banding data using DataSource, then your default item will not be available after the call of DataBind function. You need to add it manually after calling databind something like below
dropDownList.DataSource= yourdataSource;
dropDownList.DataBind();
dropDownList.Items.Insert(0, new ListItem("--Please Select Type--", "0"));
Another option is in the designer you need to specifiy
AppendDataBoundItems=true
This will make sure your designtime list items are present after databind

Populating Drop down dynamically using ASP.NET

In one of my form there were two Dropdown fields. The second dropdown has to be populated from the database, dynamically from the selection of first dropdown.
Any help is appreciated.
In the SelectedIndexChanged event of the first DropDownList, add code to populate the second DropDownList based on the selected value of the first list
Like this:
<asp:DropDownList runat="server" ID="from" AutoPostBack="true" CausesValidation="false" OnSelectedIndexChanged="from_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="to" />
protected void from_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedValue = this.from.SelectedValue;
this.to.DataSource = /*get the data of the second list based on: selectedValue*/;
this.to.DataBind();
}

ASP.NET DropDownList - GetSelectedIndices missing one selected item

Maybe I'm losing my mind - I thought this was straight forward.
ListCode.DataTextField = "code_desc";
ListCode.DataValueField = "code_id";
ListCode.DataSource = Foo.GetCodes();
ListCode.DataBind();
The selection mode is set to multipls and all is good, about 50 items with appropriate values displays. Then I select 5 items and submit the form. I do a
int[] indices = ListCode.GetSelectedIndices();
and the array only has the first four items that I selected. It seems that if I select multiple items in the list and submit the form, I'm only able to retrieve all but the last selected item - it doesn't matter if I use GetSelectedIndices or if I iterate through each item in the list.
Any help would be greatly appreciated.
You must be using a ListBox I assume. I made the following test app:
<asp:ListBox ID="lstTest" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="Test1" Value="1" />
<asp:ListItem Text="Test2" Value="2" />
<asp:ListItem Text="Test3" Value="3" />
<asp:ListItem Text="Test4" Value="4" />
</asp:ListBox>
<asp:Button ID="btnTest" runat="server" Text="Test" OnClick="btnTest_Click" />
<asp:Label ID="lblTest" runat="server" />
protected void btnTest_Click(object sender, System.EventArgs e)
{
int[] selectedIndexes = lstTest.GetSelectedIndices();
lblTest.Text = selectedIndexes.Length.ToString();
}
Seems to work fine so I have to assume it has something to do with your binding or when you are fetching the indicies. Can you post a trimmed down version of your broken code?
Not really an answer, but I swapped in a checkboxlist and it worked like a champ.

Changing WebControl ID Inside of a Repeater

<ItemTemplate>
<asp:Label runat="server"><%#DataBinder.Eval(Container.DataItem, "Question")%></asp:Label>
<asp:DropDownList runat="server" id="<%#DataBinder.Eval(Container.DataItem, "QuestionID")%>">>
<asp:ListItem value="1" text="Yes" />
<asp:ListItem value="0" text="No" />
</asp:DropDownList>
<ItemTemplate>
This is roughly what I'm trying to do. Obviously, the implementation is faulty, but I can't find any information on how I'd go about this in practice. Any help is appreciated.
Edit: What I'm trying to do exactly is add a DropDownList for each item in this Repeater, and upon submission of the form, use the ID's of each Yes/No answer to input into a database. The SqlDataReader that I'm using has two fields: The question content and the questionID.
I think you'd be better off using the built in support for IDs inside a Repeater. If the goal is to assign it an ID to make it easy to find the proper control after the data has been bound you might try something like this:
<asp:Repeater ID="Repeater1" runat="server>
<ItemTemplate>
<asp:Label ID="QuestionID" Visible="False" Runat="server"><%#DataBinder.Eval(Container.DataItem, "FieldContent")%></asp:Label>
<asp:DropDownList ID="MyDropDownList" Runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
Then, in your code you can loop through the items in the Repeater until you find the label you're looking for:
foreach (RepeaterItem curItem in Repeater1.Items)
{
// Due to the way a Repeater works, these two controls are linked together. The questionID
// label that is found is in the same RepeaterItem as the DropDownList (and any other controls
// you might find using curRow.FindControl)
var questionID = curRow.FindControl("QuestionID") as Label;
var myDropDownList = curRow.FindControl("MyDropDownList") as DropDownList;
}
A Repeater basically consists of a collection of RepeaterItems. The RepeaterItems are specified using the ItemTemplate tag. Each RepeaterItem has its own set of controls that are, by the very nature of a Repeater, associated with each other.
Say you're pulling the Repeater data from a database. Each Repeater item represents data from an individual row in the query results. So if you assign the QuestionID to a label and the QuestionName to a DropDownList, the ID in the label would match up with the name in drop down.
Could you remove the control from the markup file, and hook the repeater's onItemDataBound event. In that event, you should be able to create the dropdown control "manually", assigning whatever ID you want.

Resources