Friday, December 4, 2009

Code to Add Controls dynamically to Form

for (int k = 0; k < 5; k++)

{

TextBox t = new TextBox();

this.form1.Controls.Add(t);

LiteralControl l = new LiteralControl("<br/>");

this.form1.Controls.Add(l);

}



Note:We have taken the help of literal controls to place a TextBox in NextLine.
Here we are generating 5 TextBoxes.

PlaceHolder PlaceHolder1 = new PlaceHolder();

for (int i=1; i<=10; i++)
{
Label lbl = new Label();
lbl.Text = "Label" + i.ToString();
lbl.ID = "Label" + i.ToString();
PlaceHolder1.Controls.Add(lbl);
PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
} 

Here we are adding 10 Labels.

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);
}