RadComboBox adding an item to the top - c#

I am trying to add an item to the top of the drop down. I am using ItemTemplates so i am doing the databind and trying to add one at the top that reads
[ ] All Profiles
I was able to add it, but it is overriding the binding of the actual data, so when i added this now only the [ ] All profiles is there but not the real binded data. What am I doing wrong?
By the way i am a newbie in c# :)
Thank you
public void BindData()
{
myCombo.DataSource = myDbConnection.GetValues();
myCombo.DataTextField = "Name";
myCombo.DataValueField = "ID";
myCombo.DataBind();
var tempProfiles = new[] { new { Name = "All Profiles", ID = "1" } };
myCombo.DataSource = tempProfiles;
myCombo.DataBind();
}
<telerik:RadComboBox ID="myCombo" EmptyMessage="All Types" runat="server" Width="200px">
<ItemTemplate>
<div onclick="StopPropagation(event)">
<asp:CheckBox runat="server" ID="chk1" onclick="onCheckBoxClick(this)"/>
<asp:Label runat="server" ID="lblProfile" AssociatedControlID="chk1">
<%# Eval("Name") %>
</asp:Label>
</div>
</ItemTemplate>
</telerik:RadComboBox>

In your example you're overwriting your DataSourceObject with your 1-item-list.
You should add the item "manually" after your DataBind call:
myCombo.Items.Insert(0,
new Telerik.Web.UI.RadComboBoxItem { Text = "All Profiles", Value = "1" }
);
myCombo.SelectedIndex = 0;

if you work in Bound mode all data should come from the DataSource.
what I usually do is to add an extra element in the datasource with ID 0 or int.MaxValue depending on the sort order and the position where I want to show it.

Related

Hide entire datalist item based on a property

I have a datalist which is populated with values from a database. When the user clicks a checkbox, I want to loop through all of the datalist items and hide all of the items that have the property isActive = false (which is displayed as "Disabled" in a text label). The item template consists of a table that contains multiple other items, buttons etc, which have not been included below. I therefore want to hide all elements that are found under the itemtemplate of a specific item.
My idea was to just hide the entire table by accessing its id, and then setting the table's visible property to false. This is currently not doing anything when I run the code. Any help will
appreciated!
<asp:CheckBox runat="server" Text="Filter by active postings" OnCheckedChanged="filterByActive"/>
<asp:DataList ID="postingsDataList" runat="server" OnItemCommand="itemCommand" >
<ItemTemplate>
<div class="postingRow">
<table class="postingTable" id="posting">
<tr>
<td>
<asp:Label ID="lblActive" runat="server" Text=<%#Eval("isActive").ToString().Equals("True") ? "Active &#9989" : "Disabled &#10060"%>/>
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
Code Behind:
protected void filterByActive (object sender, EventArgs e)
{
int count = postingsDataList.Items.Count;
CheckBox check = (CheckBox)sender;
if (check.Checked == true)
{
for (int i = 0; i < count; i++)
{
Label lblActive = postingsDataList.Items[i].FindControl("lblActive") as Label;
string isActive = lblActive.Text.ToString();
if (isActive.Equals("Disabled &#10060"))
{
Table tbl = postingsDataList.Items[i].FindControl("posting") as Table;
tbl.Visible = false;
}
}
}
else
{
for (int i = 0; i < count; i++)
{
Label lblActive = postingsDataList.Items[i].FindControl("lblActive") as Label;
string isActive = lblActive.Text.ToString();
if (isActive.Equals("Active &#9989"))
{
Table tbl = postingsDataList.Items[i].FindControl("posting") as Table;
tbl.Visible = true;
}
}
}
}
}
Ok, I suggest you do this:
Persist your table - thus you don't have to hit sql server again. (but, you could - not the end of the world).
I don't have your data, but I have a simple list of hotels - and there is a Active column for the Hotel. So, lets filter the data based on checking the check box, not have to hit the database server again. And EVEN BETTER is if we un-check the box, we can display all records we had.
We assume of course this is not a lot of data.
Ok, so we have this for our markup:
(really does not matter a whole lot)
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID" RepeatColumns="4" RepeatDirection="Horizontal" >
<ItemTemplate>
<div style="border-style:solid;color:black;width:300px;">
<div style="padding:5px;text-align:right">
<p>Hotel Name: <asp:TextBox ID="HotelName" runat="server" Text ='<%# Eval("HotelName") %>' /></p>
<p>First Name: <asp:TextBox ID="FirstName" runat="server" Text ='<%# Eval("FirstName") %>' /></p>
<p>Last Name: <asp:TextBox ID="LastName" runat="server" Text ='<%# Eval("LastName") %>' /></p>
<p>City: <asp:TextBox ID="City" runat="server" Text ='<%# Eval("City") %>' /></p>
<p>Province: <asp:TextBox ID="Province" runat="server" Text ='<%# Eval("Province") %>' /></p>
Active: <asp:CheckBox ID="Active" runat="server" Checked = '<%# Eval("Active") %>'/>
</div>
</div>
</ItemTemplate>
</asp:DataList>
Now, our code to load could be this:
DataTable rstData = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
ViewState["MyData"] = rstData;
}
else
rstData = (DataTable)ViewState["MyData"];
}
void LoadData()
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
string strSQL = "SELECT top 10 * from tblHotels ORDER BY HotelName";
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
conn.Open();
rstData.Load(cmdSQL.ExecuteReader());
DataList1.DataSource = rstData;
DataList1.DataBind();
}
}
}
And now we have this:
Now, our filter show/hide becomes JUST this:
protected void ckFilter_CheckedChanged(object sender, EventArgs e)
{
// filter based on check box
if (ckFilter.Checked)
rstData.DefaultView.RowFilter = "Active = 1";
else
rstData.DefaultView.RowFilter = "";
DataList1.DataSource = rstData;
DataList1.DataBind();
}
And BETTER is if I un-check the check box, all the data is re-displayed. And we don't have to re-hit the database to do this!!!
Now, we COULD also just hide/show each row, and then un-hide if for some reason you don't want to persist the data table as I did per above.
Checking on the box filters to this:
As noted, if I un-check the box, then I get all the data back - all without hitting the database again.
Edit:=========================================
Note that if you NOT using the above horizontal across, but just rows down?
Then you can just as well format to hide/show the rows, and you do NOT have to persist the data table.
You can apply formatting say by doing this:
protected void ckFilter_CheckedChanged(object sender, EventArgs e)
{
foreach (DataListItem gRow in DataList1.Items)
{
// get checkbox
CheckBox ckActive = (CheckBox)gRow.FindControl("Active");
// get div
HtmlGenericControl Myiv = (HtmlGenericControl)gRow.FindControl("myrow");
string MyFormat = "normal";
if (ckFilter2.Checked)
{
// only show active ones
if (ckActive.Checked)
MyFormat = "normal";
else
MyFormat = "none";
}
Myiv.Style["display"] = MyFormat;
}
}
So the above does work fine. BUT the restriction is that you can't have a set of horizontal across data items like my original screen shot
(it actually still works but in fact hide each panel with a blank space).
So, if your as noted using vertical layout (and it looks to be that you are).
Note for above, I added a simple ID to the "div", and runat server like this:
<ItemTemplate>
<div id="myrow" runat="server"
style="border-style:solid;color:black;width:300px;">
Then skip my first example. You can loop the data list rows, and hide, or un-hide. And even more amazing is that if you un-hide - then the rows all do re-appear and persist correct.
I had pulled a working example - but I was trying to figure out why I had to persist the data - the reason was the "horizontail" across settings.
But, as above shows, no real need to persist the data source table - you can process the data list rows, and hide/show each one conditionals, and you can do this without a data re-bind.

how to create Dynamic check-box list with 3 options in C#

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...

How to change the text on certain condition in GridView?

I want to change the text/value of a cell in certain condition in GridView. I have a cell that return 'P', 'A', 'R' when I bind the Gridview from database. I want to show 'Pending' in case of 'P', 'Approved' in case of 'A' and 'Rejected' in case of 'R' in the dropdown with the update button. I want to change the status of a record.
How can I change the text AND do my required task at the time of Binding of data.
<asp:TemplateColumn>
<HeaderStyle CssClass="bgB white p5 b treb ttu w10" />
<HeaderTemplate>
<asp:Label ID="lblstatus" runat="server" Text="Status"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblrmastatus" runat="server" Text='<%# Bind("RMA_STATUS") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
A way to solve this is to handle the RowDataBound event. It is raised once per row (also header and footer rows, so you need to filter for rows of type DataRow). You can use it to adjust the data that are displayed or the way they are displayed. Also you can set the values of a DropDown in the event. See the link for a sample.
In order to add the DropDown-column to the GridView, use a TemplateColumn and put the DropDown into the ItemTemplate. Access the corresponding cell in the RowDataBound event handler. Use the FindControl method to retrieve the DropDown control and set its values.
The following sample shows how to set the Label and the DropDownList programmatically:
GridView
The GridView shows both the original status and a new status with the long text, a DropDownList and a command column to update the data.
<asp:GridView ID="gdv" runat="server"
OnRowDataBound="gdv_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RMAStatus" HeaderText="Original RMA Status" />
<asp:TemplateField HeaderText="Adjusted RMA Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="New Status">
<ItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
</asp:GridView>
Code-behind
In Code-behind, I've defined a class called DataClass that contains the data (you can also use a DataTable, but for the sample it was easier to use a class to generate some data). Also, a dictionary contains the mappings from the short status to the long text.
In Page_Load, the dictionary is set up and if it is no PostBack, the data are bound.
Finally, in gdv_RowDataBound, the label is set for each row and the DropDownList is initialized. Please note that the Label and the DropDownList for the row are retrieved by using FindControl.
public class DataClass
{
public string RMAStatus { get; set; }
}
private readonly Dictionary<string, string> rmaStatusMappings = new Dictionary<string, string>();
protected void Page_Load(object sender, EventArgs e)
{
rmaStatusMappings.Add("R", "Rejected");
rmaStatusMappings.Add("A", "Approved");
rmaStatusMappings.Add("P", "Pending");
if (!IsPostBack)
{
var data = new DataClass[] {
new DataClass() { RMAStatus = "P" },
new DataClass() { RMAStatus = "A" },
new DataClass() { RMAStatus = "R" },
new DataClass() { RMAStatus = "P" },
};
gdv.DataSource = data;
gdv.DataBind();
}
}
protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var rowStatus = ((DataClass)e.Row.DataItem).RMAStatus;
// Set label to long status
var lblStatus = e.Row.Cells[1].FindControl("lblStatus") as Label;
if (lblStatus != null)
lblStatus.Text = rmaStatusMappings[rowStatus];
// Set drop down items
var ddlStatus = e.Row.Cells[2].FindControl("ddlStatus") as DropDownList;
if (ddlStatus != null)
{
ddlStatus.DataTextField = "Value";
ddlStatus.DataValueField = "Key";
ddlStatus.DataSource = rmaStatusMappings;
ddlStatus.DataBind();
ddlStatus.SelectedValue = rowStatus;
}
}
}
Let me know in the comments if you have any questions.

Gridview rowdatabound error : Multiple controls with the same ID 'hlLink' were found. FindControl requires that controls have unique ID

I have a gridview that has link and description to be rendered on the page.
written the below code in gridview in .aspx
<Columns>
<asp:TemplateField>
<ItemTemplate>
<p>
<asp:HyperLink ID="hlLink" runat="server" Target="_self"></asp:HyperLink></p>
</ItemTemplate>
<ItemTemplate>
<p>
<asp:Literal ID="litSummary" runat="server"></asp:Literal></p>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<AlternatingItemTemplate>
<p>
<asp:HyperLink ID="hlLink" runat="server" Target="_self"></asp:HyperLink></p>
</AlternatingItemTemplate>
<AlternatingItemTemplate>
<p>
<asp:Literal ID="litSummary" runat="server"></asp:Literal></p>
</AlternatingItemTemplate>
</asp:TemplateField>
</Columns>
and below in .aspx.csin gridview rowdataboundevent
protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SearchResultItem data = (SearchResultItem)e.Row.DataItem;
HyperLink hlLink = (HyperLink)e.Row.FindControl("hlLink");
Literal litSummary = (Literal)e.Row.FindControl("litSummary");
if (data.Description != null)
{
hlLink.Text = data.Title;
hlLink.NavigateUrl = data.Path.Replace("&", "&");
litSummary.Text = data.Description;
}
else
{
hlLink.Text = data.Path;
hlLink.NavigateUrl = data.Path.Replace("&", "&");
litSummary.Text = data.Path;
}
}
here SearchResultItem: is the result item that has link and description details.
First time when row bound event is called, it binds the data correctly, second time when called throws error "Multiple controls with the same ID 'hlLink' were found. FindControl requires that controls have unique IDs.
Please let me know whats error with the code.
Thanks
Problem : you are trying to create the same controls with same ID multiple times.
Solution : you need to remove the controls before creating them.
Try This:
void RemoveControls()
{
HyperLink l1 = (HyperLink)Page.FindControl("hlLink");
Literal l2 = (Literal)Page.FindControl("litSummary");
if(l1!= null)
Page.Controls.Remove(l1);
if(l2!= null)
Page.Controls.Remove(l2);
}
Solution 2: Pagination for Repeater control.
for implementing pagination in Repeater control you need to create PagedDataSource.
Try This:
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 8;//page sizes

ASPxGridView, ITemplate, and Eval

How can I create a template in c# for an AXPxGridViewDataTextColumn without any markup and with using Eval to display the DataItem value?
-The problem that I am having is that the string "<%#Eval("dataTableField1")%>" shows up in the GridView for every row instead of the appropriate values.
Here is an example of my attempt:
public override void DataBind()
{
...
GridViewDataTextColumn myCol = new GridViewDataTextColumn();
myCol.Caption = "col1";
myCol.FieldName = "dataTableField1";
myCol.DataItemTemplate = new ColumnDataItemTemplate();
theGridView.Columns.Clear();
theGridView.Columns.Add(myCol);
theGridView.DataSource = AdjustDataSource();
theGridView.DataBind();
...
}
public class ColumnDataItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
GridViewDataItemTemplateContainer Container = (container as GridViewDataItemTemplateContainer);
LiteralControl lit = new LiteralControl("<div id=\"hr\" style=\"height:100%\"><%#Eval(\"dataTableField1\")%></div>");
Container.Controls.Add(lit);
}
}
Here is an example of what I want to do with the row height taken from this link:
<dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="3">
<DataItemTemplate>
<div id="hr" style="height:100%">
<%#Eval("Description")%>
</div>
</DataItemTemplate>
</dx:GridViewDataTextColumn>
This documentation link shows similar examples of using templates in the markup but I want to do it in the code behind.
Here is a link on creating templates.
Thanks in advance,
Soenhay
Edit:
I was able to get the first element to properly display by moving the template assignment to the CustomColumnDisplayText event but all other elements displayed in the ASPxGridView show the Eval string.
I had 2 other solutions but I removed them as I think this is the best ( I would be grateful for any other suggestions/improvements ):
public override void DataBind()
{
...
GridViewDataTextColumn myCol = new GridViewDataTextColumn();
myCol.Caption = "col1";
myCol.FieldName = "dataTableField1";
myCol.DataItemTemplate = new ColumnDataItemTemplate();
theGridView.Columns.Clear();
theGridView.Columns.Add(myCol);
theGridView.DataSource = AdjustDataSource();
theGridView.DataBind();
...
}
public class ColumnDataItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
GridViewDataItemTemplateContainer Container = (container as GridViewDataItemTemplateContainer);
LiteralControl lit = new LiteralControl("<div id='hr' style='height:100%; font-size:x-large;'>" + DataBinder.Eval(Container.DataItem, Container.Column.FieldName) + "</div>");
Container.Controls.Add(lit);
}
}
At this link I have found a reason to not use the event:
"The CustomColumnDisplayText event should not be handled for template columns. "
This link helped with the DataBinder.Eval part.
Heres an example but mine is binding with a database, i usually use EditTemplte
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BSA" SortExpression="BSA">
<ItemTemplate>
<asp:Label ID="lblBSA" runat="server" Text='<%# Bind("BSA") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>

Categories