Hello
I'm not able to apply in-built filters or in fact any filters on my grid. I followed these two demos in mix:
http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/edit-form-types/defaultcs.aspx#qsf-demo-source
http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx
Please help.
ASPX code :-
<!------------------------------------------------------------------------------------------------------------------->
<%@ Page Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" Inherits="UserPermissionManager.Applications" CodeBehind="Applications.aspx.cs" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="CTP" runat="Server">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<table align="center" cellspacing="10px" border="0">
<tr align="center">
<td>
<input runat="server" type="hidden" id="hdUserName" name="hdUserName" />
</td>
</tr>
<tr>
<td align="left">
<div id="divlist" runat="server" visible="true" style="border: 1px solid #666666; background-color: #ffffff; font-size: 11px; overflow: scroll; height: 579px; width: 1860px;">
<asp:CheckBox ID="chkshowinactive" runat="server" Visible="true"
Text="Show Inactive" CausesValidation="false"
Checked="false" OnCheckedChanged="rad_needpermssion_SelectedIndexChanged"
AutoPostBack="True" CssClass="Checkbox" />
<br />
<%--<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />--%>
<%--<telerik:RadSkinManager ID="QsfSkinManager" runat="server" ShowChooser="true" />--%>
<%--<telerik:RadFormDecorator ID="QsfFromDecorator" runat="server" DecoratedControls="All" EnableRoundedCorners="false" />--%>
<telerik:RadCodeBlock ID="RadCodeBlock2" runat="server">
<script type="text/javascript">
function RowDblClick(sender, eventArgs) {
sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
}
function onPopUpShowing(sender, args) {
args.get_popUp().className += " popUpEditForm";
}
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxManagerProxy ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting>
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManagerProxy>
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" ShowFooter="True"
AllowSorting="True" AutoGenerateColumns="False" ShowStatusBar="True"
OnPreRender="RadGrid1_PreRender" OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
OnInsertCommand="RadGrid1_InsertCommand" OnItemDataBound="RadGrid1_ItemDataBound"
AllowFilteringByColumn="true" CellSpacing="-1" GridLines="Both">
<PagerStyle Mode="NextPrevAndNumeric" Position="TopAndBottom" />
<HeaderStyle Font-Bold="true" />
<MasterTableView CommandItemDisplay="Top" DataKeyNames="AppID" PageSize="15">
<EditFormSettings>
<PopUpSettings Modal="true" />
</EditFormSettings>
<Columns>
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />
<telerik:GridBoundColumn Visible="true" SortExpression="AppName" DataField="AppName"
HeaderText="Application Name" ItemStyle-VerticalAlign="Top" />
<telerik:GridHyperLinkColumn DataTextField="NoOfPermTypes" SortExpression="NoOfPermTypes" HeaderText="# of Permission Types" DataNavigateUrlFields="AppID"
DataNavigateUrlFormatString="ApplicationPermission.aspx?AppID={0}" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Left" />
<telerik:GridBoundColumn Visible="true" SortExpression="Active" DataField="Active"
HeaderText="Active" ItemStyle-VerticalAlign="Top" AllowFiltering="false" />
<telerik:GridBoundColumn Visible="true" SortExpression="WebBased" DataField="WebBased"
HeaderText="Web Based" ItemStyle-VerticalAlign="Top" AllowFiltering="false" />
<telerik:GridBoundColumn Visible="true" SortExpression="ModifiedOn" DataField="ModifiedOn"
HeaderText="Modified On" ItemStyle-VerticalAlign="Top" DataFormatString="{0:d-MMM-yyyy hh:mm tt}" />
<telerik:GridBoundColumn Visible="true" SortExpression="ModifiedBy" DataField="ModifiedBy"
HeaderText="Modified By" ItemStyle-VerticalAlign="Top" />
</Columns>
<EditFormSettings EditFormType="Template" CaptionFormatString="Add/Edit Application">
<FormTemplate>
<table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
style="border-collapse: collapse;">
<tr>
<td>
<table id="Table3" cellspacing="1" cellpadding="1" border="0" class="module">
<tr>
<td>Application Name:</td>
<td>
<asp:TextBox ID="txtAppName" runat="server" Text='<%# Bind("AppName") %>' />
</td>
</tr>
<tr>
<td>Web Based:
</td>
<td>
<asp:CheckBox ID="chkWebBased" runat="server" Enabled='<%# (Container is GridEditFormInsertItem) ? false : true %>'
Checked='<%# (DataBinder.Eval(Container.DataItem,"WebBased")==DBNull.Value ? true:Eval("WebBased"))%>' />
</td>
</tr>
<tr>
<td>Active:
</td>
<td>
<asp:CheckBox ID="chkActive" runat="server" Enabled='<%# (Container is GridEditFormInsertItem) ? false : true %>'
Checked='<%# (DataBinder.Eval(Container.DataItem,"Active")==DBNull.Value ? true:Eval("Active"))%>' />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="lblError1" ForeColor="Red" runat="server"></asp:Label>
</td>
<td align="right">
<asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
CommandName="Cancel"></asp:Button>
</td>
</tr>
</table>
</FormTemplate>
</EditFormSettings>
</MasterTableView>
<ClientSettings>
<ClientEvents OnRowDblClick="RowDblClick" OnPopUpShowing="onPopUpShowing" />
</ClientSettings>
</telerik:RadGrid>
</div>
</td>
</tr>
<tr>
<td align="left">
<font color="#666666">* Red color represents the applications that does not need permissions.</font><br />
<font color="#666666">* Green color represents the applications that needs permissions.</font>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
<!------------------------------------------------------------------------------------------------------------------->
CodeBehind :-
/***************************************************************************************************/
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web.UI;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using UPMLibrary;
namespace UserPermissionManager
{
public partial class Applications : Page
{
AppPermission objApp = new AppPermission();
Users objusers = new Users();
string strFilters = "";
public DataTable BindApplications()
{
//the table index (.Tables[1]) in the below statement is done
//for TEMPORARY SUPPORT of old UPM and must changed back to (.Tables[0])
//after this new UPM is rolled OUT
/********************************************************************************************************************************************/
DataView aView = objApp.ListApplications().Tables[1].DefaultView;
/********************************************************************************************************************************************/
aView.RowFilter = "Active=" + ((chkshowinactive.Checked) ? false : true).ToString();
return aView.ToTable();
}
protected void Page_Load(object sender, EventArgs e)
{
((HtmlGenericControl)this.Master.FindControl("liApps")).Attributes.Add("class", "SelectedMainMenu");
if (!IsPostBack)
{
if (Session["PERMISSIONLOGONUSER"] != null)
objApp.CurrentUser = Session["PERMISSIONLOGONUSER"].ToString();
else
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "document.getElementById('CTP_hdUserName').value = document.getElementById('lblSupplier').innerText;", true);
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//RadGrid1.DataSource = ApplicationsTable;
//RadGrid1.DataBind();
//ApplicationsTable.PrimaryKey = new DataColumn[] { ApplicationsTable.Columns["AppID"] };
RadGrid1.MasterTableView.EditMode = (GridEditMode)Enum.Parse(typeof(GridEditMode), "PopUp");
//GridFilterMenu menu = RadGrid1.FilterMenu;
//int i = 0;
//while (i < menu.Items.Count)
//{
// if (menu.Items[i].Text == "NoFilter" ||
// menu.Items[i].Text == "Contains" ||
// menu.Items[i].Text == "DoesNotContain" ||
// menu.Items[i].Text == "StartsWith" ||
// menu.Items[i].Text == "EndsWith" ||
// menu.Items[i].Text == "EqualTo" ||
// menu.Items[i].Text == "NotEqualTo"
// )
// i++;
// else
// menu.Items.RemoveAt(i);
//}
}
protected void rad_needpermssion_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Applications"] = null;
RadGrid1.MasterTableView.Rebind();
}
protected void RadGrid1_PreRender(object sender, System.EventArgs e)
{
if (!IsPostBack && RadGrid1.MasterTableView.Items.Count > 1)
{
//RadGrid1.MasterTableView.Items[1].Edit = true;
//RadGrid1.EditIndexes.Add(0);
//RadGrid1.MasterTableView.Rebind();
//RadGrid1.Rebind();
}
}
private DataTable ApplicationsTable
{
get
{
object obj = Session["Applications"];
DataTable myDataTable = new DataTable();
if (obj != null)
{
myDataTable = (DataTable)obj;
//bool bActive = true;
//bActive = (chkshowinactive.Checked) ? false : true;
//myDataTable.DefaultView.RowFilter = "Active='" + bActive.ToString() + "'";
//myDataTable = myDataTable.DefaultView.ToTable();
//return myDataTable;
}
else
myDataTable = BindApplications();
DataView aView = myDataTable.DefaultView;
aView.RowFilter = strFilters;
//myDataTable = aView.ToTable();
//DataTable datasource;
//if (strFilters != "")
//{
DataRow[] drArr = myDataTable.Select(strFilters);
DataTable dtFiltered = myDataTable.Clone();
foreach (DataRow dr in drArr)
dtFiltered.ImportRow(dr);
// datasource = ApplicationsTable;
// DataView aView = datasource.DefaultView;
// aView.RowFilter = strFilters;
// datasource = aView.ToTable();
//}
//else
// datasource = ApplicationsTable;
//DataView aView = ApplicationsTable.DefaultView;
//aView.RowFilter = strFilters;
//DataTable dtFiltered = aView.ToTable();
Session["Applications"] = myDataTable;
return myDataTable;
}
}
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = ApplicationsTable;
ApplicationsTable.PrimaryKey = new DataColumn[] { ApplicationsTable.Columns["AppID"] };
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
//Get the instance of the right type
GridDataItem dataBoundItem = e.Item as GridDataItem;
//Check the formatting condition
if (dataBoundItem["Active"].Text.ToLower() == "true")
{
dataBoundItem["Active"].Text = "YES";
dataBoundItem["Active"].ForeColor = Color.Green;
dataBoundItem["Active"].Font.Bold = true;
dataBoundItem["AppName"].ForeColor = Color.Green;
dataBoundItem["AppName"].Font.Bold = true;
}
else
{
dataBoundItem["Active"].Text = "NO";
dataBoundItem["Active"].ForeColor = Color.Red;
dataBoundItem["AppName"].ForeColor = Color.Red;
}
if (dataBoundItem["WebBased"].Text.ToLower() == "true")
{
dataBoundItem["WebBased"].Text = "YES";
dataBoundItem["WebBased"].ForeColor = Color.Green;
dataBoundItem["WebBased"].Font.Bold = true;
}
else
{
dataBoundItem["WebBased"].Text = "NO";
dataBoundItem["WebBased"].ForeColor = Color.Red;
}
}
if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
{
GridEditableItem editform = (GridEditableItem)e.Item;
TextBox txtAppName = (TextBox)editform.FindControl("txtAppName");
Button btnUpdate = (Button)editform.FindControl("btnUpdate");
Label lblError1 = (Label)editform.FindControl("lblError1");
//btnUpdate.Attributes.Add("onclick", "return clientValidation(" + txtAppName.ClientID + "," + lblError1.ClientID + ");");
btnUpdate.Attributes.Add("onclick", "if(document.getElementById('" + txtAppName.ClientID + "').value == '') { document.getElementById('" +
lblError1.ClientID + "').innerText = 'Application name cannot be empty.'; document.getElementById('" + txtAppName.ClientID + "').focus(); return false; }");
txtAppName.Attributes.Add("onkeydown", "document.getElementById('" + lblError1.ClientID + "').innerText = '';");
}
}
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
TextBox txtAppName = (TextBox)editedItem.FindControl("txtAppName");
CheckBox chkWebBased = (CheckBox)editedItem.FindControl("chkWebBased");
CheckBox chkActive = (CheckBox)editedItem.FindControl("chkActive");
Label lblError1 = (Label)editedItem.FindControl("lblError1");
//Prepare new row to add it in the DataSource
string sAppID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["AppID"].ToString();
DataRow[] changedRows = ApplicationsTable.Select("AppID = " + sAppID);
Label lblError = new Label();
RadGrid1.Controls.Add(lblError);
if (changedRows.Length != 1)
{
lblError1.Text = "Unable to locate the Application for updating.";
e.Canceled = true;
return;
}
if (txtAppName.Text == "")
{
lblError1.Text = "Application name cannot be empty.";
txtAppName.Focus();
e.Canceled = true;
return;
}
//Update new values
Hashtable newValues = new Hashtable();
newValues["AppName"] = txtAppName.Text;
newValues["WebBased"] = chkWebBased.Checked;
newValues["Active"] = chkActive.Checked;
newValues["ModifiedOn"] = DateTime.Now;
newValues["ModifiedBy"] = "";
changedRows[0].BeginEdit();
try
{
foreach (DictionaryEntry entry in newValues)
changedRows[0][(string)entry.Key] = entry.Value;
changedRows[0].EndEdit();
ApplicationsTable.AcceptChanges();
objApp.APPID = Convert.ToInt32(sAppID);
objApp.AppName = txtAppName.Text;
objApp.WebBased = (chkWebBased.Checked) ? 1 : 0;
objApp.AppStatus = (chkActive.Checked) ? 1 : 0;
objApp.CurrentUser = "";
int ischanged = objApp.ChangeAppStatus();
lblError.Text = "Application is updated successfully!";
lblError.ForeColor = Color.Green;
}
catch (Exception ex)
{
changedRows[0].CancelEdit();
lblError1.Text = "Unable to update Application. Reason: " + ex.Message;
e.Canceled = true;
}
}
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
TextBox txtAppName = (TextBox)editedItem.FindControl("txtAppName");
CheckBox chkWebBased = (CheckBox)editedItem.FindControl("chkWebBased");
CheckBox chkActive = (CheckBox)editedItem.FindControl("chkActive");
Label lblError1 = (Label)editedItem.FindControl("lblError1");
//Create new row in the DataSource
DataRow newRow = ApplicationsTable.NewRow();
Label lblError = new Label();
RadGrid1.Controls.Add(lblError);
if (txtAppName.Text == "")
{
lblError1.Text = "Application name cannot be empty.";
txtAppName.Focus();
e.Canceled = true;
return;
}
//Insert new values
Hashtable newValues = new Hashtable();
newValues["AppName"] = txtAppName.Text;
newValues["WebBased"] = chkWebBased.Checked;
newValues["Active"] = chkActive.Checked;
newValues["ModifiedOn"] = DateTime.Now;
newValues["ModifiedBy"] = "";
//make sure that unique primary key value is generated for the inserted row
try
{
objApp.APPID = 0;
objApp.AppName = txtAppName.Text;
objApp.WebBased = (chkWebBased.Checked) ? 1 : 0;
objApp.AppStatus = (chkActive.Checked) ? 1 : 0;
objApp.CurrentUser = "";
newValues["AppID"] = objApp.ChangeAppStatus();
foreach (DictionaryEntry entry in newValues)
newRow[(string)entry.Key] = entry.Value;
ApplicationsTable.Rows.Add(newRow);
ApplicationsTable.AcceptChanges();
lblError.Text = "<font color=Green>Application is inserted successfully!</font>";
lblError.ForeColor = Color.Green;
}
catch (Exception ex)
{
lblError1.Text = "Unable to insert Application. Reason: " + ex.Message;
e.Canceled = true;
}
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
//if (e.CommandName == RadGrid.FilterCommandName)
//{
// Pair filterPair = (Pair)e.CommandArgument;
// string s1 = "Current Filter function: '" + filterPair.First + "' for column '" + filterPair.Second + "'";
// TextBox filterBox = (e.Item as GridFilteringItem)[filterPair.Second.ToString()].Controls[0] as TextBox;
// string s2 = "<br> Entered pattern for search: " + filterBox.Text;
// if (strFilters != "")
// {
// switch (filterPair.First.ToString())
// {
// case "Contains":
// strFilters = strFilters + " and " + filterPair.Second + " like '%" + filterBox.Text + "%'";
// break;
// case "DoesNotContain":
// strFilters = strFilters + " and " + filterPair.Second + " not like '%" + filterBox.Text + "%'";
// break;
// case "StartsWith":
// strFilters = strFilters + " and " + filterPair.Second + " like '%>" + filterBox.Text + "%'";
// break;
// case "EndsWith":
// strFilters = strFilters + " and " + filterPair.Second + " like '%" + filterBox.Text + "</%'";
// break;
// case "EqualTo":
// strFilters = strFilters + " and " + filterPair.Second + " like '%>" + filterBox.Text + "</%'";
// break;
// case "NotEqualTo":
// strFilters = strFilters + " and " + filterPair.Second + " not like '%>" + filterBox.Text + "</%'";
// break;
// }
// //strFilters = strFilters + " and " + filterExpressions[i].FieldName + " like '%" + filterExpressions[i].FieldValue + "%'";
// }
// else
// {
// switch (filterPair.First.ToString())
// {
// case "Contains":
// strFilters = filterPair.Second + " like '%" + filterBox.Text + "%'";
// break;
// case "DoesNotContain":
// strFilters = filterPair.Second + " not like '%" + filterBox.Text + "%'";
// break;
// case "StartsWith":
// strFilters = filterPair.Second + " like '%>" + filterBox.Text + "%'";
// break;
// case "EndsWith":
// strFilters = filterPair.Second + " like '%" + filterBox.Text + "</%'";
// break;
// case "EqualTo":
// strFilters = filterPair.Second + " like '%>" + filterBox.Text + "</%'";
// break;
// case "NotEqualTo":
// strFilters = filterPair.Second + " not like '%>" + filterBox.Text + "</%'";
// break;
// }
// //strFilters = filterExpressions[i].FieldName + " like '%" + filterExpressions[i].FieldValue + "%'";
// }
// //RadGrid1.MasterTableView.FilterExpression = strFilters;
// //RadGrid1.MasterTableView.Rebind();
// //ApplicationsTable.DefaultView.RowFilter = strFilters;
// //RadGrid1.DataSource = ApplicationsTable.DefaultView.ToTable();
// Session["Applications"] = null;
// RadGrid1.MasterTableView.Rebind();
//}
}
}
}
/*************************************************************************************************/
Please guide...
I'm not able to apply in-built filters or in fact any filters on my grid. I followed these two demos in mix:
http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/edit-form-types/defaultcs.aspx#qsf-demo-source
http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx
Please help.
ASPX code :-
<!------------------------------------------------------------------------------------------------------------------->
<%@ Page Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" Inherits="UserPermissionManager.Applications" CodeBehind="Applications.aspx.cs" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="CTP" runat="Server">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<table align="center" cellspacing="10px" border="0">
<tr align="center">
<td>
<input runat="server" type="hidden" id="hdUserName" name="hdUserName" />
</td>
</tr>
<tr>
<td align="left">
<div id="divlist" runat="server" visible="true" style="border: 1px solid #666666; background-color: #ffffff; font-size: 11px; overflow: scroll; height: 579px; width: 1860px;">
<asp:CheckBox ID="chkshowinactive" runat="server" Visible="true"
Text="Show Inactive" CausesValidation="false"
Checked="false" OnCheckedChanged="rad_needpermssion_SelectedIndexChanged"
AutoPostBack="True" CssClass="Checkbox" />
<br />
<%--<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />--%>
<%--<telerik:RadSkinManager ID="QsfSkinManager" runat="server" ShowChooser="true" />--%>
<%--<telerik:RadFormDecorator ID="QsfFromDecorator" runat="server" DecoratedControls="All" EnableRoundedCorners="false" />--%>
<telerik:RadCodeBlock ID="RadCodeBlock2" runat="server">
<script type="text/javascript">
function RowDblClick(sender, eventArgs) {
sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
}
function onPopUpShowing(sender, args) {
args.get_popUp().className += " popUpEditForm";
}
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxManagerProxy ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting>
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManagerProxy>
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" ShowFooter="True"
AllowSorting="True" AutoGenerateColumns="False" ShowStatusBar="True"
OnPreRender="RadGrid1_PreRender" OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
OnInsertCommand="RadGrid1_InsertCommand" OnItemDataBound="RadGrid1_ItemDataBound"
AllowFilteringByColumn="true" CellSpacing="-1" GridLines="Both">
<PagerStyle Mode="NextPrevAndNumeric" Position="TopAndBottom" />
<HeaderStyle Font-Bold="true" />
<MasterTableView CommandItemDisplay="Top" DataKeyNames="AppID" PageSize="15">
<EditFormSettings>
<PopUpSettings Modal="true" />
</EditFormSettings>
<Columns>
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />
<telerik:GridBoundColumn Visible="true" SortExpression="AppName" DataField="AppName"
HeaderText="Application Name" ItemStyle-VerticalAlign="Top" />
<telerik:GridHyperLinkColumn DataTextField="NoOfPermTypes" SortExpression="NoOfPermTypes" HeaderText="# of Permission Types" DataNavigateUrlFields="AppID"
DataNavigateUrlFormatString="ApplicationPermission.aspx?AppID={0}" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Left" />
<telerik:GridBoundColumn Visible="true" SortExpression="Active" DataField="Active"
HeaderText="Active" ItemStyle-VerticalAlign="Top" AllowFiltering="false" />
<telerik:GridBoundColumn Visible="true" SortExpression="WebBased" DataField="WebBased"
HeaderText="Web Based" ItemStyle-VerticalAlign="Top" AllowFiltering="false" />
<telerik:GridBoundColumn Visible="true" SortExpression="ModifiedOn" DataField="ModifiedOn"
HeaderText="Modified On" ItemStyle-VerticalAlign="Top" DataFormatString="{0:d-MMM-yyyy hh:mm tt}" />
<telerik:GridBoundColumn Visible="true" SortExpression="ModifiedBy" DataField="ModifiedBy"
HeaderText="Modified By" ItemStyle-VerticalAlign="Top" />
</Columns>
<EditFormSettings EditFormType="Template" CaptionFormatString="Add/Edit Application">
<FormTemplate>
<table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
style="border-collapse: collapse;">
<tr>
<td>
<table id="Table3" cellspacing="1" cellpadding="1" border="0" class="module">
<tr>
<td>Application Name:</td>
<td>
<asp:TextBox ID="txtAppName" runat="server" Text='<%# Bind("AppName") %>' />
</td>
</tr>
<tr>
<td>Web Based:
</td>
<td>
<asp:CheckBox ID="chkWebBased" runat="server" Enabled='<%# (Container is GridEditFormInsertItem) ? false : true %>'
Checked='<%# (DataBinder.Eval(Container.DataItem,"WebBased")==DBNull.Value ? true:Eval("WebBased"))%>' />
</td>
</tr>
<tr>
<td>Active:
</td>
<td>
<asp:CheckBox ID="chkActive" runat="server" Enabled='<%# (Container is GridEditFormInsertItem) ? false : true %>'
Checked='<%# (DataBinder.Eval(Container.DataItem,"Active")==DBNull.Value ? true:Eval("Active"))%>' />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="lblError1" ForeColor="Red" runat="server"></asp:Label>
</td>
<td align="right">
<asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
CommandName="Cancel"></asp:Button>
</td>
</tr>
</table>
</FormTemplate>
</EditFormSettings>
</MasterTableView>
<ClientSettings>
<ClientEvents OnRowDblClick="RowDblClick" OnPopUpShowing="onPopUpShowing" />
</ClientSettings>
</telerik:RadGrid>
</div>
</td>
</tr>
<tr>
<td align="left">
<font color="#666666">* Red color represents the applications that does not need permissions.</font><br />
<font color="#666666">* Green color represents the applications that needs permissions.</font>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
<!------------------------------------------------------------------------------------------------------------------->
CodeBehind :-
/***************************************************************************************************/
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web.UI;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using UPMLibrary;
namespace UserPermissionManager
{
public partial class Applications : Page
{
AppPermission objApp = new AppPermission();
Users objusers = new Users();
string strFilters = "";
public DataTable BindApplications()
{
//the table index (.Tables[1]) in the below statement is done
//for TEMPORARY SUPPORT of old UPM and must changed back to (.Tables[0])
//after this new UPM is rolled OUT
/********************************************************************************************************************************************/
DataView aView = objApp.ListApplications().Tables[1].DefaultView;
/********************************************************************************************************************************************/
aView.RowFilter = "Active=" + ((chkshowinactive.Checked) ? false : true).ToString();
return aView.ToTable();
}
protected void Page_Load(object sender, EventArgs e)
{
((HtmlGenericControl)this.Master.FindControl("liApps")).Attributes.Add("class", "SelectedMainMenu");
if (!IsPostBack)
{
if (Session["PERMISSIONLOGONUSER"] != null)
objApp.CurrentUser = Session["PERMISSIONLOGONUSER"].ToString();
else
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "document.getElementById('CTP_hdUserName').value = document.getElementById('lblSupplier').innerText;", true);
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//RadGrid1.DataSource = ApplicationsTable;
//RadGrid1.DataBind();
//ApplicationsTable.PrimaryKey = new DataColumn[] { ApplicationsTable.Columns["AppID"] };
RadGrid1.MasterTableView.EditMode = (GridEditMode)Enum.Parse(typeof(GridEditMode), "PopUp");
//GridFilterMenu menu = RadGrid1.FilterMenu;
//int i = 0;
//while (i < menu.Items.Count)
//{
// if (menu.Items[i].Text == "NoFilter" ||
// menu.Items[i].Text == "Contains" ||
// menu.Items[i].Text == "DoesNotContain" ||
// menu.Items[i].Text == "StartsWith" ||
// menu.Items[i].Text == "EndsWith" ||
// menu.Items[i].Text == "EqualTo" ||
// menu.Items[i].Text == "NotEqualTo"
// )
// i++;
// else
// menu.Items.RemoveAt(i);
//}
}
protected void rad_needpermssion_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Applications"] = null;
RadGrid1.MasterTableView.Rebind();
}
protected void RadGrid1_PreRender(object sender, System.EventArgs e)
{
if (!IsPostBack && RadGrid1.MasterTableView.Items.Count > 1)
{
//RadGrid1.MasterTableView.Items[1].Edit = true;
//RadGrid1.EditIndexes.Add(0);
//RadGrid1.MasterTableView.Rebind();
//RadGrid1.Rebind();
}
}
private DataTable ApplicationsTable
{
get
{
object obj = Session["Applications"];
DataTable myDataTable = new DataTable();
if (obj != null)
{
myDataTable = (DataTable)obj;
//bool bActive = true;
//bActive = (chkshowinactive.Checked) ? false : true;
//myDataTable.DefaultView.RowFilter = "Active='" + bActive.ToString() + "'";
//myDataTable = myDataTable.DefaultView.ToTable();
//return myDataTable;
}
else
myDataTable = BindApplications();
DataView aView = myDataTable.DefaultView;
aView.RowFilter = strFilters;
//myDataTable = aView.ToTable();
//DataTable datasource;
//if (strFilters != "")
//{
DataRow[] drArr = myDataTable.Select(strFilters);
DataTable dtFiltered = myDataTable.Clone();
foreach (DataRow dr in drArr)
dtFiltered.ImportRow(dr);
// datasource = ApplicationsTable;
// DataView aView = datasource.DefaultView;
// aView.RowFilter = strFilters;
// datasource = aView.ToTable();
//}
//else
// datasource = ApplicationsTable;
//DataView aView = ApplicationsTable.DefaultView;
//aView.RowFilter = strFilters;
//DataTable dtFiltered = aView.ToTable();
Session["Applications"] = myDataTable;
return myDataTable;
}
}
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = ApplicationsTable;
ApplicationsTable.PrimaryKey = new DataColumn[] { ApplicationsTable.Columns["AppID"] };
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
//Get the instance of the right type
GridDataItem dataBoundItem = e.Item as GridDataItem;
//Check the formatting condition
if (dataBoundItem["Active"].Text.ToLower() == "true")
{
dataBoundItem["Active"].Text = "YES";
dataBoundItem["Active"].ForeColor = Color.Green;
dataBoundItem["Active"].Font.Bold = true;
dataBoundItem["AppName"].ForeColor = Color.Green;
dataBoundItem["AppName"].Font.Bold = true;
}
else
{
dataBoundItem["Active"].Text = "NO";
dataBoundItem["Active"].ForeColor = Color.Red;
dataBoundItem["AppName"].ForeColor = Color.Red;
}
if (dataBoundItem["WebBased"].Text.ToLower() == "true")
{
dataBoundItem["WebBased"].Text = "YES";
dataBoundItem["WebBased"].ForeColor = Color.Green;
dataBoundItem["WebBased"].Font.Bold = true;
}
else
{
dataBoundItem["WebBased"].Text = "NO";
dataBoundItem["WebBased"].ForeColor = Color.Red;
}
}
if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
{
GridEditableItem editform = (GridEditableItem)e.Item;
TextBox txtAppName = (TextBox)editform.FindControl("txtAppName");
Button btnUpdate = (Button)editform.FindControl("btnUpdate");
Label lblError1 = (Label)editform.FindControl("lblError1");
//btnUpdate.Attributes.Add("onclick", "return clientValidation(" + txtAppName.ClientID + "," + lblError1.ClientID + ");");
btnUpdate.Attributes.Add("onclick", "if(document.getElementById('" + txtAppName.ClientID + "').value == '') { document.getElementById('" +
lblError1.ClientID + "').innerText = 'Application name cannot be empty.'; document.getElementById('" + txtAppName.ClientID + "').focus(); return false; }");
txtAppName.Attributes.Add("onkeydown", "document.getElementById('" + lblError1.ClientID + "').innerText = '';");
}
}
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
TextBox txtAppName = (TextBox)editedItem.FindControl("txtAppName");
CheckBox chkWebBased = (CheckBox)editedItem.FindControl("chkWebBased");
CheckBox chkActive = (CheckBox)editedItem.FindControl("chkActive");
Label lblError1 = (Label)editedItem.FindControl("lblError1");
//Prepare new row to add it in the DataSource
string sAppID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["AppID"].ToString();
DataRow[] changedRows = ApplicationsTable.Select("AppID = " + sAppID);
Label lblError = new Label();
RadGrid1.Controls.Add(lblError);
if (changedRows.Length != 1)
{
lblError1.Text = "Unable to locate the Application for updating.";
e.Canceled = true;
return;
}
if (txtAppName.Text == "")
{
lblError1.Text = "Application name cannot be empty.";
txtAppName.Focus();
e.Canceled = true;
return;
}
//Update new values
Hashtable newValues = new Hashtable();
newValues["AppName"] = txtAppName.Text;
newValues["WebBased"] = chkWebBased.Checked;
newValues["Active"] = chkActive.Checked;
newValues["ModifiedOn"] = DateTime.Now;
newValues["ModifiedBy"] = "";
changedRows[0].BeginEdit();
try
{
foreach (DictionaryEntry entry in newValues)
changedRows[0][(string)entry.Key] = entry.Value;
changedRows[0].EndEdit();
ApplicationsTable.AcceptChanges();
objApp.APPID = Convert.ToInt32(sAppID);
objApp.AppName = txtAppName.Text;
objApp.WebBased = (chkWebBased.Checked) ? 1 : 0;
objApp.AppStatus = (chkActive.Checked) ? 1 : 0;
objApp.CurrentUser = "";
int ischanged = objApp.ChangeAppStatus();
lblError.Text = "Application is updated successfully!";
lblError.ForeColor = Color.Green;
}
catch (Exception ex)
{
changedRows[0].CancelEdit();
lblError1.Text = "Unable to update Application. Reason: " + ex.Message;
e.Canceled = true;
}
}
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
TextBox txtAppName = (TextBox)editedItem.FindControl("txtAppName");
CheckBox chkWebBased = (CheckBox)editedItem.FindControl("chkWebBased");
CheckBox chkActive = (CheckBox)editedItem.FindControl("chkActive");
Label lblError1 = (Label)editedItem.FindControl("lblError1");
//Create new row in the DataSource
DataRow newRow = ApplicationsTable.NewRow();
Label lblError = new Label();
RadGrid1.Controls.Add(lblError);
if (txtAppName.Text == "")
{
lblError1.Text = "Application name cannot be empty.";
txtAppName.Focus();
e.Canceled = true;
return;
}
//Insert new values
Hashtable newValues = new Hashtable();
newValues["AppName"] = txtAppName.Text;
newValues["WebBased"] = chkWebBased.Checked;
newValues["Active"] = chkActive.Checked;
newValues["ModifiedOn"] = DateTime.Now;
newValues["ModifiedBy"] = "";
//make sure that unique primary key value is generated for the inserted row
try
{
objApp.APPID = 0;
objApp.AppName = txtAppName.Text;
objApp.WebBased = (chkWebBased.Checked) ? 1 : 0;
objApp.AppStatus = (chkActive.Checked) ? 1 : 0;
objApp.CurrentUser = "";
newValues["AppID"] = objApp.ChangeAppStatus();
foreach (DictionaryEntry entry in newValues)
newRow[(string)entry.Key] = entry.Value;
ApplicationsTable.Rows.Add(newRow);
ApplicationsTable.AcceptChanges();
lblError.Text = "<font color=Green>Application is inserted successfully!</font>";
lblError.ForeColor = Color.Green;
}
catch (Exception ex)
{
lblError1.Text = "Unable to insert Application. Reason: " + ex.Message;
e.Canceled = true;
}
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
//if (e.CommandName == RadGrid.FilterCommandName)
//{
// Pair filterPair = (Pair)e.CommandArgument;
// string s1 = "Current Filter function: '" + filterPair.First + "' for column '" + filterPair.Second + "'";
// TextBox filterBox = (e.Item as GridFilteringItem)[filterPair.Second.ToString()].Controls[0] as TextBox;
// string s2 = "<br> Entered pattern for search: " + filterBox.Text;
// if (strFilters != "")
// {
// switch (filterPair.First.ToString())
// {
// case "Contains":
// strFilters = strFilters + " and " + filterPair.Second + " like '%" + filterBox.Text + "%'";
// break;
// case "DoesNotContain":
// strFilters = strFilters + " and " + filterPair.Second + " not like '%" + filterBox.Text + "%'";
// break;
// case "StartsWith":
// strFilters = strFilters + " and " + filterPair.Second + " like '%>" + filterBox.Text + "%'";
// break;
// case "EndsWith":
// strFilters = strFilters + " and " + filterPair.Second + " like '%" + filterBox.Text + "</%'";
// break;
// case "EqualTo":
// strFilters = strFilters + " and " + filterPair.Second + " like '%>" + filterBox.Text + "</%'";
// break;
// case "NotEqualTo":
// strFilters = strFilters + " and " + filterPair.Second + " not like '%>" + filterBox.Text + "</%'";
// break;
// }
// //strFilters = strFilters + " and " + filterExpressions[i].FieldName + " like '%" + filterExpressions[i].FieldValue + "%'";
// }
// else
// {
// switch (filterPair.First.ToString())
// {
// case "Contains":
// strFilters = filterPair.Second + " like '%" + filterBox.Text + "%'";
// break;
// case "DoesNotContain":
// strFilters = filterPair.Second + " not like '%" + filterBox.Text + "%'";
// break;
// case "StartsWith":
// strFilters = filterPair.Second + " like '%>" + filterBox.Text + "%'";
// break;
// case "EndsWith":
// strFilters = filterPair.Second + " like '%" + filterBox.Text + "</%'";
// break;
// case "EqualTo":
// strFilters = filterPair.Second + " like '%>" + filterBox.Text + "</%'";
// break;
// case "NotEqualTo":
// strFilters = filterPair.Second + " not like '%>" + filterBox.Text + "</%'";
// break;
// }
// //strFilters = filterExpressions[i].FieldName + " like '%" + filterExpressions[i].FieldValue + "%'";
// }
// //RadGrid1.MasterTableView.FilterExpression = strFilters;
// //RadGrid1.MasterTableView.Rebind();
// //ApplicationsTable.DefaultView.RowFilter = strFilters;
// //RadGrid1.DataSource = ApplicationsTable.DefaultView.ToTable();
// Session["Applications"] = null;
// RadGrid1.MasterTableView.Rebind();
//}
}
}
}
/*************************************************************************************************/
Please guide...