Sabilv
Sabilv

Reputation: 600

how to get item template value on asp datagridview

i got datagridview like this :

<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>

and i want to get row value by clicking the image button, here's the code :

 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

but when i debug it, i only get the gvData.DataKeys(gvrow.RowIndex).Value.ToString() value, the others value just give me the empty string. whats wrong with my code??

Upvotes: 1

Views: 9991

Answers (2)

wy__
wy__

Reputation: 301

You can use the FindControl method to find the control that defined in a template.

ie.

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

Then you can get the value of the label as you expected.

lblusername.Text = lblNoaccount0.Text

Upvotes: 2

Sabilv
Sabilv

Reputation: 600

ahhh , my Bad

i got it by :

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

Upvotes: 0

Related Questions