I have cascading dropdowns one is for topics and the other for sections. I was hoping to be able to use tooltips to show the description of each topic and section. However I first have to choose a particular topic and or a section to get the tooltip to show up and also the only description that shows up is for the one at the bottom of the dropdown regardless if its selected or not. Any ideas what I'm doing wrong?
Below is how I'm loading the topic dropdown. Load_Topic1() is being called on the Page_Load method.
protected void Load_Topic1()
{
var topics = ReadTopics();
foreach (var topic in topics)
{
var topicListItem = new ListItem(topic.Name, topic.Id.ToString());
topic1.Items.Add(topicListItem);
topic1.Attributes.Add("Title",topic.Description);
}
topic1.Items.Insert(0, new ListItem("--- Select Topic ---", "0"));
}
Here are my cascading dropdowns:
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="topic1" DataTextField="NAME" DataValueField="ID" OnSelectedIndexChanged="Load_Section1" AutoPostBack="True" AppendDataBoundItems="true" runat="server"/>
<asp:DropDownList ID="section1" DataTextField="NAME" DataValueFile="ID" runat="server">
<asp:ListItem Text="--- Select Section ---" Value="0"></asp:ListItem>
</asp:DropDownList><br/>
<asp:RequiredFieldValidator runat="server" ID="topic1ReqVal" InitialValue="0" ControlToValidate="topic1" errormessage="Please select a topic"/>
<asp:RequiredFieldValidator runat="server" ID="section1ReqVal" InitialValue="0" ControlToValidate="section1" errormessage="Please select a section"/><br/>
</ContentTemplate>
</asp:UpdatePanel>
The 2nd dropdown or section1 dropdown is being given its information from this method:
protected void Load_Section1(object sender, EventArgs e)
{
section1.Items.Clear();
var sections = ReadForTopic(Guid.Parse(topic1.SelectedValue));
foreach (var section in sections)
{
var sectionListItem = new ListItem(section.Name, section.Id.ToString());
section1.Items.Add(sectionListItem);
section1.Attributes.Add("Title", section.Description);
}
section1.Items.Insert(0, new ListItem("--- Select Section ---", "0"));
}
You adding the attribute just for the dropdown, not for each element in the dropdown.
What you need to do is:
foreach (var topic in topics)
{
var topicListItem = new ListItem(topic.Name, topic.Id.ToString());
topicListItem.Attributes.Add("Title",topic.Description);
topic1.Items.Add(topicListItem);
}
And of course the same for section. This should give each select element in your option and title.
Cheers,
Related
I have a drop down list in a repeater. I am trying to add a required field validator to it.
The aspx code is:
<asp:Repeater ID="myRepeter" runat="server" OnItemDataBound="myRepeter_ItemDataBound">
<ItemTemplate>
<asp:DropDownList ID="ddl_Name" runat="server" DataTextField="value" DataValueField="key" ></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfv_Name" ControlToValidate="ddl_Name" InitialValue="0" runat="server" ErrorMessage="Please select a Name" ValidationGroup="valgrp_Name" ForeColor="Red"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:Repeater>
I also tried the same from code behind:
protected void myRepeter_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
foreach(RepeaterItem item in myRepeter.Items)
{
DropDownList NametList = item.FindControl("ddl_Name") as DropDownList;
RequiredFieldValidator validator = item.FindControl("rfv_Name") as RequiredFieldValidator;
validator.ControlToValidate = NametList .ID;
validator.ValidationGroup = "valgrp_Name";
}
}
How can I add the required field validator?
Why are you looping through your repeater inside the databound event? It will automatically loop. Try using e.Item.FindControl instead.
DropDownList NametList = e.Item.FindControl("ddl_Name") as DropDownList;
RequiredFieldValidator validator = e.Item.FindControl("rfv_Name") as RequiredFieldValidator;
validator.ControlToValidate = NametList.ID;
validator.ValidationGroup = "valgrp_Name";
First, Can repeater with in a repeater be used?
If yes than how I can use nested repeater in following scenario.
<div class="row">
<asp:Repeater ID="rp_Question" runat="server">
<ItemTemplate>
<p class="_100">
<h2 id="h4_Question" runat="server"><%# Eval("question_text") %></h2>
</p>
<p class="left">
<asp:RadioButtonList ID="rb_Question" runat="server">
<asp:ListItem Text="Option1" Value="1"></asp:ListItem>
<asp:ListItem Text="Option2" Value="2"></asp:ListItem>
<asp:ListItem Text="Option3" Value="3"></asp:ListItem>
<asp:ListItem Text="Option4" Value="4"></asp:ListItem>
</asp:RadioButtonList>
</p>
</ItemTemplate>
</asp:Repeater
Repeater Binding
rp_Question.DataSource = _question.GetAll();
rp_Question.DataBind();
The options of each question are saved in database, minimum option could be 3 and maximum could be 6. How can I use an other repeater inside rp_Question to repeat options of each question.
I want to show out put like this.
Expanding on the answer KateCute gave, you can use the ItemDataBound event for that.
<asp:Repeater ID="rp_Question" runat="server" OnItemDataBound="rp_Question_ItemDataBound">
And then in code behind.
protected void rp_Question_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//find the radiobuttonlist with findcontrol and cast back to it's original type
RadioButtonList rb_Question = e.Item.FindControl("rb_Question") as RadioButtonList;
//get the current datarow
DataRowView row = e.Item.DataItem as DataRowView;
//get the id from the datarow object
string questionID = row["question_id"].ToString();
//get the answers from the db with questionID and bind them as listitems just like in the loop below
//just a loop to add some listitems for demo
for (int i = 0; i < 5; i++)
{
rb_Question.Items.Insert(i, new ListItem("Option " + i.ToString(), i.ToString(), true));
}
}
Unfortunately, you can't use repeater inside asp:RadioButtonList. It allows only ListItem inside. You will get an error, that repeater is a not known element. But you can bind asp:RadioButtonList in code behind.
Im facing a pretty weird problem today.
I created a DropDownList which adds the selected Item to a List. The List will be binded to the ListView.
This is the Dropdown:
<asp:DropDownList ID="ddlChooseNewApplication" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Selected="True">Bitte wählen</asp:ListItem>
<asp:ListItem Value="windows">Windows</asp:ListItem>
<asp:ListItem Value="mail">E-Mail</asp:ListItem>
<asp:ListItem Value="app1">App1</asp:ListItem>
<asp:ListItem Value="app2">App2</asp:ListItem>
<asp:ListItem Value="app3">App3</asp:ListItem>
</asp:DropDownList>
Next, when a Item has been clicked it runs the following code:
//This is a global variable
List<NewApplicationModels> applicationName = new List<NewApplicationModels>();
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = ddlChooseNewApplication.SelectedValue.ToString();
applicationName.Add(new NewApplicationModels(){ ApplicationName = value});
ListViewNewApplications.DataSource = applicationName;
ListViewNewApplications.DataBind();
}
It will be added to a List<> which should add the selected application to the ListView which looks like this:
<asp:ListView ID="ListViewNewApplications" runat="server">
<ItemTemplate>
<br /><br />
<%# Eval("ApplicationName") %><br />
<asp:Label ID="Label3" runat="server" Text="Titel"></asp:Label><br />
<asp:TextBox ID="tbNewTitle" runat="server"></asp:TextBox><br /><br />
<asp:Label ID="Label4" runat="server" Text="Beschreibung"></asp:Label><br />
<asp:TextBox ID="tbNewDescription" runat="server" TextMode="MultiLine"></asp:TextBox><br /><br />
</div>
</ItemTemplate>
</asp:ListView>
Adding a single Item to the ListView works. The problem is, if I select a new Item in the DropDown, the current object in the ListView will be overwritten. I want it to create a new Item under the current Item.
Where is the mistake?
Many thanks in advance
Editted: Just read here that static property is per application domain, not per user, So the solution should change to use Session State
//Add using on top of .cs
using System.Linq;
//In cs class
public partial class name_of_class : Page
{
private List<NewApplicationModels> applicationName = new List<NewApplicationModels>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Do page_load stuff....
Session.Remove("name_here"); //reset the Session when first page load
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Get List from Session
applicationName = (List<NewApplicationModels>)Session["name_here"];
if (applicationName == null)
applicationName = new List<NewApplicationModels>();
string value = ddlChooseNewApplication.SelectedValue.ToString();
if (!applicationName.Any(item => item.ApplicationName == value))
{
applicationName.Add(new NewApplicationModels(){ ApplicationName = value});
Session["name_here"] = applicationName;
ListViewNewApplications.DataSource = applicationName;
ListViewNewApplications.DataBind();
}
}
}
For static property is per application domain, 2 stranger people from 2 different place browse the same page in same server (app domain) will all see it.
That means when userA change dropdownlist 2 times and the listview have 2 items, then userB browse and change dropdownlist, he may get 3 items in listview, 2 of userA and 1 of his recently choose (he may get nothing too).
I need to create check-box list dynamically populated from database.
I have no problem with creating this with the single option but with 3 - kind of puzzle me.
Currently I have following code which create single colum of checkboxes with Language name:
public void CreateCheckBox(DataSet DSDataForCheckBox, string pLangGrp)
{
CheckBoxList chkList = new CheckBoxList();
chkList.ID = "LanguageList";
chkList.AutoPostBack = true;
chkList.DataSource = DSDataForCheckBox;
chkList.DataTextField = "LangName";
chkList.DataValueField = "LangID";
chkList.DataBind();
Panel pLang = new Panel();
if (pLangGrp != "")
{
pLang.GroupingText = pLangGrp;
}
else
{
pLang.GroupingText = "Non Assigned Group";
}
pLang.Controls.Add(chkList);
this.Form.Controls.Add(pLang);
}
Need your experts help.
Thanks
PS: we are on NET 3.5, so many options from 4.0 is not available for me.
One way you can achieve this using repeater control or any list based control.
This is just to provide some idea to start with and you can do research to fit to your requirement.
ASPX
<asp:Repeater ID="Repeater1" runat="server" EnableViewState="False">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="RowStyle"><%# Eval("LangName") %></td>
<td>
<asp:CheckBoxList ID="chkListLangs" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="R" Value="R"></asp:ListItem>
<asp:ListItem Text="W" Value="W"></asp:ListItem>
<asp:ListItem Text="S" Value="S"></asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Code behind to get the selected values:
protected void btnSubmitLangs_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBoxList chkListLanguages = (CheckBoxList)item.FindControl("chkListLangs");
List<ListItem> selectedLangs = chkListLanguages.Items.Cast<ListItem>()
.Where(listItem => listItem.Selected)
.ToList();
//Or you can use foreach loop also to get the selected items.
}
}
Code to assign some test data to repeater:
public void AssignLanguageListToRepeater()
{
List<Language> languages = new List<Language>
{
new Language{LangID="1", LangName="English"},
new Language{LangID="2", LangName="Spanish"}
};
Repeater1.DataSource = languages;
Repeater1.DataBind();
}
Hope this help you...
I have form with 2 DDL named
State and City
State:
<asp:UpdatePanel ID="States" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="States"EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:DropDownList ID="States" runat="server"
AutoPostBack="True" DataSourceID="StatesObjectDataSource"
AppendDataBoundItems="true"
onselectedindexchanged="States_SelectedIndexChanged">
<asp:ListItem Value="-1" Text="- None -"/>
</asp:DropDownList>
<asp:ObjectDataSource ID="StatesObjectDataSource" runat="server"
onselecting="StatesObjectDataSource_Selecting"
SelectMethod="GetStates"
TypeName="Something">
</asp:ObjectDataSource>
</ContentTemplate>
</asp:UpdatePanel>
City:
<asp:DropDownList ID="Cities" runat="server">
</asp:DropDownList>
When they choose a state I want to populate the Cities DDL with all the cities for that state.
In code behind I am able to get to
States_SelectedIndexChanged(object sender, EventArgs e)
and i try to populate the Cities DDL by this
Cities.Items.Add(new ListItem(city,city));
However, I do not see my Cities DDL populated
I recommend creating a private property in the ViewState that holds the collection of physical objects. Then add the object to that list then databind the list of objects to the drop down.
Page Behind
<asp:DropDownList runat="server" ID="ddlCity" DataValueField="Key" DataTextField="Value">
</asp:DropDownList>
Code Behind
private List<KeyValuePair<string, string>> ListData
{
get { return (List<KeyValuePair<string, string>>) (ViewState["ListData"] ??
(ViewState["ListData"] = new List<KeyValuePair<string, string>>())); }
set { ViewState["ListData"] = value; }
}
protected void States_SelectedIndexChanged_SelectedIndexChanged(object sender, EventArgs e)
{
ListData.Add(new KeyValuePair<string, string>(ddlCitys.SelectedValue, ddlCitys.SelectedValue));
ddlCitys.DataSource = ListData;
ddlCitys.DataBind();
}
The get statement also employs lazy loading on the ListData property so you will never encounter a null reference exception when accessing the list.
If at all possible, I would suggest using the CascadingDropDown Extender instead of the UpdatePanel. There's no use reinventing that wheel, and the Toolkit control uses web services instead of partial postbacks (much faster).
Place your city DropDownList inside the update panel.