Monday, November 16, 2009

sys is undefined

The reason for this error is your web.config does not contain all the settings required for an ajax enabled web site.
First , generate an ajax enabled web site namely ajaxsite1 and compare your web.config with that of ajaxsite1.
you observe that code in httpHandlers  is not there in your website or it might be improper.
Replace the web.config code in ajaxsite1 in to your website.
See that after running you will ge 'Done' at the bottom left.Previously you get 'Done with Errors' message in this portion.
Note:
All this errors and exceptions generally occur if you are working with Ajax Controls.You will  not get your ajax controls functionality in your  webpage unless you clear all these script errors.
Some of the controls that may raise this error are AutoComplete,CalendarExtendar etc

Tuesday, November 10, 2009

Reset DropDownList to Default Value

Use following lines:

DropDownList1.SelectedIndex = 0;

or

DropDownList1.ClearSelection();
DropDownList1.Items.FindByText("--Select--").Selected = true;

or

DropDownList1.ClearSelection();
DropDownList1.Items.FindByValue("--Select--").Selected = true;


Note:

We need to mention DropDownList1.ClearSelection(); before using DropDownList1.Items.FindByValue("--Select--").Selected = true
(or)DropDownList1.Items.FindByText("--Select--").Selected = true

Otherwise you will get an error 'Cannot have multiple items selected in a DropDownList'

Wednesday, November 4, 2009

How to access asp.net server controls usingJavaScript?

<form id="form1" runat="server">
<div>


<asp:textbox id="TextBox1" runat="server">
  1. document.getElementById('<%=TextBox1.ClientID%>').value;
    ClientID - server control identifier generated by ASP.NET

  2. document.forms[0]['TextBox1'].value;

Friday, April 24, 2009

Reset all controls

private void ResetFormControlValues(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.Controls.Count > 0)
{
ResetFormControlValues(c);
}
else
{
switch (c.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
((TextBox)c).Text = "";
break;
case "System.Web.UI.WebControls.CheckBox":
((CheckBox)c).Checked = false;
break;
case "System.Web.UI.WebControls.RadioButton":
((RadioButton)c).Checked = false;
break;

}
}
}
}


Call this method in ResetButton ClickEvent as below:

protected void btnReset_Click(object sender, EventArgs e)
{
ResetFormControlValues(this);
}

Tuesday, December 16, 2008

How to Get Printer option When a Button is Clicked?

The First thing to note here is when you click on the Button,the page content should be printed but the Button say PrintButton should not appear in the document that is going to get printed.


When you simply call window.print() , this problem occurs.

How to Solve this?

The Source Code for the Button created is as shown below:

<asp:Button ID="btnPrint" runat="server" CssClass="btn1" Text="Print Me"
OnClientClick="javascript:fun(this,'print')"/>

Observe that I am calling a function fun with 2 arguments ,i.e this and print.
'this' refers to Button with ID='btnPrint' i.e the Button which we want to hide after clicking.

The Second argument is nothing but Css ClassName.

The CssClass Code is as Below:

<style type="text/css">
.print
{
visibility:hidden;
}
.btn1
{
color:#050;
font: bold 84% 'trebuchet ms',helvetica,sans-serif;
background-color:#fed;
border:1px solid;
border-color: #696 #363 #363 #696;
}
</style>

The JavaScript function fun will be as shown below:

<script type="text/javascript" language="javascript">
function fun(loc,p)
{
loc.className=p
window.print();
}

</script>