如果为特定行单击按钮字段,如何获取更新我的数据库 [英] How do I get the update my databse when a button field is clicked for a particular row
问题描述
我的网络应用程序中有一个gridview。 gridview中填充了我的Db记录。我在每行包含记录的按钮字段。我想在点击griview中的按钮时更新我的数据库中的记录。例如,如果我有这样的东西
SN NAME ADDRESS PHONE SignOut Action
1 Mike abc 0984 Logout(这是一个按钮字段)。单击注销按钮时,我想更新数据库中的同一行。我需要能够获得值(1),以便我可以更新我的数据库中的signOut字段
我尝试过:
这是我的gridview
< pre lang = ASP.NET > ; < asp:GridView ID = GridView1 < span class =code-attribute> runat = server AllowPaging = True 高度 = 326px OnPageIndexChanging = GridView1_PageIndexChanging PageSize = 5 style = text-align:left; margin-left:169px 宽度 = 1069px OnSelectedIndexChanged = GridView1_SelectedIndexChanged OnRowDataBound = GridView1_RowDataBound AutoGenerateColumns = False OnRowCommand = < span class =code-keyword> GridView1_RowCommand OnRowEditing = GridView1_RowEditing >
< 列 >
< asp:BoundField < span class =code-attribute> HeaderText = S / N DataField = SN / >
< asp:BoundField HeaderText = 名字 DataField = FirstName / >
< asp:BoundField HeaderText = 地址 DataField = 地址 / >
< asp:BoundField HeaderText = 电话号码 DataField = PhoneNumber / >
< asp:BoundField HeaderText = 性别 DataField = 性别 / >
< asp:BoundField HeaderText = 原因 DataField = 原因 / >
< asp:BoundField HeaderText = SignIn DataField = SignIn_Time / >
< asp:BoundField HeaderText = SignOut DataField = Signout_Time / >
< asp:TemplateField HeaderText = < span class =code-keyword> Action 可见 = True >
< ItemTemplate >
< asp:按钮 ID = out runat = server 文字 = 退出 CommandName = SignOut / >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< PagerSettings FirstPageText = 首先 LastPageText = 最后 模式 = NumericFirstLast PageButtonCount = 5 / >
< / asp:GridView >
我的代码背后
protected void GridView1_RowCommand( object sender,GridViewCommandEventArgs e)
{
if (e.Comman dName == SignOut)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow Grid = GridView1.Rows [index];
}
如果你想定义自己的,请不要使用commandName(删除它),例如为字段SN和Sex设置DataKeyNames(取决于你想要的字段值),
< asp:GridView ID = GridView1 runat = server DataKeyNames = SN,性别 AllowPaging = True 高度 = 326px OnPageIndexChanging = GridView1_PageIndexChanging >
< asp:TemplateField >
< ItemTemplate >
< asp:按钮 ID = 测试点击 文字 = 测试 runat = server OnClick = Unnamed_Click / >
< / ItemTemplate >
< / asp:TemplateField >
< / asp:GridVie w >
代码背后:
protected void Unnamed_Click(object sender,EventArgs e)
{
try
{
按钮TestClick =发送者为按钮;
GridViewRow rowlist = TestClick.NamingContainer as GridViewRow;
var rowindex = rowlist.RowIndex;
var SNField = this.GridView1.DataKeys [rowindex] [SN]。ToString();
var SexField = this.GridView1.DataKeys [rowindex]
[性别] .ToString();
}
catch(例外情况)
{
}
}
虽然您可以在RowCommand
中执行此操作,但您也可以在<$ c $中执行此操作c>单击 Button的事件处理程序,如下所示:
protected void out_Click( object sender,EventArgs e)
{
按钮b =(按钮)发送者;
GridViewRow row =(GridViewRow)b.NamingContainer;
if (row!= null )
{
int index = row.RowIndex; // 选择行索引
int ID = Convert.ToInt32(row.Cells [ 0 ]。文字);
// 此处的更新代码
}
// 确保使用数据源中的数据重新绑定网格以反映更改
}
然后确保你为按钮<连接了事件处理程序/ code>触发事件:
< asp:按钮 id = out runat = server text = 退出 onclick = out_Click >
< / asp:button >
I have a gridview in my web application. The gridview is filled with records from my Db. I have a button field on each row containing records. i want to update records in my db whenever a button in the griview is clicked. for example, If i have something like this
SN NAME ADDRESS PHONE SignOut Action
1 Mike abc 0984 Logout(this is a button field). When the "Log out" button is clicked, i want to update that same row in my database. i need to be able to get the value (1) so that i can update the signOut field in my db
What I have tried:
Here is my gridview
<pre lang="ASP.NET"><asp:GridView ID="GridView1" runat="server" AllowPaging="True" Height="326px" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5" style="text-align: left; margin-left: 169px" Width="1069px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:BoundField HeaderText="S/N" DataField="SN" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Address" DataField="Address" />
<asp:BoundField HeaderText="Phone Number" DataField="PhoneNumber" />
<asp:BoundField HeaderText="Sex" DataField="Sex" />
<asp:BoundField HeaderText="Reason" DataField="Reason" />
<asp:BoundField HeaderText="SignIn" DataField="SignIn_Time" />
<asp:BoundField HeaderText="SignOut" DataField="Signout_Time" />
<asp:TemplateField HeaderText="Action" Visible="True">
<ItemTemplate>
<asp:Button ID="out" runat="server" Text="Sign out" CommandName="SignOut"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="5" />
</asp:GridView>
my code behind
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SignOut")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow Grid = GridView1.Rows[index];
}
Don't use commandName(Delete it) if you want defined your own, example set "DataKeyNames" for fields "SN" and "Sex" (depend what field value you want to take),
<asp:GridView ID="GridView1" runat="server" DataKeyNames="SN,Sex" AllowPaging="True" Height="326px" OnPageIndexChanging="GridView1_PageIndexChanging" > <asp:TemplateField> <ItemTemplate> <asp:Button ID="TestClick" Text="Test" runat="server" OnClick="Unnamed_Click" /> </ItemTemplate> </asp:TemplateField> </asp:GridView>
Code Behind:
protected void Unnamed_Click(object sender, EventArgs e)
{
try
{
Button TestClick = sender as Button;
GridViewRow rowlist = TestClick.NamingContainer as GridViewRow;
var rowindex = rowlist.RowIndex;
var SNField = this.GridView1.DataKeys[rowindex]["SN"].ToString();
var SexField = this.GridView1.DataKeys[rowindex]
["Sex"].ToString();
}
catch (Exception ex)
{
}
}
While you could do it inRowCommand
, you could also do it at theClick
event handler of the Button like this:
protected void out_Click(object sender, EventArgs e) { Button b = (Button)sender; GridViewRow row = (GridViewRow)b.NamingContainer; if (row != null) { int index = row.RowIndex; //gets the row index selected int ID = Convert.ToInt32(row.Cells[0].Text); //your update code here } //Make sure to rebind your grid here with the data from the datasource to reflect the changes }
Then make sure you wired-up the event handler for yourButton
to trigger the event:
<asp:button id="out" runat="server" text="Sign out" onclick="out_Click"> </asp:button>
这篇关于如果为特定行单击按钮字段,如何获取更新我的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!