在Asp.Net中使用DataList控件 [英] Working with DataList control in Asp.Net

查看:43
本文介绍了在Asp.Net中使用DataList控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据列表控件中有一个分隔符模板。在里面,我有一个文本框和一个按钮。

我还有一个交替的项目模板,里面有一个标签。



什么时候我单击分隔符项内的按钮,我希望文本显示在标签中。此外,我想动态创建一个新标签,以便,如果在分隔符模板的文本框中输入任何新文本,它应该显示在新标签中,输入的旧文本也可见。



我面临的问题是我无法使用datalist控件的分隔符模板的文本框值设置标签。我在按钮点击事件中编写此代码。



那么,如何将标签设置为文本框的值?





请帮助!!



谢谢!!

I have a separator template inside a data list control. Inside it, i have a textbox and a button.
I also have an alternating item template and have a label inside it.

When i click the button inside the separator item, i want the text to be shown in the label. Also i want to dynamically create a new label so that, if any new text is entered in the textbox of separator template, it should be shown in the new label, with the old text entered also visible.

The problem i am facing is that I am not able to set label with the textbox value of a separator template of datalist control. I am writing this code inside the button click event.

So, how to set the label to a value of the text box?


Please help!!

Thanks!!

推荐答案

如果需要在服务器端设置标签文本,只需使用属性 System.Web.UI.WebControls.Label.Text

http://msdn.microsoft.com /en-us/library/system.web.ui.webcontrols.label.aspx [ ^ ]。



要在客户端执行此操作,不使用回发,您可以使用JavaScript document.getElementById(id).value =your text;其中 id 是你的 ClientID ,它被翻译成HTML id 属性。请参阅:

http:// msdn .microsoft.com / zh-cn / library / system.web.ui.control.clientid.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx [ ^ ]。



几个代码示例,第二个链接也使用jQuery显示示例:

http://forums.asp.net/t/1116345.aspx [ ^ ],

http://stackoverflow.com/questions/1298695/javascript-set-label-text [ ^ ]。



如果你需要学习jQuery(强烈推荐),请参阅:

http://en.wikipedia.org/wiki/JQuery [ ^ ],

< a href =http://jquery.com/> http://jquery.com [ ^ ],

http://learn.jquery.com [ ^ ],

http://learn.jquery.com/using-jquery-core [ ^ ],

http://learn.jquery.com/about-jquery/how-jquery-works [ ^ ](从这里开始)。



请记住,jQuery在设计时考虑了兼容性。



-SA
If you need to setup the label text on the server side, you just use the property System.Web.UI.WebControls.Label.Text:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.label.aspx[^].

To do this on client side, without a postback, you can use JavaScript document.getElementById(id).value = "your text"; where id is your ClientID which is translated to HTML id attribute. Please see:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx[^].

Couple of code samples, the second link one also shows the sample using jQuery:
http://forums.asp.net/t/1116345.aspx[^],
http://stackoverflow.com/questions/1298695/javascript-set-label-text[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com[^],
http://learn.jquery.com[^],
http://learn.jquery.com/using-jquery-core[^],
http://learn.jquery.com/about-jquery/how-jquery-works[^] (start from here).

Remember that jQuery is designed with compatibility in mind.

—SA


Sehaj,

问题似乎是你无法在ItemCommand事件中找到label1,因为它没有放在Datalist的ItemTemplate中。



根据发布的代码,ItemTemplate和AlternateItem模板中的控件是不同的。 ItemTemplate是负责显示Datalist内部控件的属性,而AlternateItem Template元素用于描述交替行和样式的外观。 (AlternateItemTemplate项目已添加到ItemTemplate中)。添加标签,其中包括Itemtemplate和使用findControl来查找它。



希望这会有所帮助。
Sehaj,
The problem seems to be that you are not able to find the label1 in your ItemCommand event since its not placed inside the ItemTemplate of Datalist.

As per the code posted the controls placed inside ItemTemplate and the AlternateItem Template are different. ItemTemplate is the property which is responsible to Display the Controls inside the Datalist whereas the AlternateItem Template element is for describing the appearance of alternating rows and styling. ( The AlternateItemTemplate items are added those are already in the ItemTemplate). Add the Label which inisde the Itemtemplate and Use findControl to find it.

Hope this helps.


这篇关于在Asp.Net中使用DataList控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆