Technical Insights: Azure, .NET, Dynamics 365 & EV Charging Architecture

Category: C#

C# Code

Register Javascript to hide Div

Sometimes people add javascript into the aspx page instead of using code behind but there is a case when we need to add javascript in the runtime using code behind. This is the example where javascript is used to provide switching of visibility based on the table visibility status. I used this technique to hide another user control when user click expand button. Client ID is required to know the real ID of the user control after rendering. In here we also add onclick event to the image tag.

if (!this.Page.IsStartupScriptRegistered("tableScript"))
{
string script = @"function checkVisibility() "
+ @"{"
+ @"    var element = document.getElementById('" + this.OuterTableDetailSearch.ClientID + @"');"
+ @"    var elementBtn = document.getElementById('" + this.btnExp.ClientID + @"');"
+ @"    if (element.style['visibility'] == 'hidden')"
+ @"{"
+ @"element.style['visibility'] = 'visible';"
+ @"elementBtn.src = '/Images/button_expand.gif';"
+ @"}"
+ @"else"
+ @"{"
+ @"element.style['visibility'] = 'hidden';"
+ @"elementBtn.src = '/Images/image_casestudy_search.gif';"
+ @"}"
+ @"}";

this.Page.RegisterStartupScript("tableScript", script);
}

btnExp.Attributes.Add("OnClick",
               "javascript:checkVisibility()");

Row filter in Dataset

Here it will be useful for someone who would like to filter a particular record in datagrid. You need to create a dataview and filter it. Here, i would like to filter a particular record where the field name is “metakey” and i don’t want to show it when its value is “thumbnail”. So a record where metakey is thumbnail will not be shown at all. It’s a simple thing but it will be useful.

DataSet metaData = new DataSet();
dv = new DataView(metaData.Tables[0]);

gridMeta.DataSource = dv;
gridMeta.DataBind();

dv.RowFilter = ("metakey  'thumbnail'");
//bind it to datagrid

Using wild card to look up in a string C#

This is the class that can be used to look up a keyword from a string. This class is inherited from Regex class.
Base Class

public class ProductLookupRegex : Regex
{
public ProductLookupRegex()
{
//
// TODO: Add constructor logic here
//
}
///
/// Initializes a wildcard with the given search pattern.
///
///
The wildcard pattern to match.
public ProductLookupRegex(string pattern) : base(WildcardToRegex(pattern))
{
}

///
/// Initializes a wildcard with the given search pattern and options.
///
///
The wildcard pattern to match.
///
A combination of one or more
/// .
public ProductLookupRegex(string pattern, RegexOptions options): base(WildcardToRegex(pattern), options)
{
}

///
/// Converts a wildcard to a regex.
///
///
The wildcard pattern to convert.
/// A regex equivalent of the given wildcard.
public static string WildcardToRegex(string pattern)
{
return "^" + Regex.Escape(pattern).
Replace("\\*", ".*").
Replace("\\?", ".") + "$";
}
}

How to use it

ProductLookupRegex productlookup =
                 new ProductLookupRegex(productName,
                                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

foreach (DictionaryEntry de in prodList)
{

if (productlookup.IsMatch(de.Key.ToString().Trim()))
{
productLink = LinkGenerator(de.Value.ToString());
productName = de.Key.ToString();
break; // (sn)
}
}

Page 4 of 4

Powered by WordPress & Theme by Anders Norén