js获取asp.net服务器端控件Label,TextBox,RadioButtonList,DropDownList的值

在做 BS架构的项目时,经常遇到 需要用js 来调用 asp.net 服务器端控件的值。

大多数的 控件他的值都可以通过js调用它的 value属性来获得此控件的值,但是也有例外的情况。

经常用的 Label控件。他的值用js就不能通过value属性来获得。

Label控件 js获取的实例, var text= document.getElementById('Label1').innerText;

假如, 这样 var text= document.getElementById('Label1').value; 则 text 为Undefined。

而 TextBox 的值

就可以 var text= document.getElementById('TextBox 1').value;

对于,RadioButtonList 与 DropDownList 他们的获取方式是大不一样的! 这主要是因为 他们所生成的 html元素不一样。

DropDownList 的值 获取 比较简单:

var ddlvalue = document.getElementById('ctl00_Contentplaceholder3_ddlFolws').value;

而 RadioButtonList 的值获取 就比较麻烦:

var value = "";

var Result = document.getElementsByName('ctl00$Contentplaceholder3$rblResult');

for (var i = 0; i < Result.length; i++) {

if (Result.item(i).checked) {

value = Result.item(i).value;

}

}

如果 RadioButtonList 控件 没有一个选择的 那么 value的值 为空!

=======關於substring 用法(字符串截取)======

function SubstringDemo(){

var ss; // 声明变量。

var s = "The rain in Spain falls mainly in the plain..";

ss = s.substring(12, 17); // 取子字符串。

return(ss); // 返回子字符串。

}

========================================

關於寫得一個超爛的學習稿:

protected void Page_Load(object sender, EventArgs e)

{

this.Drop1.Attributes.Add("onblur", "javascript:document.getElementById('TextBox5').value=(this.options[this.selectedIndex].text).concat('FFFF');");

this.TextBox5.Attributes.Add("onfocus", "javascript:this.value=parseInt(document.getElementById('Drop1').value)+parseInt(document.getElementById('Drop2').value)");

this.ListBox1.Attributes.Add("onchange", "javascript:listclick1();return false;");

this.ListBox2.Attributes.Add("onchange", "javascript:listclick2();return false;");

}

--------------

<script language="javascript" type="text/javascript">

function listclick1()

{

var listBox1=document.getElementById("ListBox1");

var listBox2=document.getElementById("ListBox2");

var index1 = listBox1.selectedIndex;

var index2 = listBox2.selectedIndex;

if(index1!=-1 && index2!=-1)

{

var num=parseInt(document.getElementById("listBox1").value)+parseInt(document.getElementById("listBox2").value);

var ss=document.getElementById("TextBox3").value.substring(3,12);

var i = (''+num).length;

while (i++ < 2) num = '0' + num;

num='B'+num+ss;

document.getElementById("LBcode").innerText=num;

}

}

function listclick2()

{

var listBox1=document.getElementById("ListBox1");

var listBox2=document.getElementById("ListBox2");

var index1 = listBox1.selectedIndex;

var index2 = listBox2.selectedIndex;

if(index1!=-1 && index2!=-1)

{

var num=parseInt(document.getElementById("listBox1").value)+parseInt(document.getElementById("listBox2").value);

var ss=document.getElementById("TextBox3").value.substring(3,12);

var i = (''+num).length;

while (i++ < 2) num = '0' + num;

num='B'+num+ss;

document.getElementById("LBcode").innerText=num;

}

}

</script>