如何在asp datagridview上获取项模板值

时间:2013-05-20 07:37:51

标签: asp.net vb.net gridview

我得到了像这样的datagridview:

<asp:GridView ID="gvData" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                            CellPadding="4" DataKeyNames="InquiryId" Font-Size="11px" ForeColor="Black" PageSize="20"
                            Width="100%" Style="text-align: center" CssClass="zebra" AlternatingRowStyle-CssClass="even">
                            <AlternatingRowStyle CssClass="even" />
                            <Columns>
                                <asp:TemplateField>
                                <ItemStyle HorizontalAlign="Center" Width="100px" />
                                    <FooterStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                       <asp:ImageButton ID="imgbtn" ImageUrl="~/Edit.jpg" runat="server" Width="25" Height="25" onclick="imgbtn_Click" />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="No Account">
                                    <ItemStyle HorizontalAlign="Center" Width="100px" />
                                    <FooterStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="lblNoaccount0" runat="server" Text='<%# Bind("InquiryAccount") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Language">
                                    <ItemStyle HorizontalAlign="center" Width="50px" />
                                    <FooterStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="lblSlotTime" runat="server" Text='<%# Bind("InquiryLang") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Amount">
                                    <ItemStyle HorizontalAlign="Center" Width="20px" />
                                    <FooterStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <asp:Label ID="lblInquiryAmount" runat="server" Text='<%# Bind("InquiryAmount") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Response">
                                    <ItemStyle HorizontalAlign="Center" Width="70px" />
                                    <FooterStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:Label ID="lblResponse" runat="server" Text='<%# Bind("InquiryResponse") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>

                            </Columns>

                        </asp:GridView>

我希望通过单击图像按钮获取行值,这是代码:

 Protected Sub imgbtn_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        Dim btndetails As ImageButton = TryCast(sender, ImageButton)
        Dim gvrow As GridViewRow = DirectCast(btndetails.NamingContainer, GridViewRow)
        lblID.Text = gvData.DataKeys(gvrow.RowIndex).Value.ToString()
        lblusername.Text = gvrow.Cells(1).Text
        txtfname.Text = gvrow.Cells(2).Text
        txtlname.Text = gvrow.Cells(3).Text
        txtCity.Text = gvrow.Cells(4).Text
        txtDesg.Text = gvrow.Cells(5).Text
        Me.pnlGrid_ModalPopupExtender.Show()
    End Sub

但是当我调试它时,我只得到gvData.DataKeys(gvrow.RowIndex).Value.ToString()值,其他值只给我空字符串。我的代码怎么了?

2 个答案:

答案 0 :(得分:2)

您可以使用FindControl方法查找模板中定义的控件。

Dim lblNoaccount0 As Label = DirectCast(gvrow.FindControl("lblNoaccount0"), Label)

然后您可以按预期获得标签的值。

lblusername.Text = lblNoaccount0.Text

答案 1 :(得分:0)

啊,我的坏

我得到了:

  Dim lblAmount As Label = DirectCast(gvrow.Cells(1).FindControl("lblNoaccount0"), Label)
        lblusername.Text = lblAmount.Text