Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

Edit GridView Using CheckBoxes

Sometime back I wrote a post about how to convert the whole GridView control into the edit mode with a single button click. You can check out the post here. One gentleman posted that how he can edit only the selected rows. This selection is based on selecting the Checkboxes which resides inside the GridView control.

First, lets see the GridView code:

<asp:GridView ID="gvCategories" runat="server" AutoGenerateColumns="false">

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" onclick="editMode(this)" />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Category Name">

<ItemTemplate>

<asp:TextBox ID="txtCategoryName" BorderWidth="0px" CssClass="textbox" runat="server"

Text='<%# Eval("CategoryName") %>' />

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

 

Now, let's check out the JavaScript code which is used to enable and disable the Textboxes inside the row.

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

disableGridViewTextBoxes();

// make all the input elements hidden

function disableGridViewTextBoxes()

{

var gvControl = document.getElementById("gvCategories");

var inputElements = gvControl.getElementsByTagName("INPUT");

for(i=0;i<inputElements.length;i++)

{

if(isTextBox(inputElements[i]))

{

inputElements[i].disabled = true;

}

}

}

function isTextBox(obj)

{

return obj.type == 'text';

}

function editMode(obj)

{

var rowObject = obj.parentElement.parentElement;

var inputElements = getElementsByTagName(rowObject,"INPUT");

if(obj.checked)

{

showElements(inputElements,"INPUT","text");

}

else

{

hideElements(inputElements,"text");

}

}

function showElements(list, tagName,type)

{

for(i=0;i<list.length;i++)

{

if(isTypeOf(list[i],"text"))

{

list[i].disabled = false;

list[i].focus();

list[i].select();

}

}

}

function isTypeOf(obj,type)

{

return obj.type == type;

}

 

function hideElements(list, type)

{

for(i=0; i<list.length;i++)

{

if(isTypeOf(list[i],type))

{

list[i].disabled = true;

}

}

}

function getElementsByTagName(obj,tagName)

{

return obj.getElementsByTagName(tagName);

}

Take a look at the effect in the following animation.

Sponsor

Published Thursday, June 28, 2007 6:37 PM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Rhonda Tipton&#8217;s WebLog Links - 07.03.07 &laquo; @ Monday, July 02, 2007 8:02 PM

PingBack from http://rtipton.wordpress.com/2007/07/02/links-070307/

Rhonda Tipton’s WebLog Links - 07.03.07 «

# 利用ASP.NET AJAX实现表格更新程序 @ Monday, July 09, 2007 5:22 AM

今天看了一个网站http://aspadvice.com/blogs/azamsharp/archive/2007/06/28/Edit-GridView-Using-CheckBoxes.aspx,...

Leepy

# re: Edit GridView Using CheckBoxes @ Sunday, July 15, 2007 11:39 AM

Good ! thank the author.

窃听器

# re: Edit GridView Using CheckBoxes @ Sunday, July 15, 2007 4:34 PM

Good ! thank the author.

窃听器

# re: Edit GridView Using CheckBoxes @ Monday, July 16, 2007 6:41 AM

Good ! thank the author.

窃听器

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below