如何从DataList ItemTemplate中的标签获取文本? [英] How to get text from a label in a DataList ItemTemplate ?
问题描述
我使用Visual Web Developer 2008 Express.
我在DataList ItemTemplate
中有Label1.我希望使用一个按钮从该标签获取文本,并将其添加到页面其他位置的TextBox1中的文本中.
我知道如何从ASP.NET网页上的标准标签执行此操作,但无法解决如何从DataList ItemTemplate中的标签获取文本的情况.
这是我认为应该做的:-
Hi,
I use Visual Web Developer 2008 Express.
I have Label1 in a DataList ItemTemplate
. I wish to use a button to get the text from that label and add it to text in TextBox1 which is elsewhere on the page.
I know how to do this from a standard label on a ASP.NET web page but cannot work out how to get the text from a label inside a DataList ItemTemplate.
This is what I thought I should do:-
Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
TextBox1.Text = TextBox1.Text & " " & DataList1.ItemTemplates.ItemTemplate.Label2.Text
End Sub
我以为是因为在数据列表模板中选择标签时,属性窗口标题中写的是"DataList1.ItemTemplates.ItemTemplate.Label2
-System.Web.UI.WebControls.Label
".
当我尝试这样做时,我在"DataList1.ItemTemplates
"下看到一个蓝色花体,如果将鼠标悬停在该花体上,则会出现错误框,提示" ItemTemplates"不是"System.Web.UI.WeControls.DataList
"的成员
如何从DataList ItemTemplate
中的标签获取文本?
感谢您提供的任何帮助
I thought that because "DataList1.ItemTemplates.ItemTemplate.Label2
- System.Web.UI.WebControls.Label
" - is what is written in the properties window header when I select the label in the DataList template.
When I try that, I get a Blue Squiggle under "DataList1.ItemTemplates
" and if I hover over the squiggle the error box comes up saying " ''ItemTemplates'' is not a member of ''System.Web.UI.WeControls.DataList
''
How to get text from a label in a DataList ItemTemplate
?
Thanks for any help on offer
推荐答案
而不是使用ItemTemplates
,您可以在按钮的click事件中使用DataListItemCollection.
您可以通过
找到您的标签
Rather then usingItemTemplates
you can use DataListItemCollection in button''s click event.
you can find your label with
DataList1.Items(0).FindControl("Lable2")
Thanks Mehul,
I got it sorted but your method does look like it could be simpler simpler.
The following does work.
VB :
<pre><pre lang="vb">Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.ItemCommand
If e.CommandName = "Item" Then
Dim label1 As Label = DirectCast(DataList1.Items(e.Item.ItemIndex).FindControl("Label1"), Label)
Dim label2 As Label = DirectCast(DataList1.Items(e.Item.ItemIndex).FindControl("Label2"), Label)
TextBox3.Text = TextBox3.Text & vbCrLf & TextBox4.Text & " " & "X" & " " & label2.Text & " " & label1.Text
End If
End Sub</pre>
</pre>
C# :
<pre><pre lang="C++">protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{ if (e.CommandName == "Item")
{
Label label1 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl("Label1");
Label label2 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl("Label2");
TextBox2.Text = TextBox2.Text + label1.Text + " " + label2.Text + Environment.NewLine;
}
}
我很想知道您完整的方法将如何启发我.
< pre lang ="C ++">再次感谢您的努力和建议.</pre>
I would be interested to know how your complete method would be kind enough to enlighten me.
<pre lang="C++">Thanks again for your effort and advice.</pre>
这篇关于如何从DataList ItemTemplate中的标签获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!