Change Telerik grid columns based on Appsetting - grid

I have a Html.Telerik().Grid() that is bound to a model in my MVC view. I want it to return a link based on a value in the appsettings in the web.config. Basically, if this is the dev server then show the links but not on the production server, is that possible? I use Ajax binding and my bound column looks as follows:
columns.Bound(f => f.TechnicalKey)
.ClientTemplate("<# if (FileName != 'status.txt' && StatusText=='PROCESSED') { #><a href='/AType/DownloadAFile/<#= TechnicalKey #>'>Download</a> <# } else { #>Not available<# } #>")
.Title("").Filterable(false);
I want the status.txt to be a link on development but not on production (this is how it is now)
Thank you.
Jack

You need to set the client template in a different way depending on the fact your application is deployed or not:
if (/* some check to see if on production which is specific to your implementation */) {
columns.Bound(f => f.TechnicalKey)
.ClientTemplate("<# if (FileName != 'status.txt' && StatusText=='PROCESSED') { #>Download <# } else { #>Not available<# } #>")
.Title("").Filterable(false);
} else {
columns.Bound(f => f.TechnicalKey)
.ClientTemplate("<# if (FileName != 'status.txt' && StatusText=='PROCESSED') { #><a href='/AType/DownloadAFile/<#= TechnicalKey #>'>Download</a> <# } else { #>Not available<# } #>")
.Title("").Filterable(false);
}

I actually achieved this by adding a property in the domain object as follows:
public bool isProduction
{
get
{
return ConfigurationManager.AppSettings["ActivationURL"].Contains("production");
}
}
and then in the view I had:
.Columns(columns =>
{
columns.Bound(f => f.TechnicalKey)
.Template(f => { %>
<% if (f.StatusText == "PROCESSED")
{
if (!f.isProduction || f.FileName != "status.txt")
{
%>Download<%
}
else
{
%>Not available<%
}
}
else
{
%>Not available<%
}
%>
<% })
.ClientTemplate("<# if (StatusText=='PROCESSED') { if(!isProduction || FileName!='status.txt') { #><a href='/AType/DownloadAFile/<#= TechnicalKey #>'>Download</a> <# } else { #>Not available<# }} else { #>Not available<# } #>").Encoded(false).Title("").Filterable(false);
This way I cater for the initial server bound data and the later Ajax bound data.

Related

how to comapare single object in listItem1 to customersList.if same id it will perform update operation otherwise insertion operation

in my controller class this is my logic.
for (Customer listItem1 : customerList) {
System.out.println(listItem1);
customerList1= customerService.getCustomerList();
if(listItem1.CompareTo(customerList1)){
customerService.updateCustomer(listItem1);
}
else{
customerService.insertCustomer(listItem1);
}
you can try instanceof instead of CompareTo like so:
if(listItem1 instanceof customerList1) {
//your code
} else {
//your code
}

How to show intro text only in news website (asp.net mvc)

I have a function in my view:
#helper truncate(string input, int length)
{
if (input.Length<=length)
{
#input;
}
else
{
#input.Substring(0,length)#:...
}
}
If i write #truncate("some word",50) => it worked
But i write #truncate(item.Description, 50) => it error: Object reference not set to an instance of an object.
Please help me to fix my problem or show me another way to show intro text only in my site
Thanks!
if input is null, then you get error input.Length and input.Substring lines. You should check, if it is not null.
#helper truncate(string input, int length)
{
if(input == null)
{
//
}
else
{
if (input.Length <= length)
{
#input;
}
else
{
#input.Substring(0,length)#:...
}
}
}
EDIT (after comment)
you can use:
input = Regex.Replace(input , "<.*?>", string.Empty);
NOTE: Html.Raw returns markup that is not HTML encoded.

Read and write to ASP.NET cache from static method

I have a static method in a helper class named helper.getdiscount(). This class is ASP.NET frontend code and used by UI pages.
Inside this method I check if some data is in the ASP.NET cache then return it, otherwise it makes a service call and store the result in the cache and then returns that value.
Will this be a problem considering multiple threads might be accessing it at the same time?
if (HttpContext.Current.Cache["GenRebateDiscountPercentage"] == null)
{
IShoppingService service = ServiceFactory.Instance.GetService<IShoppingService>();
rebateDiscountPercentage= service.GetGenRebateDiscountPercentage().Result;
if (rebateDiscountPercentage > 0)
{
HttpContext.Current.Cache.Add("GenRebateDiscountPercentage", rebateDiscountPercentage, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
}
}
else
{
decimal.TryParse(HttpContext.Current.Cache["GenRebateDiscountPercentage"].ToString(), out rebateDiscountPercentage);
}
Please advise if this is fine or any better approach could be used.
try something like this with lock object.
static readonly object objectToBeLocked= new object();
lock( objectToBeLocked)
{
if (HttpContext.Current.Cache["GenRebateDiscountPercentage"] == null)
{
IShoppingService service = ServiceFactory.Instance.GetService<IShoppingService>();
rebateDiscountPercentage= service.GetGenRebateDiscountPercentage().Result;
if (rebateDiscountPercentage > 0)
{
HttpContext.Current.Cache.Add("GenRebateDiscountPercentage", rebateDiscountPercentage, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
}
}
else
{
decimal.TryParse(HttpContext.Current.Cache["GenRebateDiscountPercentage"].ToString(), out rebateDiscountPercentage);
}
}
Also you can look into following thread.
What is the best way to lock cache in asp.net?
Use these generic methods to use the cache for any type:
`public static void AddCache(string key, object Data, int minutesToLive = 1440)
{
if (Data == null)
return;
HttpContext.Current.Cache.Insert(key, Data, null, DateTime.Now.AddMinutes(minutesToLive), Cache.NoSlidingExpiration);
}
public static T GetCache<T>(string key)
{
return (T)HttpContext.Current.Cache.Get(key);
} `
Now to solve your problem:
`if(GetCache<decimal>("GenRebateDiscountPercentage") == null)
{
IShoppingService service = ServiceFactory.Instance.GetService<IShoppingService>();
rebateDiscountPercentage= service.GetGenRebateDiscountPercentage().Result;
if (rebateDiscountPercentage > 0)
{
AddCache("GetGenRebateDiscountPercentage", rebateDiscountPercentage);
}
}
else
{
rebateDiscountPercentage = GetCache<decimal>("GetGenRebateDiscountPercentage");
}
`

Symfony2 - Query Builder LIKE null values

I'm trying to build a dynamic query in response to a custom search from users. I have an issue: when I'm building the query, I don't have the results because the SELECT LIKE column comparison doesn't work with NULL values. How can I work around this issue, considering that query is dynamically built? So, users can give values or not to search criteria...
This is my code:
$qb->add('select', 'f')
->add('from', 'Bundle:Object f')
->add('where', $qb->expr()->andx(
$qb->expr()->like('f.c1',':c1'),
$qb->expr()->like('f.c2',':c2'),
$qb->expr()->like('f.c3',':c3')))
->add('orderBy', 'f.nnumCatalogo ASC');
if ($data->getField1() != null) {
$isField1 = true;
}
if ($data->getField2() != null) {
$isField2 = true;
}
if ($data->getField3() != null) {
$isField3 = true;
}
if ($isField1) {
$qb->setParameter('c1', $data->getField1());
} else {
$qb->setParameter('c1', '%');
}
if ($isField2) {
$qb->setParameter('c2', $data->getField2());
} else {
$qb->setParameter('c2', '%');
}
if ($isField3) {
$qb->setParameter('c3', $data->getField3());
} else {
$qb->setParameter('c3', '%');
}
With this code I have no results becuase of NULL values in some columns not selected with LIKE '%' (mysql).
Try this:
$qb->add('select', 'f')->add('from', 'Bundle:Object f');
if ($data->getField1() != null) {
$qb->andWhere('f.c1 like :c1')
->setParameter('c1', $data->getField1());
}
if ($data->getField2() != null) {
$qb->andWhere('f.c2 like :c2')
->setParameter('c2', $data->getField2());
}
if ($data->getField3() != null) {
$qb->andWhere('f.c3 like :c3')
->setParameter('c3', $data->getField3());
}
$qb->add('orderBy', 'f.nnumCatalogo ASC');

Can this Encryption/Decryption Code work on Windows phone 7

Hi
Have use this code before. Find it useful on Web. But Dont know how to convert it for wp7. Will some1 take a shot at it?
script language="JavaScript"
len=0;
function CalcKey()
{
len=0;
var temp=document.Encrypt.Key.value;
for(i=0;i<temp.length;i++)
{
len=len+temp.charCodeAt(i);
}
if(len==0)
{
alert('Please Enter the appropriate Key');
document.Encrypt.Key.focus();
}
return len;
}
function Encryption()
{
CalcKey();
document.Encrypt.Encrypted.value="";
var txt=document.Encrypt.normal.value;
var net="";
var fin=0;
if(len>0)
{
if(txt.length>0)
{
for(i=0;i<txt.length;i++)
{
fin=txt.charCodeAt(i)+len;
if(fin>99)
{
net=net+fin;
}
else
{
net=net+'0'+fin;
}
}
document.Encrypt.Encrypted.value=net;
document.Encrypt.normal.value="";
}
else
{
alert('Please Enter the Text to be Encrypted');
document.Encrypt.normal.focus();
}
}
}
function Decryption()
{
var txt=document.Encrypt.Encrypted.value;
var j=3;
var temp1;
var res="";
CalcKey();
if(len>0)
{
if(txt.length>0)
{
for(i=0;i<txt.length;i+=3)
{
var temp=txt.substring(i,j);
temp1=(parseInt(temp)-len);
var t=unescape('%'+temp1.toString(16));
if(t=='%d' || t=='%a')
{
res=res+' ';
}
else
{
res=res+t
}
j+=3;
}
document.Encrypt.normal.value=res;
document.Encrypt.Encrypted.value="";
}
else
{
alert('Please Enter the Encrypted Text');
document.Encrypt.Encrypted.focus();
}
}
}
It looks like this code simply escapes/unescapes text from a form like "HelloWorld" to some kind of integer shifted by its current position.
You can definitely port this algorithm across to C# - you'll need to use
some kind of ((int)c).ToString() in order to write
Char.ParseInt to read bits of the string back in
Alternatively there are existing encryption methods you can use - http://robtiffany.com/windows-phone-7/dont-forget-to-encrypt-your-windows-phone-7-data
It seems that all you need is a simple cipher or form of symmetric encryption.
Have a look at the AesManaged encryption algorithm.

Resources