如何在gridview中的按钮单击上存储行值 [英] How to Store row value on button click inside gridview
本文介绍了如何在gridview中的按钮单击上存储行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用网格视图,并且在每个行中都有一个编辑按钮,我希望在按钮上单击行选择单元格[1]值的值以存储在标签中..
使用asp.net c#
解决方案
请看这个链接。
http://www.aspsnippets.com/Articles/Get-selected-按Grid-on-Button-Click-in-ASPNet.aspx [ ^ ]
这里是删除按钮的代码。
在aspx文件中我们添加代码:
< asp:GridView ID = GridView1 runat = 服务器 AutoGenerateColumns = 错误 CellPadding = 4 EnableModelValidation = True
字体名称 = Tahoma 字体大小 = 8pt ForeColor = #333333 宽度 = < span class =code-keyword> 100% OnRowCommand = GridView1_RowCommand >
< AlternatingRowStyle BackColor = 白色 ForeColor = #284775 / >
< 列 >
< asp:BoundField DataField = No HeaderText = 否。 >
< HeaderStyle 宽度 = 100px / >
< ItemStyle 宽度 = 100px / >
< / asp:BoundField >
< asp:TemplateField HeaderText = 删除 >
< HeaderStyle 宽度 = 50px / >
< ItemStyle 宽度 = 50px / >
< ItemTemplate >
< asp:ImageButton runat = 服务器 CommandName = delete ImageUrl = 〜/ images / delete.png CommandArgument =' <% #Eval( ID)%> ' / >
< / ItemTemplate >
< / asp:TemplateField >
< asp:BoundField DataField = CustName HeaderText = 名称 / >
< < span class =code-leadattribute> / Columns >
< EditRowStyle BackColor = #999999 / >
< FooterStyle BackColor = #5D7B9D 字体粗体 = 真 ForeColor = 白色 / >
< HeaderStyle BackColor = #5D7B9D 字体粗体 = True ForeColor = 白色 / >
< PagerStyle BackColor = #284775 < span class =code-attribute> ForeColor = 白色 HorizontalAlign = 中心 / >
< RowStyle BackColor = #F7F6F3 ForeColor = #333333 / >
< SelectedRowStyle BackColor = #E2DED6 Font-Bold = True ForeColor = #333333 / >
< / asp:GridView < span class =code-keyword>>
在代码中我们添加以下代码:
受保护 void GridView1_RowCommand( object sender,GridViewCommandEventArgs e)
{
if (e。 CommandName.Equals( delete,StringComparison.OrdinalIgnoreCase))
{
// 获取所需记录的ID
var rowId = Convert.ToInt32(e.CommandArgument);
// ....
GridView1.DataBind(); // 刷新数据源
}
}
I am using a grid view, and inside that there's a edit button for each row, i want on button's click the values of row selected cell[1] value to store in a a label..
using asp.net c#
解决方案
Please see this link.
http://www.aspsnippets.com/Articles/Get-selected-row-in-GridView-on-Button-Click-in-ASPNet.aspx[^]
here is code for delete button.
in aspx file we add code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" EnableModelValidation="True" Font-Names="Tahoma" Font-Size="8pt" ForeColor="#333333" Width="100%" OnRowCommand="GridView1_RowCommand"> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <Columns> <asp:BoundField DataField="No" HeaderText="No."> <HeaderStyle Width="100px" /> <ItemStyle Width="100px" /> </asp:BoundField> <asp:TemplateField HeaderText="Delete"> <HeaderStyle Width="50px" /> <ItemStyle Width="50px" /> <ItemTemplate> <asp:ImageButton runat="server" CommandName="delete" ImageUrl="~/images/delete.png" CommandArgument='<%#Eval("ID")%>' /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="CustName" HeaderText="Name" /> </Columns> <EditRowStyle BackColor="#999999" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> </asp:GridView>
And in code behind we add following code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("delete", StringComparison.OrdinalIgnoreCase)) { //get id of record that you want var rowId = Convert.ToInt32(e.CommandArgument); //.... GridView1.DataBind();//refresh data source } }
这篇关于如何在gridview中的按钮单击上存储行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文