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

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
}

Related

Need to set 1st value as select in Drop down list when the data source is table in database

<asp:DropDownList ID="ddl_Role" runat="server" DataSourceID="Sql_role"
DataTextField="Role_Name" DataValueField="Role_ID">
</asp:DropDownList>
<asp:SqlDataSource ID="Sql_role" runat="server"
ConnectionString="<%$ ConnectionStrings:Mytime_role %>"
SelectCommand="SELECT * FROM [Role_Alert]"></asp:SqlDataSource>
<asp:RequiredFieldValidator ID="Rfv_role" runat="server" ControlToValidate="ddl_role"
Display="Dynamic" ErrorMessage="*Role is Required field" Font-Italic="True" ForeColor="Red"
InitialValue="0"></asp:RequiredFieldValidator>
it gives only the starting text in database table as first value in Drop down list .so i need select as default one then the remaining list as usual
You set the selectedItem of the dropdownlist after the DataBind method.
dlTest.DataSource = // some datasource;
dlTest.DataBind();
dlTest.Items.FindByValue(search_value).Selected = true;
You can search for the item, either using FindByText or FindByValue. Alternatively, if you could store the selected item text/value, then you can set the dropdownlist selected item in the DataBound event. This will be fired after the data has been binded to dropdownlist.
protected void dlTest_DataBound(object sender, EventArgs e) {
dlTest.Items.FindByValue(search_value).Selected = true;
}
<asp:DropDownList ID="DropDownListID" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="Default text" Value="Default value" />
</asp:DropDownList>
Add a default item and also attribute AppendDataBoundItems="true"

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();
}

How to Ensure a DropDownList is Required if a given Radio Button is selected?

I have a RadioButtonLIst as follows:
<asp:RadioButtonList runat="server" ID="Location" ValidationGroup="formVal">
<asp:ListItem Value="Beverly Hills" />
<asp:ListItem Value="Seattle" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator runat="server" ID="rfvLocation"
ControlToValidate="Location">
</asp:RequiredFieldValidator>
If the first radio button is selected, I need to require a selection in the following dropdownlist:
<asp:DropDownList runat="server" ID="Food" ValidationGroup="formVal">
<asp:ListItem Text="Chicken" Value="Chicken"></asp:ListItem>
<asp:ListItem Text="Beef" Value="Beef"></asp:ListItem>
<asp:ListItem Text="Fish" Value="Fish"></asp:ListItem>
</asp:DropDownList>
Would I use a CustomValidator to accomplish this? If so, what validation expression should be used? If a CustomValidator isn't a good fit, which validator is suggested?
Thanks much:)
UPDATE: Added the following to code behind
protected void LocationChanged(object sender, EventArgs e)
{
if (Location.SelectedIndexChanged == true)
{
rfvFood.Enabled = true;
}
else rfvFood.Enabled = false;
}
Is this correct?
You could do the following:
add a RequiredFieldValidator for the DropDownList and set its enabled property to false
set AutoPostBack=true on the RadioButtonList
in code behind, handle the RaditButtonList's OnSelectedIndexChanged event
Here, add logic that enables or disables the RequiredFieldValidator depending on RadioButtonList's selected item
If you don't like the whole page refreshing when the user chooses an option, wrap these controls up inside an UpdatePanel.

Bound Drop Down List changes to first item in list

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;
}

give the ID as a custom attribute to checkbox in a grid for manual update

I like to update just the value of my checkbox in a asp grid.
so i thought i bind the id to the checkbox ( but how?) and fire an update in code behind by clicking the checkbox.
but how can I get the ID, and where I have to bind this id to get it in code behind in the event?
Thanks
Here is what I've managed to do. (Be aware there should be an easier way to do this. I'm very new to ASP.NET)
Here you have a TemplateField in a GridView. Inside it there is an UpdatePanel and the CheckBox is inside it. This is done to make the checking of the TextBox do post in the backgroung (ajax). You might not need it (UpdatePanel) at all.
<asp:TemplateField HeaderText="Private" SortExpression="IsPrivate">
<ItemTemplate>
<asp:UpdatePanel ID="upIsPrivate" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:CheckBox ID="chkIsPrivate" runat="server" OnCheckedChanged="chkIsPrivate_CheckedChanged" AutoPostBack="true" />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
And this is the method that handles this. Notice that I get the Id from the GridViewRow that contains the CheckBox:
GridViewRow row = (GridViewRow)((CheckBox)sender).Parent.Parent.Parent.Parent;
protected void chkIsPrivate_CheckedChanged(object sender, EventArgs e)
{
if (editMode)
{
GridViewRow row = (GridViewRow)((CheckBox)sender).Parent.Parent.Parent.Parent;
Int32 id = (Int32)uxPhoneCallList.DataKeys[row.RowIndex]["Id"];
CheckBox isPrivate = (CheckBox)row.FindControl("chkIsPrivate");
PhoneCall phoneCall = PhoneCallManager.GetById(id);
...
}
}

Resources