locked
访问客户端Option的问题 RRS feed

  • 问题

  • 大家好:

    有一个列表控件<select id="slt" runat="server"></select>,
    我在客户端动态的给它添加了一些<option>...</option>,
    请问我怎么才能在服务器端获取到这些添加的<option>?

    我尝试过在服务器端用slt.Items来访问,但是取得的集合是空的。
    然后我想是不是需要在添加<option>的时候同时添加runat="server"属性,但是添加失败。
    2009年9月10日 1:53

答案

  • 简单的方法是   把  id="slt".outerHTML 放在一个 hidden input中
    恭喜自己5星用户达成
    2009年9月10日 3:05
  • <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
      EnableViewState="false" %>
    
    <!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 id="Head1" runat="server">
      <title>Scan </title>
    
    <script type="text/javascript">
      var i = 0;
      function Add() {
        s = document.getElementById("slt")
        s.options[s.options.length] = new Option(++i, i);
      }
    
      function getData() {
        s = [];
        for (i = 0; i < document.getElementById("slt").options.length; i++) {
          s.push(document.getElementById("slt").options[i].value);
        }
        document.getElementById("h").value = s;
      }
    </script>
    
    </head>
    <body onkeypress="return EnterTextBox(event)">
      <form id="form1" runat="server" defaultbutton="btnSubmit">
      <select id="slt" runat="server">
      <option value="aa">aaaa</option>
      </select>
      <input onclick="Add()" type="button"  value="添加选项" />
      <input id="h" type="hidden" runat="server" />
      <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" OnClientClick="getData()">
      </asp:Button>
      </form>
    </body>
    </html>
    
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;
    using System.Web;
    using System.IO;
    using System.Xml;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page
    {
      protected void btnSubmit_Click(object sender, EventArgs e)
      {
        Response.Write(h.Value);
    
      }
    }
    

    【孟子E章】
    2009年9月10日 9:32
    版主

全部回复

  • 动态添加的是不能保留状态的。一般只是在服务器端得到选中的值,可以使用 Request.Form[slt.UniqueID]获取
    【孟子E章】
    2009年9月10日 2:03
    版主
  • 要得到所有option,你可以在提交之前,使用js将遍历一下所有option,然后存在隐藏的输入框内
    【孟子E章】
    2009年9月10日 2:04
    版主
  • 简单的方法是   把  id="slt".outerHTML 放在一个 hidden input中
    恭喜自己5星用户达成
    2009年9月10日 3:05
  • 我把值放在<input type="hidden" id="hid" runat="server">中,但是在服务器端我用hid.Value取不到值,是取值方法有问题么?

    2009年9月10日 8:33
  • <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
      EnableViewState="false" %>
    
    <!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 id="Head1" runat="server">
      <title>Scan </title>
    
    <script type="text/javascript">
      var i = 0;
      function Add() {
        s = document.getElementById("slt")
        s.options[s.options.length] = new Option(++i, i);
      }
    
      function getData() {
        s = [];
        for (i = 0; i < document.getElementById("slt").options.length; i++) {
          s.push(document.getElementById("slt").options[i].value);
        }
        document.getElementById("h").value = s;
      }
    </script>
    
    </head>
    <body onkeypress="return EnterTextBox(event)">
      <form id="form1" runat="server" defaultbutton="btnSubmit">
      <select id="slt" runat="server">
      <option value="aa">aaaa</option>
      </select>
      <input onclick="Add()" type="button"  value="添加选项" />
      <input id="h" type="hidden" runat="server" />
      <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" OnClientClick="getData()">
      </asp:Button>
      </form>
    </body>
    </html>
    
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;
    using System.Web;
    using System.IO;
    using System.Xml;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page
    {
      protected void btnSubmit_Click(object sender, EventArgs e)
      {
        Response.Write(h.Value);
    
      }
    }
    

    【孟子E章】
    2009年9月10日 9:32
    版主