惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
月光博客
月光博客
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
博客园 - 【当耐特】
博客园_首页
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
量子位
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
V
Visual Studio Blog
F
Fortinet All Blogs
Martin Fowler
Martin Fowler

博客园 - liufu627

使用option 链式调用来代替异步方法幂调用 Rust 各种情况下如何修改变量 Rust 单链表的实现 Closure move 数值类型与引用类型的区别 rust 使用泛型来完成多态 # Rust异步网络编程 Rust学习 红黑树新解(删除) 红黑树新解(插入) WCF快速創建 ArcGIS安装 泛型OraHelper - liufu627 - 博客园 让SendKeys支持空格键 - liufu627 - 博客园 标点符号的英文说法 Hashtable 让哈希表顺序输出。 PDA datagrid问题 日期格式化 - liufu627 - 博客园 JSON.net的使用 关于C#测试Oracle数据库链接的问题
dropdownlist - liufu627 - 博客园
liufu627 · 2008-09-16 · via 博客园 - liufu627

修改select  的selectedValue或者selectedIndex或者两者,在服务器端还是默认的选择项,想想可能与元素的selected有关,试了一下,果然如此。

aspx代码如此:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
        <asp:DropDownList ID="ddlTest" runat="server">
        <asp:ListItem Text="1" Value="1" Selected="true"></asp:ListItem>
        <asp:ListItem Text="2" Value="1" ></asp:ListItem>
        <asp:ListItem Text="3" Value="1"></asp:ListItem>
        <asp:ListItem Text="4" Value="4"></asp:ListItem>
        <asp:ListItem Text="5" Value="5"></asp:ListItem>
        <asp:ListItem Text="6" Value="6"></asp:ListItem>       
        </asp:DropDownList>
        <script type="text/C#" runat="server">
            private void foo()
            {
                btnReturn.Click += new EventHandler(btnReturn_Click);               
            }
        </script>
        <script type="text/javascript">
        var txt = document.getElementById("<%=txtTest.ClientID %>");
        txt.value = "asdfsdf";
        var ddl =document.getElementById("<%=ddlTest.ClientID %>");
              
       ddlSelByValue( ddl,1,0);
      
        function ddlSelByIndex( ddl, index )
        {
         var selIndex = -1;
         // check
         if( ddl.length < 1 )
         {
            return;
         }
         if( index >= ddl.length )
         {
            return;
         }
        
         for(var i=0;i<ddl.length;i++)
         {
            if( ddl[i].selected == true )
            {
                selIndex  =i;
                break;
            }
         }
        
         if( selIndex != -1 )
         {
            ddl[selIndex].selected = false;
         }        
         ddl[index].selected = true;
        }
      
        function ddlSelByValue( ddl , value ,order)
        {
            // check
            if( ddl == undefined )
            {
                return;
            }
            if( value == undefined)
            {
                return;
            }
            if ( order == undefined )
            {
               order = 0;
            }
            if( order < 0 )
            {
                return;
            }
           
           var selIndex = -1;
           var index4Value =[];
        
         // get current selectedIndex and indexs for match the input value
         for(var i=0;i<ddl.length;i++)
         {
            if( ddl[i].selected == true )
            {
                selIndex  =i;
            }
            if( ddl[i].value == value )
            {
              index4Value.push(i);             
            }
         }
        
         // process
         if( index4Value == undefined || index4Value.length < 1 )
         {
            return;
         }        
         if( order >=  index4Value.length )
         {
            return;
         }        
         if( selIndex != -1 && selIndex == index4Value[order])
         { 
            return;
         }
        
         ddl[ selIndex ].selected = false;        
         ddl[ index4Value[order] ].selected = true;        
        }
        </script>
       
        <asp:Button ID="btnReturn" runat="server" Text="return" OnClick="btnReturn_Click" />
    </div>
    </form>
</body>
</html>
CS代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

 }
 protected void btnReturn_Click(object sender, EventArgs e)
 {
  this.Request.SaveAs("c:\\a.txt", true);
 }
}

打开C:\a.txt,我们将会看到以下内容:

POST /WebSite1/Default.aspx HTTP/1.1
Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 254
Content-Type: application/x-www-form-urlencoded
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-cn
Host: localhost:1230
Referer: http://localhost:1230/WebSite1/Default.aspx
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)

__VIEWSTATE=%2FwEPDwUKLTgxOTUxNjU1N2RkD74GTaa7YkYEXnEtKp8fUvs%2Fo10%3D&txtTest=asdfsdf&ddlTest=6&btnReturn=return&__EVENTVALIDATION=%2FwEWCQK6tuK0AwL9h%2FmcCQKK6%2BCJCAKK6%2BCJCAKK6%2BCJCAKJ6%2BCJCAKO6%2BCJCAKP6%2BCJCAL02tCyAsYjRYHRHKh5VciUq%2FsNDu9bmDaK

__ViewState以前是头部信息,__ViewState及其后面是控件的值,我们可以看到ddlTest=6,6为Value值,如果我们选择第二项或第三项,这个值永远是1,那么在服务器端,ddlTest.SelectedItem永远是第一个。