vb.net 如何在asp datagridview上获取项目模板值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16644737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get item template value on asp datagridview
提问by Sabilv
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??
但是当我调试它时,我只得到gvData.DataKeys(gvrow.RowIndex).Value.ToString()值,其他值只是给我空字符串。我的代码有什么问题??
回答by wy__
You can use the FindControlmethod to find the control that defined in a template.
您可以使用FindControl方法查找在模板中定义的控件。
ie.
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
回答by Sabilv
ahhh , my Bad
啊,我的坏
i got it by :
我是通过以下方式得到的:
Dim lblAmount As Label = DirectCast(gvrow.Cells(1).FindControl("lblNoaccount0"), Label)
lblusername.Text = lblAmount.Text

