Tuesday, May 30, 2006

Checking All CheckBoxes in a GridView

Checking All CheckBoxes in a GridView

Here's the part which shows how to implement client-side JavaScript checking/unchecking
C#:
protected void Page_Load(object sender, System.EventArgs e)
{
// DataBind GridView here
//...

'On every page visit we need to build up the CheckBoxIDs array
foreach(GridViewRow row in grid.Rows)
{
//Get a programmatic reference to the CheckBox control
CheckBox chk = (CheckBox)row.FindControl("chk");
ClientScript.RegisterArrayDeclaration("CheckBoxIDs", string.Concat("'", chk.ClientID, "'"));
}
}

JavaScript:
<script type="text/javascript">
function ChangeCheckBoxState(id, checkState)
{
var cb = document.getElementById(id);
if (cb != null)
cb.checked = checkState;
}

function ChangeAllCheckBoxStates(checkState)
{
// Toggles through all of the checkboxes defined in the CheckBoxIDs array
// and updates their value to the checkState input parameter
if (CheckBoxIDs != null)
{
for (var i = 0; i < CheckBoxIDs.length; i++)
ChangeCheckBoxState(CheckBoxIDs[i], checkState);
}
}
</script>

...

<input type="button" value="Check All" onclick="ChangeAllCheckBoxStates(true);" />
 
<input type="button" value="Uncheck All" onclick="ChangeAllCheckBoxStates(false);" />

Tuesday, May 02, 2006

How to use DefaultFocus and DefaultButton properties with Master page

Want to set default button (submit) and default input control in ASP.NET 2.0?
No problem - use Page.Form.DefaultFocus and Page.Form.DefaultButton properties.

But what if you work with Master Page or trying to implement User Control (Server Control)?

Short answer:
=====
Page.Form.DefaultFocus = TextBox1.UniqueID;
Page.Form.DefaultButton = Button1.UniqueID;
=====

Longer answer is here:
How do I set DefaultFocus or DefaultButton

Keywords: ASP.NET 2.0, C# 2.0, MSDN2

Great ASP.NET 2.0 Tutorial Videos Online

Microsoft & Scott Guthrie's team published ASP.NET 2.0 Tutorial Videos:
Great ASP.NET 2.0 Tutorial Videos Online
The videos are short (~15 minutes each), easy to grasp and implement/repeat, and are very functional.
Enjoy!

Followers

About Me

My photo
Email me: blog@postjobfree.com