如果不使用SelectedIndexChanged,如何从gridview中检索每一行的值?

时间:2012-01-11 07:35:42

标签: asp.net vb.net gridview

我有GridView不使用SelectedIndexChanged,如果点击每行中的每个按钮,如何从GridView检索每一行的值?

这是我的aspx代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
        DataSourceID="SqlDataSource1" ShowHeader="False" AllowPaging="True" BorderColor="White"
        CellPadding="6" GridLines="None" Height="100px" Width="800px">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    Card Name:
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                    <br />
                    Cost :
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
                    <br />
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductImgID") %>'></asp:Label>
                    <asp:Image ID="Image3" runat="server" ImageUrl='<%# Eval("ProductImgUrl", "images/{0}") %>' />
                    <br />
                    <asp:Button ID="btnAddProduct" runat="server" Text="Add" />
                    <br />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

4 个答案:

答案 0 :(得分:2)

一个选项可以是将按钮的CommandArgument绑定到ProductID

<asp:Button ID="btnAddProduct" runat="server" Text="Add" CommandArgument='<%#Bind("ProductID")%>' />

然后在RowCommand事件中检索ProductID并从数据库中提取行

string prodID=(string)e.CommandArgument()

然后使用ID从数据库中检索行。

答案 1 :(得分:0)

要获取行值,您必须获取行参考,获取行后,您可以轻松访问指定列并获取值

让我们考虑一下模板字段列中有“链接按钮控件”。对于gridview,您必须设置行命令事件,并且还要在列的编辑模板中,为链接按钮设置命令名称,也要说“lnkTest”

在RowCommand Event中,您必须包含以下代码部分

if(e.CommandName.Equals("lnkTest")) // Checks that link button was clicked

  { 
    GridViewRow grdRow = (((LinkButton)e.CommandSource).Container)

// This Will give you the reference of the Row in which the link button was clicked

    int grdRowIndex = grdRow.RowIndex;

//This will give you the index of the row

    var uniqueDataKeyValue = GridView1.DataKeys[grdRowIndex].Value;

//这将为您提供链接Control单击

的行的DataKey值

希望上面的代码能够提供帮助

答案 2 :(得分:0)

CommandArgument='<%# Container.DataItemIndex %>'添加到“添加”按钮

<asp:Button ID="btnAddProduct" runat="server" Text="Add" CommandArgument='<%# Container.DataItemIndex %>'/>

要检索Name,请在gridview行命令中使用此代码

Dim gvr As GridViewRow = grvGRNCONs.Rows(e.CommandArgument)
Dim name As String = DirectCast(gvr.FindControl("Label1"), Label).Text

依旧......

答案 3 :(得分:0)

        <asp:GridView ID="grdResults" CssClass="CommonTable dataTable" AutoGenerateColumns="false"    runat="server">
                                                    <Columns>
                                    <asp:TemplateField HeaderText="Sl#">
                                        <ItemTemplate>
                                            <asp:Label ID="lblSlno" Text='<%# Container.DataItemIndex+1 %>' runat="server"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderText="" ControlStyle-Height="15px" ControlStyle-Width="15px">
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle HorizontalAlign="Left" CssClass="PInfoTd" />
                                        <ItemTemplate>
                                            <asp:ImageButton ID="lknassesno" ToolTip="Edit Assessment" Width="50" CssClass="NewButton" ***CommandName="LINK"***
                                                runat="server" ImageUrl="~/img/Edit.png" />
                                            <asp:HiddenField ID="hidassesmentno" Value='<%#  EVAL("PAN_CODE")%>' runat="server" />
                                            <asp:HiddenField ID="hidPendStatus" Value='<%#  EVAL("Pstatus")%>' runat="server" />
                                            <asp:HiddenField ID="hidIPNO"Value='<%#EVAL("IP_NO")%>' runat="server" />

                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>



**code behind**
    Protected Sub grdResults_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdResults.RowCommand
            If **e.CommandName = "LINK"** Then
                Dim ctrl As Control = e.CommandSource
                Dim grrow As GridViewRow = ctrl.Parent.NamingContainer
                'Dim i As Integer = Convert.ToInt16(e.CommandArgument)
                'Dim lknassesno As HiddenField = DirectCast(e.CommandSource, ImageButton)
                Dim hidAssesmentNo As HiddenField = DirectCast(grdResults.Rows(grrow.RowIndex).FindControl("hidassesmentno"), HiddenField)
                Dim lblstatus As HiddenField = DirectCast(grdResults.Rows(grrow.RowIndex).FindControl("hidPendStatus"), HiddenField)
                Dim hidIpNo As HiddenField = DirectCast(grdResults.Rows(grrow.RowIndex).FindControl("hidIPNO"), HiddenField)
                Dim Assno As String = hidAssesmentNo.Value
                Dim Ipno As String = hidIpNo.Value
                Dim st As String = ""
                If lblstatus.Value = "Pending" Then
                    st = "E"`enter code here`
                ElseIf lblstatus.Value = "Completed" Then
                    st = "V"
                End If
                Response.Redirect("Assessment.aspx?PAssNo=" & Assno & "&Mode=" & st & "&IPNO=" & Ipno & "")
            End If
        End Sub