Archive

Posts Tagged ‘listbox’

Listbox select all using checkbox through Javascript

March 29, 2010 3 comments

In this article, we gonna implement selection of all items in listbox using checkbox through javascript.

<script language=”javascript” type=”text/javascript”>

function SelectAllCountry(chkObj) {
var multi = document.getElementById(‘lstcountry’);
if (chkObj.checked)
for (i = 0; i < multi.options.length; i++)
multi.options[i].selected = true;
else
for (i = 0; i < multi.options.length; i++)
multi.options[i].selected = false;
}

</script>

<asp:CheckBox ID=”chkcountry” OnClick=”SelectAllCountry(this);”
runat=”server” Text=”Select All” />

<asp:ListBox ID=”lstcountry” runat=”server” Rows=”10″ SelectionMode=”Multiple”>

<asp:ListItem Text=”Australia” Value=”Australia”></asp:ListItem>

<asp:ListItem Text=”Germany” Value=”Germany”></asp:ListItem>

<asp:ListItem Text=”Holland” Value=”Holland”></asp:ListItem>

<asp:ListItem Text=”India” Value=”India”></asp:ListItem>

<asp:ListItem Text=”Japan” Value=”Japan”></asp:ListItem>

</asp:ListBox>

Reset/Clear all fields – Textbox/Dropdown/Radionbuttonlist/Listbox etc of a web form in Asp.net

May 27, 2009 11 comments

In most of the case after inserting data into database or carrying out any other transactions. in which we have one form that contains bunch of textbox’s, radiobuttonlist, checkboxlist, dropdownlist, checkbox, listbox etc or any other controls in asp.net .now after inserting data, on submit/save click we want to empty/reset/clear all controls in the web form, we actually do like this:

txtid.text =””;
ddid.SelectedIndex = -1;

and so on… and that for each textbox, dropdowns… etc it becomes very tedious and long.. so to make it short we should create a common function to reset all controls in web form…

Here is the ready made function :

Step 1:

///
/// Pankaj lalwani
/// Reset all textbox/radiobutton/dropdowns/listbox
///
///
public static void ClearFields(ControlCollection pageControls)
{
foreach (Control contl in pageControls)
{
string strCntName = (contl.GetType()).Name;
switch (strCntName)
{
case “TextBox”:
TextBox tbSource = (TextBox)contl;
tbSource.Text = “”;
break;
case “RadioButtonList”:
RadioButtonList rblSource = (RadioButtonList)contl;
rblSource.SelectedIndex = -1;
break;
case “DropDownList”:
DropDownList ddlSource = (DropDownList)contl;
ddlSource.SelectedIndex = -1;
break;
case “ListBox”:
ListBox lbsource = (ListBox)contl;
lbsource.SelectedIndex = -1;
break;
}
ClearFields(contl.Controls);
}
}

Step 2:
Call this method on submit/save click after your code ends or sometimes when you provide clear/reset button to empty all fields

ClearFields(Form.Controls);