Saturday, March 30, 2013

Limiting Textarea Text,

In asp.net max length is not working when text mode is set to multi line.
To achieve same functionality for text area , we can call same function two event
onkeyup =" return CheckMaxLenghtOfMessage(this,160)" onchange =" return CheckMaxLenghtOfMessage(this,160)"

OnKeyup will come in light when user typing text by keybord.
OnChange will come in light when user copy paste the text in values.

solution is that only trim the extra value.


  function CheckMaxLenghtOfMessage(sender, maxlimit) {
     
if (sender.value.length >= maxlimit) {

sender.value = sender.value.substring(0, maxlimit);

}
   }
     <asp:TextBox ID="test" runat="server" onkeyup =" return CheckMaxLenghtOfMessage(this,10)" onchange =" return CheckMaxLenghtOfMessage(this,160)" TextMode="MultiLine"></asp:TextBox>

Thanks
Mahesh k Sharma

Wednesday, March 20, 2013

JavaScriptSerializer DateTime Problem

  JavaScriptSerializer  class Deserialize Method does not return same date time wich is is not returning the value which is Serializing by Serialize Method define in JavaScriptSerializer. At least for me it is bug. :) System.Web.Script.Serialization.



public class SimpleClassWithJavaScriptSerializerError {


private DateTime m_Date;


public DateTime Date {


get { return m_Date; }


set { m_Date = value; }//Code which is giving error.

}

}


public class SimpleClassFixForAjax {


private DateTime m_Date;


public DateTime Date {


get { return m_Date; }


set { m_Date = DateTime.SpecifyKind(value, DateTimeKind.Utc); }//This way can fix the problem

}

}


protected void Page_Load(object sender, EventArgs e) {

System.Web.Script.Serialization.
JavaScriptSerializer javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();


DateTime dt = DateTime.Now;


SimpleClassWithJavaScriptSerializerError simpleClassWithJavaScriptSerializerError = new SimpleClassWithJavaScriptSerializerError();

simpleClassWithJavaScriptSerializerError.Date = dt;

Response.Write(

"<br/><br/>This is error from JavaScriptSerializer becase deserilaization is not returning which is value which is serialized.");

Response.Write(
string.Format("<br/> Before serialization: {0}", simpleClassWithJavaScriptSerializerError.Date));


string jsonStr = javaScriptSerializer.Serialize(simpleClassWithJavaScriptSerializerError);


SimpleClassWithJavaScriptSerializerError newInstance = javaScriptSerializer.Deserialize<SimpleClassWithJavaScriptSerializerError>(jsonStr);

Response.Write(
string.Format("<br/> After serialization: {0}",newInstance.Date));


//How to fix

Response.Write(
"<br/>------------------------------------------------------------------------------------------------");

 


SimpleClassFixForAjax simpleClassFixForAjax = new SimpleClassFixForAjax();

simpleClassFixForAjax.Date = dt;

Response.Write(

string.Format("<br/> Before serialization: {0}", simpleClassFixForAjax.Date));

jsonStr = javaScriptSerializer.Serialize(simpleClassFixForAjax);


SimpleClassFixForAjax simpleClassFixForAjaxDes = javaScriptSerializer.Deserialize<SimpleClassFixForAjax>(jsonStr);

Response.Write(
string.Format("<br/> After serialization: {0}", simpleClassFixForAjaxDes.Date));


DateTime d = DateTime.SpecifyKind(new DateTime(), DateTimeKind.Utc);

}