Dropdownlist just retrieving objects and dynamically show in Ajax Text box - sql

So Im starting as a new ASP Developer (been using Java for a bit, but now project demands ASP), so bare a little with my ignorance :( .
What I am trying to do is create a dynamic dropdown list from a table I got in SQL SERVER and depending on what you choose on the list show the information on the txt editor (In theory shouldnt be so hard, but since I just started, it just does not seem so easy). I created the entity for it, the data for it and the bussiness logic for it and the interconnection (It is already reading and retrieving from the DB, but just a list). Here is, what I have doing so hard.
Entity.Messages
public class Messages
{
public int id { get; set; }
public string title { get; set; }
public string subject { get; set; }
public string body { get; set; }
public string createdBy { get; set; }
public Messages()
{
id = 0;
title = "";
subject = "";
body = "";
createdBy = "";
}
public Messages(int idMessages)
{
idMessages = id;
title = "";
subject = "";
body = "";
createdBy = "";
}
}
}
Data.Messages
public class Messages : Data
{
public Messages() : base()
{
}
public List<Entity.Messages> GetAll()
{
List<Entity.Messages> message = new List<Entity.Messages>();
//SQL Command para llamar el stored procedure
SqlCommand comando = new SqlCommand("dbo.[Messages_GetAll]", base.Db);
//Ejecuta consulta
DataTable dtItem = base.Execute(comando);
//Transforma el Datatable en una lista de proyectos.
foreach (DataRow dr in dtItem.Rows)
message.Add(GetFromDataRow(dr));
return message;
}
public Entity.Messages GetById(int id)
{
Entity.Messages m = new Entity.Messages();
//SQL Command para llamar el stored procedure
SqlCommand comando = new SqlCommand("dbo.[Messages_GetById]", base.Db);
//parametros del store procedure
SqlParameter spKey = new SqlParameter("#Id", System.Data.SqlDbType.Int);
spKey.Value = id;
comando.Parameters.Add(spKey);
//Ejecuta consulta
DataTable dt = base.Execute(comando);
if (dt.Rows.Count > 0)
m = GetFromDataRow(dt.Rows[0]);
return m;
}
private ASF.HC.JobApplication.Entity.Messages GetFromDataRow(DataRow dr)
{
Entity.Messages m = new Entity.Messages();
m.id = dr["Id"] == DBNull.Value ? -1 : int.Parse(dr["Id"].ToString());
m.title = dr["Title"] == DBNull.Value ? "" : dr["Title"].ToString();
m.subject = dr["Subject"] == DBNull.Value ? "" : dr["Subject"].ToString();
m.body = dr["Body"] == DBNull.Value ? "" : dr["Body"].ToString();
m.createdBy = dr["createdBy"] == DBNull.Value ? "" : dr["createdBy"].ToString();
return m;
}
}
BO.Messages
public class Messages
{
public Entity.Messages GetByID(int id)
{
Data.Messages oMessage = new Data.Messages();
return oMessage.GetById(id);
}
public List<Entity.Messages> GetAll()
{
Data.Messages oMessage = new Data.Messages();
return oMessage.GetAll();
}
And where Im trying to display it, I see the DropDownList and well I see the List with the objects, but I want to display is the title.
MEssage.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Messages.aspx.cs" Inherits="ASF.HC.JobApplication.Admin.Messages" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Messages</h2>
<asp:ScriptManager ID="ScriptManager2" runat="server"></asp:ScriptManager>
<legend>Pick the template to use:</legend>
<asp:dropdownlist id ="ddlTemplate" runat ="server" Height="38px" Width="397px">
<asp:listitem value ="1"> Juan Valdez </asp:listitem >
<asp:listitem Value ="2"> Querido bebe</asp:listitem>
</asp:dropdownlist >
<p> </p>
<asp:TextBox ID ="txtDetails" runat="server" Width="600px" Height="300px" Visible="true" ></asp:TextBox>
<ajaxToolkit:HtmlEditorExtender ID="TextBox1_HtmlEditorExtender" runat="server" TargetControlID="txtDetails"
EnableSanitization="false" DisplaySourceTab="true" >
</ajaxToolkit:HtmlEditorExtender>
</asp:Content>
Messages.aspx.cs
public partial class Messages : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
loadList();
}
public void loadList()
{
BO.Messages template = new BO.Messages();
ddlTemplate.DataSource = template.GetAll();
ddlTemplate.DataBind();
}
}
}
Im trying to go step by step, but first I wanna see the value and not the objects, and according to what I choose to see the Body field Displayed on the text field on Ajax. But first, and most importantly, show the title field on the dropdownlist :(
EDIT: So I got to show the Title instead of all the object. But now, do you guys know any way to as soon as I choose the title, to have it displayed on the Text from ajax? Like dynamically? Any point would be greatly appreciated.
Any help or pointer would be greatly appreciated.
Thanks!

Apparently, all I needed was to map the datavalue and datatext field.

Related

Custom Model as Generic TypeArgument in XAML

I have made a small class, which inherits from DataGrid and takes in classes that derive from a specific interface:
public class RecordDataGrid<T> : DataGrid where T : IRecord
{
public RecordDataGrid()
{
this.AutoGenerateColumns = false;
this.CanUserAddRows = false;
this.CanUserDeleteRows = false;
this.CanUserResizeRows = false;
this.IsReadOnly = true;
this.SelectionMode = DataGridSelectionMode.Single;
this.Margin = new System.Windows.Thickness(0, 10, 0, 0);
var propertyInfos = typeof(T).GetProperties();
var list = new Dictionary<PropertyInfo, DataGridColumnAttribute>();
foreach (var propertyInfo in propertyInfos)
{
var customAttributes = propertyInfo.GetCustomAttributes(true);
foreach (var customAttr in customAttributes)
{
if (customAttr != null && customAttr is DataGridColumnAttribute)
{
list.Add(propertyInfo, (DataGridColumnAttribute)customAttr);
}
}
}
var ordered = (from entry in list orderby entry.Value.OrderIndex ascending select entry).ToDictionary(e => e.Key, e => e.Value);
foreach (var kvp in ordered)
{
var propertyInfo = kvp.Key;
var dgcAttr = kvp.Value;
var column = new DataGridTextColumn();
column.Header = dgcAttr.DisplayName;
column.Binding = new Binding(propertyInfo.Name);
column.Binding.StringFormat = dgcAttr.StringFormat ?? null;
column.Width = dgcAttr.ColumnWidthType == DataGridColumnAttribute.ColumnWidthTypes.Auto ? new DataGridLength(10, DataGridLengthUnitType.Auto) : new DataGridLength(10, DataGridLengthUnitType.Star);
this.Columns.Add(column);
}
}
}
It is very rough at the moment, just testing a few things out. The goal is to make my life easier by letting the DataGrid fill the Columns by itself, based on a custom Attribute:
public class DataGridColumnAttribute : Attribute
{
public string DisplayName { get; private set; }
public string StringFormat { get; private set; }
public ColumnWidthTypes ColumnWidthType { get; private set; }
public int OrderIndex { get; private set; }
public DataGridColumnAttribute(string displayName, int orderIndex, string stringFormat = null, ColumnWidthTypes columnWidthType = ColumnWidthTypes.Auto)
{
DisplayName = displayName;
StringFormat = stringFormat;
OrderIndex = OrderIndex;
ColumnWidthType = columnWidthType;
}
public enum ColumnWidthTypes
{
Auto,
Fill
}
}
Later on, as far as I am concerned, I should be able to use it in xaml like this:
Namespaces:
xmlns:model="clr-namespace:NickX.KswErp.Model.Classes;assembly=NickX.KswErp.Model"
xmlns:ctrl="clr-namespace:NickX.KswErp.ClientApplication.UI.Controls"
Control:
<ctrl:RecordDataGrid x:Name="_gridTransactions" x:TypeArguments="model:TransactionRecord" />
But I get following compilation error:
Only a master tag can specify the "x: TypeArguments" attribute.
(Roughly translated by google translation)
Maybe my approach is completely wrong tho. Should I do it completle in code behind. Or are there better approaches? Please let me know!
Conveniently I just found a thread in a german forum, which answeres my exact question. So people questioning the same in the future:
It is not possible. Easiest thing to do at this point is making a specific class for each model, which again derives from your generic class.
In my case:
public class TransactionDataGrid : RecordDataGrid<TransactionRecord>
{
}
Doesen't seem like a nice solution to me, and probably isn't the best way to do it. But it works.

Fill VIEW data based on clicked movie

I am making a Movie Review site (without entity framework) ( not meant to use it)
I have a stored procedure to fill the info of the movie:
This code below: is how I get the movie information based on the query below
<div class="panel panel-default" style="background-color:white;">
<div class="panel-body">
<div style="height:400px; width:100%;">
<section class="center slider" style="width: 88%;">
#*Aqui va el poster de reviews recientes*#
#foreach (var poster in Model.LGetLastReviews)
{
<div>
<a href="/Review/Review/?=#poster.Id" id="tag<%=count++%" name="#poster.Id">
<img class="sizecool hvr-shrink" src="#Href("~/Content/"+#poster.Path )"/>
</a>
<div class="subtitulo">
#poster.Title
#poster.Date
</div>
</div>
}
</section>
</div>
</div>
</div>
Here is the query:
CREATE PROCEDURE [dbo].[GetLastReviews] #Top bigint
AS
BEGIN
SELECT TOP(#Top) Movie.Id, MOVIE.title, Poster.path_image, review.comment, review.Date from review
inner join movie on review.Movie_Id = movie.Id
inner join poster on movie.Id = poster.Movie_Id
order by review.Date desc
END
Now the real question is: How to fill this other view based on what the user clicked?
get the img id,name..etc and fill this view:
class:
public class GetLastReviews
{
public long Id { get; set; }
public string Title { get; set; }
public string Path { get; set; }
public string Comment { get; set; }
public DateTime Date { get; set; }
}
public static List<GetLastReviews> GetLastReviews(int top)
{
List<GetLastReviews> lastReviewses= new List<GetLastReviews>();
SqlConnection connection = new SqlConnection(StrConnection);
string cmd = "GetLastReviews";
SqlCommand sqlCmd = new SqlCommand(cmd, connection);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add("#Top", SqlDbType.VarChar).Value = top.ToString();
connection.Open();
SqlDataReader dr = sqlCmd.ExecuteReader();
while (dr.Read())
{
GetLastReviews getLasR = new GetLastReviews();
getLasR.Id = long.Parse(dr["Id"].ToString());
getLasR.Title = dr["Title"].ToString();
getLasR.Path = dr["Path_Image"].ToString();
getLasR.Comment = dr["Comment"].ToString();
getLasR.Date = DateTime.Parse(dr["Date"].ToString());
lastReviewses.Add(getLasR);
}
connection.Close();
return lastReviewses;
}
Just bind the one function on div's click event with uniqueId(MovieId or any) in like as below.
#foreach (var poster in Model.LGetLastReviews)
{
<div onclick="GoToMovieDetail(#Model.MovieId)">
<a href="/Review/Review/?=#poster.Id" id="tag<%=count++%" name="#poster.Id">
<img class="sizecool hvr-shrink" src="#Href("~/Content/"+#poster.Path )"/>
</a>
<div class="subtitulo">
#poster.Title
#poster.Date
</div>
</div>
}
Make one function in Javascript like as below.
function GoToMovieDetail(id)
{
if(id != null)
{
var url = "#Url.Action("DisplayMovieDetailActionName", "MovieControllerName")" + "?MovieId=" + id;
window.location.href = url;
}
}
Create one Action in your Controller to get the Movie Detail as per the requested Id in parameter as below.
public ActionResult DisplayMovieDetail(int id)
{
MovieModel model = new MovieModel();
// Get the detail of moview based on requested id in action parameter.
// Bind MovieModel object.
// Pass thid object in your return View.
// In view you need to just bind the model data.
return View(model);
}

ASP.NET Web API - SQL - Retrieving Information

I am having some issues with retrieving information from sql database using ASP.net Web API.
I have tried to use forms, which worked great (using gridview) but when I try to do it using a separate class dedicated to store my specific table information I get this error:
"Index was out of range. Must be non-negative and less than the size of the collection."
This is the code:
public ActionResult Details()
{
List<Employee> employeeList = new List<Employee>();
string CS = ConfigurationManager.ConnectionStrings["EmployeeContext"].ConnectionString;
using (var myConn = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("select * from tblEmployee", myConn);
myConn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
int i = 0;
employeeList[i].PersonID = Convert.ToInt32(rdr["PersonID"]);
employeeList[i].Name = rdr["Name"].ToString();
employeeList[i].Gender = rdr["Gender"].ToString();
employeeList[i].City = rdr["City"].ToString();
employeeList[i].DepartmentID = Convert.ToInt32(rdr["DepartmentID"]);
i++;
}
return View(employeeList);
}
}
This is the Employee class:
[Table("tblEmployee")]
public class Employee
{
public int PersonID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public int DepartmentID { get; set; }
}
}
I get this error on any of the retrieving information lines.
The table has 5 columns: PersonID(int PK), Name(nvarchar), Gender(nvarchar), City(nvarchar), DepartmentID(int).
I checked many times the columns names to make sure I didn't got those wrong and I double checked the connection string which is also fine (the same code works with gridview using forms API).
Hope someone can help me with this. I didn't find any specific information on that and I guess it's should be easy and I'm doing something wrong here.
You are trying to populate a List<> object by using an index. To populate a List<> you need to use .Add(). You need to change your code from this:
int i = 0;
employeeList[i].PersonID = Convert.ToInt32(rdr["PersonID"]);
employeeList[i].Name = rdr["Name"].ToString();
employeeList[i].Gender = rdr["Gender"].ToString();
employeeList[i].City = rdr["City"].ToString();
employeeList[i].DepartmentID = Convert.ToInt32(rdr["DepartmentID"]);
i++;
To this:
Employee emp = new Employee();
emp.PersonID = Convert.ToInt32(rdr["PersonID"]);
emp.Name = rdr["Name"].ToString();
emp.Gender = rdr["Gender"].ToString();
emp.City = rdr["City"].ToString();
emp.DepartmentID = Convert.ToInt32(rdr["DepartmentID"]);
employeeList.Add(emp);
When adding a new item to a list, you should use .Add(). Here's one option:
while (rdr.Read())
{
employeeList.Add(new Employee {
PersonID = Convert.ToInt32(rdr["PersonID"]),
Name = rdr["Name"].ToString(),
Gender = rdr["Gender"].ToString(),
City = rdr["City"].ToString(),
DepartmentID = Convert.ToInt32(rdr["DepartmentID"])
});
}
You could then access individual items in the list with their index or by using foreach.

Filtering Data on a DevExpress GridView

I am an MVC person, and have minimal experience with WebForms, but now I am having to add some functionality to a legacy VB.NET WebForms application.
So the application is using DevExpress Grids, and it displays a really long grid view on the page where one of the columns has the following:
The extra functionality that I am asked to add is a filter where the user can say:
I only want to see the rows where the on-load radio button selected is Print (or one of the other two actions).
So I went to the bottom of the grid and created the following:
My thinking was, the user can come to this drop down and selected what he or she wants to filter on for radio button actions.
DevExpress GridView Code
<dx:ASPxGridView ID="GridView1" runat="server" Theme="Office2010Blue" KeyFieldName="ID" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" Width="3200px">
<Columns>
<!-- Many more columns go here -->
<dx:GridViewDataColumn Caption="" VisibleIndex="29" Name="decision" Width="150px">
<HeaderTemplate>
<asp:LinkButton runat="server" ID="lnkPrint" OnClick="SelectPrintAll">Print All</asp:LinkButton>
<asp:LinkButton runat="server" ID="lnkEmail" OnClick="SelectEmailAll">Email All</asp:LinkButton>
<asp:LinkButton runat="server" ID="lnkIgnore" OnClick="SelectIgnoreAll">Ignore All</asp:LinkButton>
</HeaderTemplate>
<DataItemTemplate>
<asp:UpdatePanel runat="server" ID="upRadDecision" UpdateMode="Conditional">
<ContentTemplate>
<dx:ASPxRadioButtonList ID="radDecision" runat="server" RepeatDirection="Horizontal"
OnSelectedIndexChanged="StoreDecisionForRow" AutoPostBack="True" Height="15px"
OnDataBinding="BindDecisionRadioButton">
<Border BorderStyle="None"></Border>
<Paddings Padding="0"></Paddings>
<Items>
<dx:ListEditItem Text="Print" Value="Print" />
<dx:ListEditItem Text="Email" Value="Email" />
<dx:ListEditItem Text="Ignore" Value="Ignore" />
</Items>
</dx:ASPxRadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
</DataItemTemplate>
<Settings HeaderFilterMode="CheckedList"></Settings>
</dx:GridViewDataColumn>
</Columns>
<!-- Stylres -->
<Styles>
<AlternatingRow Enabled="true" />
</Styles>
<!-- Settings -->
<Settings ShowFilterRow="True" ShowFilterRowMenu="true" ShowFilterBar="Auto" ShowHeaderFilterButton="true" ShowGroupPanel="True" ShowFooter="True" />
<SettingsBehavior AllowSelectByRowClick="False" />
<SettingsBehavior AllowSelectSingleRowOnly="False" />
<SettingsBehavior ProcessSelectionChangedOnServer="true" />
<SettingsPager Mode="ShowAllRecords" />
<GroupSummary>
<dx:ASPxSummaryItem SummaryType="Count" />
</GroupSummary>
</dx:ASPxGridView>
I have added a click handler to my filter button, and the code is like so:
Private Sub btnFilterDefaults_Click(sender As Object, e As EventArgs) Handles btnFilterDefaults.Click
Dim filterOn As String = ddDefaultsFilterOption.SelectedValue
'Code Handling the Filtering.
GridView1.filter
For rowIndex As Integer = 0 To GridView1.VisibleRowCount - 1
Dim datarow = GridView1.GetDataRow(rowIndex)
Dim radDecision As ASPxRadioButtonList = CType(GridView1.FindRowCellTemplateControl(rowIndex, Nothing, "radDecision"), ASPxRadioButtonList)
Dim decision As String = ""
If radDecision.Value Is Nothing then Continue For
decision = radDecision.Value.ToString()
If decision.Contains(filterOn) Then
datarow.?? <<<<< no option to hide row here!!! :/
End If
Next
End Sub
I was hoping that when I got hold of the data row, I'd be able to hide it, but there is no such option!
I think the issue is you're trying to apply a visible trait to a datarow. What you want to do is use GridViewRow instead. It's the presentation object. Sorry I don't have an example for you but here is a link to msdn for it
Did you try to use HeaderFilterMode for your GridView? This function started from 12 DevExpress version, as I remember. Look at the example below, how you can enable it.
<dx:GridViewDataColumn Caption="" VisibleIndex="29" Name="decision" Width="150px">
...
<Settings HeaderFilterMode="CheckedList" />
</dx:GridViewDataColumn>
If you have older version or you need completely another functionality, you can create your own custom template for filter column. Look at the example on c# below
public class FilterLookupTemplate : ITemplate
{
private const string FormatFilterValue = "FilterLookupTemplateValue_{0}";
public string ClientIdLookup { get; private set; }
public string FieldName { get; set; }
public string DataSourceID { get; set; }
public ASPxGridView GridView { get; set; }
#region ITemplate Members
/// <summary>
/// Initialization template
/// </summary>
public void Init()
{
(GridView != null).Ensure<ArgumentException>("There didn't passed reference to grid view!");
if (!GridView.IsClientSideAPIEnabled() || String.IsNullOrEmpty(GridView.ClientInstanceName))
{
GridView.ClientInstanceName = GridView.ID;
}
var column = GridView.Columns[FieldName] as GridViewDataColumn;
(column != null).Ensure<ArgumentException>("There is error to get column by name!");
column.Settings.ShowFilterRowMenu = DefaultBoolean.False;
GridView.AutoFilterCellEditorCreate += OnAutoFilterCellEditorCreate;
GridView.AutoFilterCellEditorInitialize += OnAutoFilterCellEditorInitialize;
GridView.ProcessColumnAutoFilter += OnProcessColumnAutoFilter;
}
/// <summary>
/// Creating filter dropdown control
/// </summary>
/// <param name="sender">Event sender</param>
/// <param name="e">Event arguments</param>
private void OnAutoFilterCellEditorCreate(object sender, ASPxGridViewEditorCreateEventArgs e)
{
if (e.Column.FieldName.Equals(FieldName))
{
var dde = new DropDownEditProperties { EnableClientSideAPI = true, DropDownWindowTemplate = this };
e.EditorProperties = dde;
}
}
/// <summary>
/// Initializing filter dropdown control
/// </summary>
/// <param name="sender">Event sender</param>
/// <param name="e">Event arguments</param>
private void OnAutoFilterCellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
{
if (e.Column.FieldName.Equals(FieldName))
{
var editor = e.Editor as ASPxDropDownEdit;
(editor != null).Ensure<ArgumentException>("There wasn't passed reference to the drop down editor!");
editor.ReadOnly = true
}
}
/// <summary>
/// Processing column filtering
/// </summary>
/// <param name="sender">Event sender</param>
/// <param name="e">Event arguments</param>
private void OnProcessColumnAutoFilter(object sender, ASPxGridViewAutoFilterEventArgs e)
{
var session = GridView.Page.Session;
if (e.Kind == GridViewAutoFilterEventKind.CreateCriteria)
{
session[String.Format(FormatFilterValue, e.Column.FieldName)] = e.Value;
if (e.Column.FieldName.Equals(FieldName) && !String.IsNullOrEmpty(e.Value))
{
var values = e.Value.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
if (values.Length > 0)
{
var action = new Func<string, string, FunctionOperator>((name, value) =>
new FunctionOperator(FunctionOperatorType.Contains, new OperandProperty(name), new OperandValue(value)));
if (values.Length > 1)
{
var group = new GroupOperator(GroupOperatorType.Or);
group.Operands.AddRange(values.Select(v => action(e.Column.FieldName, v)).ToArray());
e.Criteria = group;
}
else
{
e.Criteria = action(e.Column.FieldName, values[0]);
}
}
}
}
else
{
if (session[String.Format(FormatFilterValue, e.Column.FieldName)] != null)
{
e.Value = session[String.Format(FormatFilterValue, e.Column.FieldName)].ToString();
}
}
}
/// <summary>
/// Rendering loolup template controls
/// </summary>
/// <param name="container">Container of date range template</param>
public void InstantiateIn(Control container)
{
(GridView != null).Ensure<ArgumentException>("There didn't passed reference to grid view!");
var table = new Table { Width = new Unit(200, UnitType.Pixel) };
container.Controls.Add(table);
var row = new TableRow();
table.Rows.Add(row);
var cell = new TableCell();
row.Cells.Add(cell);
var lbl = new ASPxLabel { ID = "lblSelect", Text = MessageResources.FilterLookupTemplate_SelectLabelText };
cell.Controls.Add(lbl);
cell = new TableCell();
row.Cells.Add(cell);
var lookup = new ASPxGridLookup
{
ID = GridView.ID + "lookupValues",
EnableClientSideAPI = true,
Width = new Unit(100, UnitType.Percentage),
SelectionMode = GridLookupSelectionMode.Multiple
};
lookup.GridView.Width = new Unit(100, UnitType.Percentage);
lookup.GridView.DataSourceID = DataSourceID;
lookup.GridView.KeyFieldName = "id";
lookup.GridView.Columns.Add(new GridViewCommandColumn { ShowSelectCheckbox = true, AllowDragDrop = DefaultBoolean.False });
var nameColumn = new GridViewDataTextColumn { FieldName = "name" };
nameColumn.Settings.AllowDragDrop = DefaultBoolean.False;
nameColumn.Settings.AllowGroup = DefaultBoolean.False;
nameColumn.Settings.AllowHeaderFilter = DefaultBoolean.False;
nameColumn.Settings.AllowSort = DefaultBoolean.False;
lookup.GridView.Columns.Add(nameColumn);
lookup.EnableClientSideAPI = true;
cell.Controls.Add(lookup);
ClientIdLookup = lookup.ClientID;
row = new TableRow();
table.Rows.Add(row);
cell = new TableCell { ColumnSpan = 2 };
row.Cells.Add(cell);
var lnk = new ASPxHyperLink { Text = MessageResources.FilterLookupTemplate_ApplyLinkText, NavigateUrl = "#" };
lnk.ClientSideEvents.Click =
String.Format("function (s, e) {{ {0}.HideDropDown(); ApplyLookupFilter({0}, {1}, '{2}', {3}); }}",
container.NamingContainer.NamingContainer.ClientID,
GridView.ClientInstanceName,
FieldName,
ClientIdLookup);
cell.Controls.Add(lnk);
container.Controls.Add(table);
}
#endregion
}
and then register it for your grid view. ReportsCustomerDataSource is a LINQ data source control.
public partial class YourPage: Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
new FilterLookupTemplate { FieldName = "ReportCustomers", DataSourceID = ReportsCustomerDataSource.ID, GridView = _gridView }.Init();
}
}
On the form it will be look like that

Templated control in ektron

I have tried to retrieve the taxonomy list using taxonomy tree, in edit i am selecting the taxonomy and getting TaxonomyID,by using the taxonomyID i want to display the data using taxonomy filters using templated control.I am unable to retrive the data.I am attaching HTML and Code what i have done,So please help me out for the solution.
<asp:View ID="View" runat="server">
<asp:Label ID="lblID" runat="server"></asp:Label>
<CMS:Directory CssClass="taxList" ID="TaxonomySummary1" EnableAjax="true" runat="server"
EnablePaging="false" />
<ektron:ContentModelSource ID="cntModelSourcs" runat="server" OrderByField="DateCreated"
OrderByDirection="Descending">
<TaxonomyFilters>
<ektron:ContentTaxonomyFilter Operator="Contains" ID="taxFilter" runat="server" />
</TaxonomyFilters>
<ContentFilters>
<ektron:ContentFilter Field="Id" Operator="EqualTo" Value="" />
</ContentFilters>
</ektron:ContentModelSource>
<ektron:ContentView ID="ContentView1" runat="server" ModelSourceID="cntModelSourcs"
EktronCustomTemplate="Ektron_ContentList_Template" >
</ektron:ContentView>
</asp:View>
enter code here
_edit" class="TSWidget">
<div class="ByTaxonomy TSTabPanel">
<div style="height: 150px; overflow: auto;">
<UC:TaxonomyTree ID="TaxonomyTree1" runat="server" />
</div>
<hr />
</div>
<div class="ByProperty TSTabPanel">
<table style="width: auto;">
<tr>
<td class="label">
<%=m_refMsg.GetMessage("lbl taxonomy id:")%>
</td>
<td>
<asp:TextBox ID="taxonomyid" CssClass="folderid" runat="server" Style="width: auto;"></asp:TextBox>
</td>
</tr>
<tr>
<td class="label">
<%=m_refMsg.GetMessage("lbl taxonomy path:")%>
</td>
<td>
<asp:Label ID="taxonomypath" CssClass="taxonomypath" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
<div class="TSEditControls">
<asp:Button ID="CancelButton" CssClass="TSCancel" runat="server" Text="Cancel" OnClick="CancelButton_Click" />
<asp:Button ID="SaveButton" CssClass="TSSave" runat="server" OnClick="SaveButton_Click" Text="Save" />
</div>
</div>
</asp:View>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ektron.Cms;
using Ektron.Cms.Widget;
using Ektron.Cms.Common;
using Ektron.Cms.API;
using Ektron.Cms.Organization;
using Ektron.Cms.Framework.Organization;
using System.Text;
using Ektron.Cms.Search.Expressions;
using Ektron.Cms.Search;
using Ektron.Cms.Framework;
public partial class widgets_Content_TaxonomyFilter : System.Web.UI.UserControl,IWidget
{
# region Properties
protected string appPath = "";
private Ektron.Cms.CommonApi _commonAPI = new CommonApi();
private long _taxonomyid;
public string TaxonomySelected = "selected";
public string PropertySelected = "selected";
public string m_strTaxonomyPath = "/";
private string _taxonomypath;
[WidgetDataMember(0)]
public long TaxonomyId { get { return _taxonomyid; } set { _taxonomyid = value; } }
[WidgetDataMember("")]
public string TaxonomyPaths { get { return _taxonomypath; } set {
_taxonomypath = value; } } private IWidgetHost _host;
protected ContentAPI m_refContentApi = new ContentAPI();
protected EkMessageHelper m_refMsg;
#endregion
# region Page Load
protected void Page_Load(object sender, EventArgs e)
{
taxFilter.Value = TaxonomyId.ToString();
ContentView1.DataBind();
}
#endregion
#region Page Init
protected void Page_Init(object sender, EventArgs e)
{
m_refMsg = m_refContentApi.EkMsgRef;
CancelButton.Text = m_refMsg.GetMessage("btn cancel");
SaveButton.Text = m_refMsg.GetMessage("btn save");
_host = Ektron.Cms.Widget.WidgetHost.GetHost(this);
_host.Title = "Templated Control";
_host.Edit += new EditDelegate(EditEvent);
_host.Maximize += new MaximizeDelegate(delegate() { Visible = true; });
_host.Minimize += new MinimizeDelegate(delegate() { Visible = false; });
_host.Create += new CreateDelegate(delegate() { EditEvent(""); });
_host.ExpandOptions = Expandable.ExpandOnEdit;
appPath = _commonAPI.AppPath;
Load += new EventHandler(delegate(object PreRenderSender, EventArgs Evt) { if
(ViewSet.GetActiveView() != Edit) SetTaxonomySummary(); });
ViewSet.SetActiveView(View);
}
protected void SetTaxonomySummary()
{
if (TaxonomyId > 0)
{
lblID.Text = Convert.ToString(taxFilter.Value);
}
}
#endregion
#region EditEvent
void EditEvent(string settings)
{
try
{
string sitepath = _commonAPI.SitePath;
string webserviceURL = sitepath + "widgets/taxonomysummary/TSHandler.ashx";
JS.RegisterJSInclude(this, JS.ManagedScript.EktronJS);
Ektron.Cms.API.JS.RegisterJSInclude(this,
Ektron.Cms.API.JS.ManagedScript.EktronJQueryClueTipJS);
JS.RegisterJSInclude(this, JS.ManagedScript.EktronScrollToJS);
JS.RegisterJSInclude(this, sitepath + "widgets/taxonomysummary/behavior.js",
"TaxonomySummaryWidgetBehaviorJS");
if (TaxonomyId > 0)
{
TaxonomySelected = "";
JS.RegisterJSBlock(this, "Ektron.PFWidgets.TaxonomySummary.webserviceURL =
\"" + webserviceURL + "\";
Ektron.PFWidgets.TaxonomySummary.setupAll();
Ektron.PFWidgets.TaxonomySummary.SetTabs.init()
; ", "EktronPFWidgetsTSInit");
}
else
{
PropertySelected = "";
JS.RegisterJSBlock(this, "Ektron.PFWidgets.TaxonomySummary.webserviceURL =
\"" + webserviceURL + "\";
Ektron.PFWidgets.TaxonomySummary.setupAll(); ", "EktronPFWidgetsTSInit");
}
Css.RegisterCss(this, sitepath + "widgets/taxonomysummary/TSStyle.css",
"TSWidgetCSS");
ViewSet.SetActiveView(Edit);
//set taxonomy path
Ektron.Cms.API.Content.Taxonomy _apiTaxonomy = new
Ektron.Cms.API.Content.Taxonomy();
Ektron.Cms.TaxonomyRequest taxRequest = new Ektron.Cms.TaxonomyRequest();
taxRequest.TaxonomyId = TaxonomyId;
taxRequest.IncludeItems = false;
taxRequest.TaxonomyLanguage = _apiTaxonomy.ContentLanguage;
Ektron.Cms.TaxonomyData taxData = _apiTaxonomy.LoadTaxonomy(ref taxRequest);
if (!(taxData == null || string.IsNullOrEmpty(taxData.Path)))
{
taxonomypath.Text = taxData.Path;
m_strTaxonomyPath = taxData.Path;
}
else
{
m_strTaxonomyPath = "";
}
}
catch (Exception e)
{
ViewSet.SetActiveView(View);
}
}
#endregion
#region Button Events
protected void CancelButton_Click(object sender, EventArgs e)
{
ViewSet.SetActiveView(View);
}
protected void SaveButton_Click(object sender, EventArgs e)
{
int taxID = Convert.ToInt32(taxonomyid.Text);
TaxonomyId = taxID;
taxFilter.Value = TaxonomyId.ToString();
SetTaxonomySummary();
_host.SaveWidgetDataMembers();
ViewSet.SetActiveView(View);
}
#endregion
public EventArgs e { get; set; }
}
If I understand your question correctly, you have the taxonomy id and want to display all the content within that taxonomy. If that is not what you are after, then please clarify your question, and I'll try to help as best I can.
I find that the added baggage that comes along with a widget can sometimes make it hard to test for correct API and server control usage. For that reason, I'd recommend starting off with a simple ASPX page.
<ektron:ContentModelSource ID="contentModelSource" runat="server">
</ektron:ContentModelSource>
<ektron:ContentView ID="ContentView1" runat="server" ModelSourceID="contentModelSource" EktronCustomTemplate="Ektron_ContentList_Template">
</ektron:ContentView>
Normally, the examples you'll find for the templated server controls show the declarative syntax for setting the filters, where you put them right into the ASPX markup - right inside the ContentModelSource control. Something like:
<TaxonomyFilters>
<ektron:ContentTaxonomyFilter Field="Id" Operator="EqualTo" Value="208" />
</TaxonomyFilters>
(More examples here and here.)
But for what it sounds like you want to accomplish, you need to define the filters via code. This code does just that:
protected long TaxonomyId
{
get
{
long id;
long.TryParse(Request.QueryString["id"], out id);
return id;
}
}
protected bool IsRecursive
{
get
{
bool recursive;
bool.TryParse(Request.QueryString["recursive"], out recursive);
return recursive;
}
}
protected void Page_Init(object sender, EventArgs e)
{
if (TaxonomyId > 0)
{
if (IsRecursive)
{
var tm = new TaxonomyManager();
var td = tm.GetItem(TaxonomyId);
if (td != null)
{
var pathFilter = new ContentTaxonomyFilter();
pathFilter.Field = ContentTaxonomyProperty.Path;
pathFilter.Operator = CriteriaFilterOperator.StartsWith;
pathFilter.Value = td.Path;
contentModelSource.TaxonomyFilters.Add(pathFilter);
}
}
else
{
var filter = new ContentTaxonomyFilter();
filter.Field = ContentTaxonomyProperty.Id;
filter.Value = TaxonomyId.ToString();
filter.Operator = CriteriaFilterOperator.EqualTo;
contentModelSource.TaxonomyFilters.Add(filter);
}
}
}
A couple of things to note:
The TaxonomyId is a property. I did this so that there would be an easy point of comparison between this simple ASPX page and a custom widget where your property would be decorated with the WidgetDataMember attribute.
The code is in the Page_Init event handler. That is important. Page_Load is too late in the page's lifecycle. I assume this would also be true in the context of a widget.
My code for when IsRecursive == true is not necessarily optimized. You could get reasonable performance by turning on caching in the FrameworkAPI, but the idea of calling the api to get the taxonomyData and then using that to set the filter for the content seems a little off. Ideally, the ContentTaxonomyFilter would have a "Recursive" property.