如何从Gridview中的模板字段中获取值

时间:2014-02-21 23:53:47

标签: asp.net gridview

我有一个带有模板字段的gridview。 我想按下图像按钮以获取ItemTemplate中的标签值。

    <asp:GridView ID="GridView1" ShowHeader="False"
        GridLines="None" AutoGenerateColumns="False"
        runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <table style="border: 0px solid">

                        <tr>
                          <td style="width: 220px">
                                <asp:Image ID="imgEmployee" BorderStyle="Solid" Height="182px" Width="235px"
                                    ImageUrl='<%# Eval("Photo")%>'
                                    runat="server" />
                            </td>
                            <td style="width: 600px">
                                <table>
                                    <tr>
                                        <td>
                                            <b>ID:</b>
                                        </td>
                                        <td>
                                            <asp:Label ID="lblId"
                                                runat="server"
                                                Text='<%#Eval("id")%>'>
                                            </asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <b>Item:</b>
                                        </td>
                                        <td>
                                            <asp:Label ID="lblItem"
                                                runat="server"
                                                Text='<%#Eval("Item")%>'>
                                            </asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <b>Price:</b>
                                        </td>
                                        <td>
                                            <asp:Label ID="lblPrice"
                                                runat="server"
                                                Text='<%#Eval("Price")%>'>
                                            </asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <b>Notes:</b>
                                        </td>
                                        <td>
                                            <asp:Label ID="lblNotes"
                                                runat="server"
                                                Text='<%#Eval("Notes")%>'>
                                            </asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:TextBox ID="TextBox1" runat="server" Text ="1" ></asp:TextBox>
                                        </td>
                                        <td>
                                            <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/buy.png" CommandName="AddToBasket" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"  />
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

在GridView1_RowCommand中,我有以下代码

     If e.CommandName = "AddToBasket" Then

这里我想采用标签的值

1 个答案:

答案 0 :(得分:1)

您需要先将OnRowCommand事件方法附加到GridView

<asp:GridView ID="GridView1" ShowHeader="False"
    GridLines="None" AutoGenerateColumns="False"
    OnRowCommand ="GridView1_RowCommand"
    runat="server">

您可以从命令源GridViewRow获取NamingContainer并使用FindControl方法查找Labels,如下所示:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "AddToBasket")
    {
        int id = 0;
        string item = string.Empty; ;
        decimal price = 0.00m;
        string notes = string.Empty;

        var gvRow = ((ImageButton)e.CommandSource).NamingContainer as GridViewRow;
        var lblId = gvRow.FindControl("lblId") as Label;
        var lblItem = gvRow.FindControl("lblId") as Label;
        var lblPrice = gvRow.FindControl("lblId") as Label;
        var lblNotes = gvRow.FindControl("lblId") as Label;

        if (lblId != null && lblItem != null && lblPrice != null && lblNotes != null)
        {
            int.TryParse(lblId.Text, out id);
            item = lblItem.Text;
            decimal.TryParse(lblPrice.Text, out price);
            notes = lblNotes.Text;
        }

    }
}