Tuesday, November 29, 2011

How to use CustomValidator with dropdown list


Here is an example of using CustomValidator with  dropdown list in asp.net application

Step 1 : Write  java script function
< script type="text/javascript" language="javascript" >

function CheckWorker(sender, args) {
                var worker = $get('<%=ddlstWorkers.ClientID %>');
                if (worker.selectedIndex == 0) {
                    args.IsValid = false;
                    return;
                }
                args.IsValid = true;
            }
< / script>

Step 2: Define the asp:DropDownList and CustomValidator

< asp:DropDownList ID="ddlstWorkers" runat="server" CssClass="ddl" Style="width: 220px;">
                                        < /asp:DropDownList >
                                        < asp:CustomValidator ID="cvWorker" runat="server" ControlToValidate="ddlstWorkers"   Display="None" ErrorMessage="Select a worker." ClientValidationFunction="CheckWorker" >< /asp:CustomValidator >

Monday, September 5, 2011

DateTime format problem in Csharp

Often we face this problem, there are may example on google, but still i feel like sharing this with you, this is how you can play with any date format in any culture.

DateTime dob = DateTime.Now.AddYears(-18);

 if (!string.IsNullOrEmpty(txtDOB.Text))
{
      IFormatProvider culture = new CultureInfo("en-US", true); // this is how you can set any culture you want !
  dob = DateTime.ParseExact(txtDOB.Text, "dd/MM/yyyy", culture);
 }

profile.DOB = dob;

Tuesday, August 9, 2011

convert enum to list in csharp

This function will convert any enum to a List object

public class EnumHelper
{
/// < summary >
/// Convert enum to a list object.
/// < /summary >
/// < typeparam name="T" >< /typeparam >
/// < returns >< /returns >
public static List< T > EnumToList< T >()
{
Type enumType = typeof(T);

// You can't use type constraints on value types, so have to check & throw error.
if (enumType.BaseType != typeof(Enum))
throw new ArgumentException("T must be of type System.Enum type");

Array enumValArray = Enum.GetValues(enumType);

List< T > enumValList = new List< T >(enumValArray.Length);

foreach (int val in enumValArray)
{
enumValList.Add((T)Enum.Parse(enumType, val.ToString()));
}

return enumValList;
}






// Now call the function with enum you want to conver into a list EnumHelper.EnumToList< EventState >()

public static List < EventState > GetEventStates()
{
List< EventState > eventStates = EnumHelper.EnumToList< EventState >();
//.FindAll(
//delegate(EventState x)
//{
// return x != EventState.Cancelled ;
//});
return eventStates;
}

Tuesday, April 26, 2011

WCF Service debuging technique from client

While implementing any WCF service in any client application, you may find lots of issue, here i share some experience how you can get to know whats going wrong at service end!




You just need to turn on Tracing and Messging on WCF server





As a result you see the following set of tags has been added in your web config file.

< system.diagnostics >
< sources >
< source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
propagateActivity="true" >
< listeners >
< add type="System.Diagnostics.DefaultTraceListener" name="Default" >
< filter type="" / >
< /add >
< add name="ServiceModelTraceListener" >
< filter type="" / >
< /add >
< /listeners >
< /source >
< source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing" >
< listeners >
< add type="System.Diagnostics.DefaultTraceListener" name="Default" >
< filter type="" / >
< /add >
< add name="ServiceModelMessageLoggingListener" >
< filter type="" / >
< /add >
< /listeners >
< /source >
< /sources >
< sharedListeners >
< add initializeData="F:\MyWCFFolder\Web_tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp" >
< filter type="" / >
< /add >
< add initializeData="F:\MyWCFFolder\Web_messages.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp" >
< filter type="" / >
< /add >
< /sharedListeners >
< /system.diagnostics >

Stop subsequent submit button clicks when a earlier request is being processed in ASP.NET

How we can stop the subsequent click when a earlier request is still been processed.

here is the javascript code you need to place above your update panel.
< script type="text/javascript" language="javascript" >
var requestManager = Sys.WebForms.PageRequestManager.getInstance();
requestManager.add_initializeRequest(StopPostbackOnSubsequentSubmitClicks);

function StopPostbackOnSubsequentSubmitClicks(sender, args) {
if (requestManager.get_isInAsyncPostBack() &
args.get_postBackElement().id == '<%=btnSubmit.ClientID %>')
{
args.set_cancel(true);
alert('Please wait, earlier request is still being processed');
}
}
< /script >

Now write your update panel code
< asp:UpdatePanel runat="server" ID="upnlLogin" UpdateMode="Conditional" >
< ContentTemplate >

// do whatever you need

< asp:Button ID="btnSubmit" runat="server" Text="Sign in" CssClass="button" OnClick="btnSubmit_Click" />

< /ContentTemplate>

< /asp:UpdatePanel>
< asp:UpdateProgress AssociatedUpdatePanelID="upnlLogin" runat="server" >
< ProgressTemplate>
< img src="../Images/spinner.gif" / >
< /ProgressTemplate >
< /asp:UpdateProgress >

// Now see the code behind implementation

protected void btnSubmit_Click(object sender, EventArgs e)
{

Thread.Sleep(1000);
}

ETG Consultancy

Web Designing Development Analysis & Promotion
Asp.Net 2.0 SQL WWF WCF SEO Marketing Ajax JQuery NHibernate MVC