如何code在GridView一个按钮单击事件,从它的行中的GridView控件返回一个值 [英] How to code a button click event in a GridView that returns a value from its row in the GridView
问题描述
有关稍曲标题的道歉。
基本上,我有一个项目,我连接到它的数据库。我使用的GridView,这一切都取决于显示一些数据库的内容和运行,但方式我设计,我需要能够单击每排一个按钮,在本质出口值的页面行到子程序中的一个细胞。
Basically, I have a project where I have a database connected to it. I'm displaying some of the contents of the database using a GridView, which is all up and running, but the way I'm designing the page I need to be able to click a button in each row that in essence exports the value of one of the cells in the row to a subroutine.
我已经试过Google上搜寻它颇有几分努力有关的东西,我发现在这里,但没有什么我能找到的功能会像我想它。
I've tried googling it quite a bit and trying related things I found here, but nothing I could find would function like I would like it to.
相关code如下。
标记:
<asp:GridView ID="Staffgv" runat="server" AutoGenerateColumns="false" OnRowCommand="Staffgv_RowCommand" AllowPaging="true" PageSize="20" OnPageIndexChanging="Staffgv_PageIndexChanging" BackColor="#f9f9f9" CssClass="gvStyle" >
<Columns>
<asp:TemplateField HeaderText="Start" InsertVisible="False" SortExpression="DateTimeStart">
<HeaderStyle Width="70px" CssClass="hdrGvStart"/>
<ItemTemplate>
<asp:Label ID="lblDateTimeStart" runat="server" Text='<%# Bind("DateTimeStart", "{0:t}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Finish" SortExpression="DateTimeEnd">
<HeaderStyle Width="70px" CssClass="hdrGvFinish"/>
<ItemTemplate>
<asp:Label ID="lblDateTimeEnd" runat="server" Text='<%# Bind("DateTimeEnd", "{0:t}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Forename" SortExpression="Forename">
<HeaderStyle Width="140px" CssClass="hdrGvForename"/>
<ItemTemplate>
<asp:Label ID="lblForename" runat="server" Text='<%# Bind("Forename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname" SortExpression="Surname">
<HeaderStyle Width="140px" CssClass="hdrGvSurname"/>
<ItemTemplate>
<asp:Label ID="lblSurname" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle CssClass="gvHeaderEdit" />
<ItemTemplate>
<asp:Button ID="Btnapptid" runat="server" Text="" CssClass="btnGVEdit" CommandName="FillStaffTables" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
和相关VB code:
And the relevant VB code:
Private Sub Staffgv_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles Staffgv.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
Dim row = DirectCast(e.Row.DataItem, DataRowView)
Dim apptid As Integer = Integer.Parse(row("AppointmentID").ToString)
Dim Btnapptid = DirectCast(e.Row.FindControl("Btnapptid"), Button)
'Btnapptid.Text = apptid
Btnapptid.ToolTip = apptid
End Select
End Sub
Protected Sub Staffgv_RowCommand(ByVal sender As Object, e As GridViewCommandEventArgs)
If (e.CommandName = "FillStaffTables") Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
lblTxtTester.Text = "AppointmentID"
TextboxTester.Text = index
End If
End Sub
如果有人想了解更多code喜欢用来填补在GridView我会后的code,只是不想发表太多不相干code。
If anyone would like more code like the code used to fill the GridView I'll post it, just didn't want to post too much irrelevant code.
感谢
推荐答案
如果你只想从按钮被点击时,GridView的第1行的价值。
您可以简单地利用 CommandArgument
的按钮( Btnapptid
)。这假定您不需要单击该按钮时,为GridView行索引。如果您确实需要这一点,那么请参阅解决方案B 的,否则,继续在这里。
You could simply utilize the CommandArgument
of your button (Btnapptid
). This assumes that you don't need the gridview row index for when the button is clicked. If you do need this, then please see Solution B, otherwise continue here.
首先,你需要修改按钮的 CommandArgument
在aspx页面
First, you'd need to modify your button's CommandArgument
in the aspx page
ASPX
<asp:Button ID="Btnapptid" runat="server" Text="" CssClass="btnGVEdit"
CommandName="FillStaffTables" CommandArgument='<%# Bind("AppointmentID") %>'/>
然后,你应该能够抢到 AppointmentID
像这样
VB (在 Staffgv_RowCommand()
)
If (e.CommandName = "FillStaffTables") Then
txtAppointmentID.Text = e.CommandArgument
End If
解决方案B
当你在GridView行需要1个多值单击按钮时
的请注意,这种解决方案需要在您的最终一对夫妇的变化的
- 创建一个额外的控制(在GridView),这应该认为,你想要得到的是单击按钮时的值。
- 填充说你想要的(价值通过
控件绑定()
在UI或的RowDataBound
在codebehind)。
- Create an additional control (in the gridview) which should hold the value that you want to get when the button is clicked.
- Fill said control with the value you want (via
Bind()
in the UI or inRowDataBound
in the codebehind).
接下来,在你的 RowCommand
你能够抓住与指数的帮助下,新创建的控制
变量(从你的例子),像这样
Next, in your RowCommand
you'd be able to grab the newly created control with the help of the index
variable (from your example) like so
Staffgv.Rows(index).FindControl("YOUR_CONTROLS_ID")
示例
说你决定创建一个 HiddenField
控制在其中的价值从数据库中存储
Example
Say that you decide to create a HiddenField
control in which to store the value from your database
ASPX (隐藏字段应该是某个地方的GridView内 - 就在 Btnapptid
应该是罚款)
ASPX (hidden field should be somewhere inside the gridview - right under Btnapptid
should be fine)
<asp:HiddenField ID="hfMyControl" runat="server" Visible="False"
Value='<%# Bind("SOME_DB_COLUMN") %>'/>
VB (在 Staffgv_RowCommand()
)
Dim hfMyControl As HiddenField = _
DirectCast(Staffgv.Rows(index).FindControl("hfMyControl"), HiddenField)
然后,只需使用 hfMyControl
hfMyControl.Value
您也可以重复这一切使用多个控件,以便潜在地访问在GridView存储在控制多个数据库值。
You could also repeat all this using multiple controls in order to potentially access multiple DB values stored in controls in the gridview.
这篇关于如何code在GridView一个按钮单击事件,从它的行中的GridView控件返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!