DataBound CheckBoxList - asp.net

I have a website programmed in Asp.Net and use a ListView for displaying data. The data is coming from a LinqDataSource.
In my EditItemTemplate I have a CheckBoxList which consist of:
<asp:CheckBoxList runat="server" ID="TypeCheckBoxList" RepeatColumns="2">
<asp:ListItem Value="128">6.-10. klasse<br />Norddjurs vejleder</asp:ListItem>
<asp:ListItem Value="64">6.-10. klasse<br />Syddjurs vejleder</asp:ListItem>
<asp:ListItem Value="32">Gået ud af skolen<br/>Norddjurs vejleder</asp:ListItem>
<asp:ListItem Value="16">Gået ud af skolen<br/>Syddjurs vejleder</asp:ListItem>
<asp:ListItem Value="8">Ekstra støtte<br/>Norddjurs vejleder</asp:ListItem>
<asp:ListItem Value="4">Ekstra støtte<br />Syddjurs vejleder</asp:ListItem>
<asp:ListItem Value="2">Kontakt</asp:ListItem>
<asp:ListItem Value="1">Om os<br />Medarbejdere</asp:ListItem>
</asp:CheckBoxList>
I have a column called Type in my db and it is a tinyint. Therefore I can say (byte)Eval("Type").
But how do I databind my Eval("Type") to the CheckBoxList so if Eval("Type") is 3, then the two last items are selected?
I have tried setting a hidden value which binds to Type and then in the CheckBoxList OnLoad setting the selected items. But that did'nt work.

That's the way to do it, with the hidded value binding to Type, but on the ItemDataBound event of the ListView.
So the event would look something like this:
protected void ListViewId_ItemDataBound (object sender, ListViewItemEventArgs e)
{
HiddenField hdfType = (HiddenField)e.Item.FindControl("hdfType");
CheckBoxList TypeCheckBoxList = (HiddenField)e.Item.FindControl("TypeCheckBoxList");
// and you put the hidden just for EditItem and do:
if (hdfType != null)
foreach (ListItem item in TypeCheckBoxList.Items)
if (int.Parse(item.Value) < int.Parse(hdfType.Value))
item.Selected = true;
}
(I wrote all of this from my head, so there might be some small mistakes)

First you should write a javascript function like this
function Selected(value,type)
{
if(value<type)
return true;
else
return false;
}
<asp:ListItem Value="32" Selected= javascript:function Selected(32,Eval("Type"))>Gået ud af skolen<br />Norddjurs vejleder</asp:ListItem>
<asp:ListItem Value="16" Selected= javascript:function Selected(16,Eval("Type")>Gået ud af skolen<br />Syddjurs vejleder</asp:ListItem>
little bit modification may be required to finalize it..major focus on Selected attribute

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
}

data binding to dropdownlist in asp.net?

For a Label in we bind a data using
<asp:Label ID="Label2" runat="server" Text='<%#Eval("address") %>'></asp:Label>
how to bind data to dropdownlist like that?
asp:DropDownList ID="droplist" runat="server" >
<asp:ListItem Text="admin"></asp:ListItem>
<asp:ListItem Text="manager"></asp:ListItem>
</asp:DropDownList>
Like this....
<asp:DropDownList ID="droplist" runat="server" SelectedValue='<%#Eval("fieldname")%>'>
<asp:ListItem Text="admin"></asp:ListItem>
<asp:ListItem Text="manager"></asp:ListItem>
</asp:DropDownList>
Note that intellisense will not pick SelectedValue out. You will of course need to populate the dropdown with the data... using any method that suits
Either put your datasource in the DropDownList declaration like here: populate dropdownlist
Or use Codebehind like this:
Eg: inside Page_Load():
List<string> ItemsToGoInDropDown = new List<string>{"manager", "admin", "etc"};
droplist.DataSource = ItemsToGoInDropDown;
droplist.DataBind();
Put the data in hidden field. Then asigen that in drop down in Gridview Rowdatabound Event.Like This.
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hf = (HiddenField)e.Row.FindControl("hf");
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
ddl.SelectedValue = hf.Value;
}

dropdown in gridview, find GridRow on OnSelectedIndexChanged

I have a asp.net gridview. A template field of type dropdownlist is contained in a column:
<asp:TemplateField HeaderTextLabel="strManagedOETeamart">
<ItemTemplate>
<asp:DropDownList runat="server" AutoPostBack="True" OnSelectedIndexChanged="SelectedTeamartChanged">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Silver"> Silver </asp:ListItem>
<asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
<asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
<asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
How can I get the GridViewRow when the user selects another item?
protected void SelectedTeamartChanged(object sender, EventArgs e)
{
DropDownList dropDown = (DropDownList) sender;
//I would like to know the GridViewRow this DropDownList is in
}
You're looking for the NamingContainer property:
GridViewItem row = (GridViewRow)dropDown.NamingContainer;
By the way, that works for any kind of web databound control like GridView, DataList,Repeater or ListView.
Consider this more complex requirement: you have a GridView which is nested in another GridView. Now you're handling a DropDownList's SelectedIndexChanged event that is inside the child's GridView and you want to get the reference to the GridViewRow of the parent GridView:
var control = (Control)sender;
var row = (GridViewRow)control.NamingContainer;
var parentRow = (GridViewRow)row.NamingContainer.NamingContainer;
That's the safest and easiest way to get it.

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

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.

Resources