【问题标题】:how to get item template value on asp datagridview如何在asp datagridview上获取项目模板值
【发布时间】:2013-05-20 07:37:50
【问题描述】:

我得到了这样的数据网格视图:

<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() 值,其他值只是给我空字符串。我的代码有什么问题??

【问题讨论】:

    标签: asp.net vb.net gridview


    【解决方案1】:

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

    即。

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

    那么就可以按照你的预期得到label的值了。

    lblusername.Text = lblNoaccount0.Text
    

    【讨论】:

      【解决方案2】:

      啊,我的坏人

      我得到了它:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 2020-02-16
        • 2017-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多