我有一个网页,我可以动态添加一个总文本框数。我添加了一个HTML表格,我在其中添加行和单元格以放置文本框。
我不希望为每个文本框添加标签控件,但希望在每个文本框中放置水印文本,这些文本框是动态创建的文本框数组。
请帮助我实现这一目标。任何代码都会好得多!
下面的代码是我写的,它只在HTML表格控件中创建了文本框。
//Add row to table
tRow = new TableRow();
tblBokingDet.Rows.Add(tRow);
//Add cell to row
tCell = new TableCell();
tRow.Cells.Add(tCell);
//Add a literal text as control
AddTextBox = new TextBox();
AddTextBox.ID = "txtName" + i;
tCell.Controls.Add(AddTextBox);
答案 0 :(得分:1)
我不知道是否有办法通过动态创建的文本框通过Ajax toolkit连接某些东西,但是“开放”的方式是使用JavaScript将水印附加到每个你的文本框。 (我说“开放”是因为它通常很好地将您的UI功能与您用于可移植性/可重用性的后端框架分开。)
这是jQuery way。
您正在浏览每个文本框的onfocus()和onblur()事件。当然,你需要创建一个足够通用的js函数来迭代你的每个文本框并连接它们的事件。
示例(单个文本框):
<input id="MyTextBox" type="text" value="Search" />
<script>
var MyTextBox = document.getElementById("MyTextBox");
MyTextBox.addEventListener("focus", function(){
if (this.value=='Search') this.value = '';
}, false);
MyTextBox.addEventListener("blur", function(){
if (this.value=='') this.value = 'Search';
}, false);
</script>
答案 1 :(得分:1)
尝试:
AddTextBox = new TextBox();
AddTextBox.ID = "txtName" + i;
tCell.Controls.Add(AddTextBox); // add to the cell
AddTextBox.Attributes.Add("class", "hintTextbox");
AddTextBox.Attributes.Add("onfocus", "if (this.value=='Name') this.value = '';this.style.color = '#000';");
AddTextBox.Attributes.Add("onblur", "if (this.value=='') {this.value = 'Name';this.style.color ='#888';}");
添加CSS:
INPUT.hintTextbox { color: #888; }
INPUT.hintTextboxActive { color: #000; }
请在此处查看示例小提琴:http://jsfiddle.net/F5R6Z/3/