Sunday, December 21, 2008

Javascript:Window.Open Invalid Argument Error IE

Internet Explorer (IE) starts giving Javascript error when I tried to open Pop-up Dialog, Which works perfect with Fire Fox and Chrome.


So If you are getting javascript error in IE on doing Javascript:Window.Open

Javascript Error: Invalid Argument or Syntax Error 
Example: (In Asp.net Code Behind)
string url = "MyTestURL?qsTestId=" + TestId;

string winFeatures = "toolbar=no,status=no,menubar=no,height=400,width=400";

string winPoPUpScript = string.Format("javascript:window.open('{0}', 'My Test Popup', '{1}');", url, winFeatures);

lnkCounter.Attributes.Add("onclick", winPoPUpScript);

Opening Window.open with javascript works perfectly with Firefox and Chrome, but will give error with IE.

Solution (Quick Fix): 
You need to remove spaces from 2nd Argument of Javascript Function Window.open() to make it work with IE.   If your second argument of Window.Open contains spaces than you may receive javascript errors like Invalid Argument or Syntax Error. 

To Fix the above code we need remove spaces from 2nd argument of Window.open
Example: (In Asp.net Code Behind)
string url = "MyTestURL?qsTestId=" + TestId;

string winFeatures = "toolbar=no,status=no,menubar=no,height=400,width=400";

string winPoPUpScript = string.Format("javascript:window.open('{0}', 'MyTestPopup', '{1}');", url, winFeatures);

lnkCounter.Attributes.Add("onclick", winPoPUpScript);

Sunday, December 14, 2008

Maintain Scroll Position Rating Control Ajax

Update to my previous post Rating Control Ajax - Display Message on Rating

Ajax Rating Control has bug, that is whenever user clicks the rating control, page causes jumps to top of the page. To avoid jump to top of page when user clicks Rating Control you need to add following line.

Add following line to maintain scroll position of page after user clicks rating control asp.net ajax.


protected void Page_Load(object sender, EventArgs e)
{
Rating1.Attributes.Add("onclick", "return false;");
}

For Detailed explanation about why Rating control jumps to top of page when user clicks on Rating control

Saturday, December 13, 2008

MSXML to System.xml with C# - XSL Transformation

Good Link for XSLT Transformation

XSL Transformation


This step-by-step article describes the features of MSXML and .NET Framework Classes for XML processing. This article also includes samples on how to use MSXML in Microsoft Visual Basic 6.0 and how to use .NET Framework Classes for XML processing.

http://support.microsoft.com/kb/330589

This article mainly focus on following
  • MSXML vs. System.xml for XML Processing
  • XSL Transformation
  • XML Validation
  • XML Navigation

Wednesday, December 10, 2008

Reflection in C# - List of Class Name, Method Name

Reflection is way through which we can identify metadata about assembly runtime.
Example: We have a .Net Assembly file (.dll file), which consist of 2 Class Definition and 10 Method Names, We can get information about classes and method name list through reflection.

Few Examples of Reflection

Loading Assembly FileAssembly assem = Assembly.LoadFrom(sAssemblyFileName);

Get List of Class Name.
Type[] types = assem.GetTypes();

Get List of Method Name


foreach (Type cls in types)
{
try
{
//Add Namespace Name
arrl.Add(cls.FullName);

//Add Class Name
if(cls.IsAbstract)
arrl.Add("Abstract Class:" + cls.Name.ToString());
else if(cls.IsPublic)
arrl.Add("Public Class:" + cls.Name.ToString());
else if(cls.IsSealed)
arrl.Add("Sealed Class:" + cls.Name.ToString());


MemberInfo[] methodName = cls.GetMethods();
foreach (MemberInfo method in methodName)
{
if (method.ReflectedType.IsPublic)
arrl.Add("\tPublic - " + method.Name.ToString());
else
arrl.Add("\tNon-Public - " + method.Name.ToString());
}
}
catch (System.NullReferenceException)
{
Console.WriteLine("Error msg");
}
}


A Sample Application Showing Good Usage of Reflection in .Net
Following application shows how to identify list of Classes and Method in any .Net Assembly file with the help of .Net Reflection. For an example I am using Ajax Assembly filename to list all its classname and method name.



Step 1: Design a web form consisting of following controls.
Label – Shows text “Assembly File Name”
Textbox – Taking Input Assembly File Name.
Listbox – For displaying list of Class Names and Method Names
Button – On Button Click Event Display List of Class Name and Method Name

Step 2: Write following code on Button_Click Event.


private void btnListMethodName_Click(object sender, EventArgs e)
{
string sAssemblyFileName = txtAssemblyFileName.Text;

if (sAssemblyFileName.Length != 0)
{
Assembly assem = Assembly.LoadFrom(sAssemblyFileName);
Type[] types = assem.GetTypes();
ArrayList arrl = new ArrayList();
foreach (Type cls in types)
{
try
{
//Add Class Name
arrl.Add(cls.FullName);
if(cls.IsAbstract)
arrl.Add("Abstract Class:" + cls.Name.ToString());
else if(cls.IsPublic)
arrl.Add("Public Class:" + cls.Name.ToString());
else if(cls.IsSealed)
arrl.Add("Sealed Class:" + cls.Name.ToString());

MemberInfo[] methodName = cls.GetMethods();
foreach (MemberInfo method in methodName)
{
//arrl.Add("\t" + method.Name.ToString());
if (method.ReflectedType.IsPublic)
arrl.Add("\tPublic - " + method.Name.ToString());
else
arrl.Add("\tNon-Public - " + method.Name.ToString());
}
}
catch (System.NullReferenceException)
{
Console.WriteLine("Error msg");
}
}

//Dump Data to NotePad File
FileStream fs = new FileStream(@"c:\myData.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
for (int i = 0; i < arrl.Count; i++)
{
lstInfo.Items.Add(arrl[i].ToString());
sw.WriteLine(arrl[i].ToString());
}
sw.Flush();
sw.Close();
}
}

Thursday, December 04, 2008

Rating Control Ajax - Display Message on Rating

One Famous Problem with Rating Control is How to Display Message after Rating is done, without making use of button click.

Following Images Explain How can we display label text on Rating Control Click, without explicitly clicking any button control.

Before Rating Control is Clicked


During Rating Control is Clicked


After Rating Control is Clicked


Now, lets understand how to display message on click of Star Image.

Step1: Declare CSS Styles in Style Sheet file Also Add Images to Image Folder


.ratingStar {
font-size: 0pt;
width: 31px;
height: 30px;
margin: 0px;
padding: 0px;
cursor: pointer;
display: block;
background-repeat: no-repeat;
}

.filledRatingStar {
background-image: url(Images/FilledStar.png);

}

.emptyRatingStar {
background-image: url(Images/EmptyStar.png);
}

.savedRatingStar {
background-image: url(Images/SavedStar.png);
}


Step2: Declare ScriptManager in .aspx file with EnablePartialRendering=true

<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server">
</asp:ScriptManager>

Step3: Declare UpdatePannel

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!-- Declare Rating Control Here -->
</ContentTemplate>
</asp:UpdatePanel>

Step4: Add Rating Control and Label in UpdatePannel

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!-- Rating Control -->
<cc1:Rating ID="Rating1" runat="server"
BehaviorID="RatingBhvrId1"
CurrentRating="3"
MaxRating="5"
StarCssClass="ratingStar"
WaitingStarCssClass="savedRatingStar"
FilledStarCssClass="filledRatingStar"
EmptyStarCssClass="emptyRatingStar"
OnChanged="Rating1_Changed"
ToolTip="Please Rate!"
style="float:left;">
</cc1:Rating>

<!-- Label to Display Message -->
<span id="lblResponse" class="heading"></span>
</ContentTemplate>
</asp:UpdatePanel>

Step5: Declare Necessary Javascript to show "Message" after user performs Rating with the help of e.CallbackResult

<script language="javascript" type="text/javascript">
Sys.Application.add_load(function()
{
$find("RatingBhvrId1").add_EndClientCallback(function(sender, e)
{
var lblCtrl = document.getElementById('lblResponse');
lblCtrl.innerHTML = e.get_CallbackResult();
});
});
</script>
Step6: Declaring Rating1_Changed Event
protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
{
System.Threading.Thread.Sleep(500);
int iRate = Convert.ToInt16(e.Value);
string strMessage = string.Empty;
switch (iRate)
{
case 1:
strMessage = "Not Useful";
break;
case 2:
strMessage = "Average";
break;
case 3:
strMessage = "Useful";
break;
case 4:
strMessage = "Informative";
break;
case 5:
strMessage = "Excellent";
break;
}
strMessage = "Thanks for Rating, You found this Question " + strMessage;
e.CallbackResult = strMessage;
}

Summary View of .aspx Page

<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<cc1:Rating ID="Rating1" runat="server"
BehaviorID="RatingBhvrId1"
CurrentRating="3"
MaxRating="5"
StarCssClass="ratingStar"
WaitingStarCssClass="savedRatingStar"
FilledStarCssClass="filledRatingStar"
EmptyStarCssClass="emptyRatingStar"
OnChanged="Rating1_Changed"
ToolTip="Please Rate!"
style="float:left;">
</cc1:Rating>
<br />
<span id="lblResponse" class="heading"></span>
<script language="javascript" type="text/javascript">
Sys.Application.add_load(function()
{
$find("RatingBhvrId1").add_EndClientCallback(function(sender, e)
{
var lblCtrl = document.getElementById('lblResponse');
lblCtrl.innerHTML = e.get_CallbackResult();
});
});
</script>
</ContentTemplate>
</asp:UpdatePanel>

Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape