CheckBox实现GridView的编辑(转载)

本文介绍如何使用ASP.NET GridView控件实现仅对选中行进行编辑的功能。通过在每一行添加复选框,并利用JavaScript控制TextBox的禁用状态,实现灵活的行编辑功能。

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

}

    </script>

源代码打包下载

Take a look at the effect in the following animation.

EditGridViewUsingCheckBoxes505.gif

转载于:https://www.cnblogs.com/ajaxworld/archive/2007/07/08/810569.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值