关于GridView中的"编辑"功能

我想问一下,如果我不用GridView中自带的“编辑”功能,而是自己添加一个BUTTON,点击button后可以实现和点击"编辑"一样的功能,该如何实现呢?能否说的具体一... 我想问一下,如果我不用GridView中自带的“编辑”功能,而是自己添加一个BUTTON,点击button后可以实现和点击"编辑"一样的功能,该如何实现呢?
能否说的具体一点呢?
展开
 我来答
百度网友8304525
2009-07-10 · TA获得超过154个赞
知道小有建树答主
回答量:193
采纳率:0%
帮助的人:227万
展开全部
看看MSDN吧
=======================================
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
allowpaging="true"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
onrowcreated="CustomersGridView_RowCreated"
runat="server">

<columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="City"
headertext="City"/>
</columns>

</asp:gridview>
====================================================
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Add")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = CustomersGridView.Rows[index];

// Create a new ListItem object for the customer in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[2].Text);

// If the customer is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!CustomersListBox.Items.Contains(item))
{
CustomersListBox.Items.Add(item);
}
}
}

void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{

// The GridViewCommandEventArgs class does not contain a
// property that indicates which row's command button was
// clicked. To identify which row's button was clicked, use
// the button's CommandArgument property by setting it to the
// row's index.
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];

// Set the LinkButton's CommandArgument property with the
// row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString();
}

}

======================================================
关键部分:
1.<asp:buttonfield buttontype="Link" commandname="Add" text="Add"/>
——> commandname="Add"

2.void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Add")//——>根据1中的 commandname="Add"
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = CustomersGridView.Rows[index];

}

参考资料: http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友41b31623
2009-07-10 · TA获得超过216个赞
知道小有建树答主
回答量:349
采纳率:0%
帮助的人:167万
展开全部
EditTemplate下边放一个button 然后还在编辑事件里写就行了 ,!
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
goblin_y
2009-07-11 · 超过39用户采纳过TA的回答
知道答主
回答量:186
采纳率:0%
帮助的人:117万
展开全部
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:HyperLink ID="hyl_Edit" runat="server" CommandName="Add >编辑</asp:HyperLink>
</ItemTemplate>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
xf14837616
2009-07-10 · TA获得超过558个赞
知道小有建树答主
回答量:799
采纳率:0%
帮助的人:405万
展开全部
要设置editTemplate的格式
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式