在服务器端得到CheckBoxList控件最后选择项的方法

探讨这个问题的起因是CSDN论坛的一个帖子:

http://community.csdn.net/Expert/TopicView.asp?id=5460832

 查看客户端生成的HTML代码可以看出,每次点击都执行一次__doPostBack,并且将选择项的参数传递到隐藏表单域之中。 当然,通过脚本和自己的隐藏域可以很轻松得到选择项,但完全在服务器端,也可以得到。

< form  name ="MengXianhui"  method ="post"  action ="a.aspx?ddfg"  id ="MengXianhui" >
< input  type ="hidden"  name ="__EVENTTARGET"  value =""   />
< input  type ="hidden"  name ="__EVENTARGUMENT"  value =""   />
< input  type ="hidden"  name ="__VIEWSTATE"  value ="dDw0NjI1MDk1MzQ7O2w8Q2hlY2tCb3hMaXN0MTowO0NoZWNrQm94TGlzdDE6MTtDaGVja0JveExpc3QxOjI7Q2hlY2tCb3hMaXN0 MTozO0NoZWNrQm94TGlzdDE6NDtDaGVja0JveExpc3QxOjQ7Pj6h9p2gvuU8iLKlA4DU0GtLAoZhCw=="   />

< script  language ="javascript"  type ="text/javascript" >
<!--
    
function  __doPostBack(eventTarget, eventArgument) {
        
var  theform;
        
if   (window.navigator.appName.toLowerCase().indexOf( " microsoft " )   >   - 1 )  {
            theform 
=  document.MengXianhui;
        }
        
else  {
            theform 
=  document.forms[ " MengXianhui " ];
        }
        theform.__EVENTTARGET.value 
=  eventTarget.split( " $ " ).join( " : " );
        theform.__EVENTARGUMENT.value 
=  eventArgument;
        theform.submit();
    }
//  -->
</ script >

  
< div  style ="padding:20px" >
    
< table  id ="CheckBoxList1"  border ="0" >
    
< tr >
        
< td >< input  id ="CheckBoxList1_0"  type ="checkbox"  name ="CheckBoxList1:0"  onclick ="__doPostBack('CheckBoxList1$0','') "  language ="javascript"   />< label  for ="CheckBoxList1_0" > a </ label ></ td >
    
</ tr >< tr >
        
< td >< input  id ="CheckBoxList1_1"  type ="checkbox"  name ="CheckBoxList1:1"  onclick ="__doPostBack('CheckBoxList1$1','')"  language ="javascript"   />< label  for ="CheckBoxList1_1" > b </ label ></ td >
    
</ tr >< tr >
        
< td >< input  id ="CheckBoxList1_2"  type ="checkbox"  name ="CheckBoxList1:2"  checked ="checked"  onclick ="__doPostBack('CheckBoxList1$2','') "  language ="javascript"   />< label  for ="CheckBoxList1_2" > c </ label ></ td >
    
</ tr >< tr >
        
< td >< input  id ="CheckBoxList1_3"  type ="checkbox"  name ="CheckBoxList1:3"  checked ="checked"  onclick ="__doPostBack('CheckBoxList1$3','')"  language ="javascript"   />< label  for ="CheckBoxList1_3" > d </ label ></ td >
    
</ tr >< tr >
        
< td >< input  id ="CheckBoxList1_4"  type ="checkbox"  name ="CheckBoxList1:4"  onclick ="__doPostBack('CheckBoxList1$4','') "  language ="javascript"   />< label  for ="CheckBoxList1_4" > e </ label ></ td >
    
</ tr >
</ table >
    
</ div >
   
  
</ form >

在服务器端得到选择项的做法是分析事件的参数,通过提交过来的事件参数得到。代码如下:

<% @ Page Language = " C# "  AutoEventWireup = " true "  EnableViewState = " false "  Debug = " true "   % >
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd" >
< script  runat ="server" >
void  BoxList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
// 得到触发事件的选择项
string ClickedItem  =  Request.Form[ " __EVENTTARGET " ];
ClickedItem 
=  ClickedItem.Split( ' : ' )[ 1 ];
int  index  =  Int32.Parse(ClickedItem );
Response.Write(CheckBoxList1.Items [index].Text);
}
</ script >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head  runat ="server" >
  
< title > 在服务器端得到CheckBoxList 最好所选择项的方法 </ title >
</ head >
< body >
  
< form  id ="MengXianhui"  runat ="server" >
  
< div  style ="padding:20px" >
    
< asp:CheckBoxList  AutoPostBack ="true"  ID ="CheckBoxList1"  runat ="server"
     OnSelectedIndexChanged
="BoxList1_SelectedIndexChanged" >
    
< asp:ListItem  Value ="1" > a </ asp:ListItem >
    
< asp:ListItem > b </ asp:ListItem >
    
< asp:ListItem > c </ asp:ListItem >
    
< asp:ListItem > d </ asp:ListItem >
    
< asp:ListItem > e </ asp:ListItem >
    
</ asp:CheckBoxList >
    
</ div >    
  
</ form >
</ body >
</ html >

你可能感兴趣的:(JavaScript,server,服务器,Microsoft,input,asp)