我做了两个DropDownList控件,他们是无刷新联动的,但我始终都得不到第二个DropDownList的值
下面的源码:
SelectDeptStaff.aspx
<%@ Page language="c#" Codebehind="SelectDeptStaff.aspx.cs" AutoEventWireup="false" Inherits="REB.web.Sell.SelectDeptStaff" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>SelectDeptStaff</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<SCRIPT LANGUAGE="JavaScript">
    <!--
    //以XML求取数据
    function Go(obj)
    {
      var svalue = obj.value;
      
      var fullObj = document.all("ddlStaff");
      if(svalue=="请选择")
      {
       fullObj.length=0;
      }
      else
      {
       var webFileUrl = "?DeptID=" + svalue;
       var result = "";
       var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
       xmlHttp.open("POST", webFileUrl, false);//使用XMLPOST方式
       xmlHttp.send("");//发送
       result = xmlHttp.responseText;//得到返回的结果
       
       if(result != "" && svalue != "")
       {
         fullObj.length=0;//清空原来存在的
         var piArray = result.split(",");
         for(var i=0;i<piArray.length;i++)
         {
           var ary1 = piArray[i].toString().split("|");                  fullObj.options.add(new Option(ary1[0].toString(),ary1[1].toString())); 
         }
       }
       else
       {
           fullObj.length=0;
         alert("没有员工");
       }
      }
    }
    //-->
</SCRIPT>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="0"
cellPadding="0" width="100%" border="0">
<TR>
<TD><FONT face="宋体">选择业务员</FONT></TD>
</TR>
<TR>
<TD><FONT face="宋体">部门</FONT>
<asp:DropDownList id="ddlDept" runat="server"></asp:DropDownList><FONT face="宋体">员工
<asp:DropDownList id="ddlStaff" runat="server"></asp:DropDownList>
<asp:LinkButton id="LinkButton1" runat="server">确定</asp:LinkButton>
</FONT>
</TD>
</TR>
</TABLE>
</form>
</body>
</HTML>SelectDeptStaff.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using REB.Common;
using REB.DataAccess;namespace REB.web.Sell
{
/// <summary>
/// SelectDeptStaff 的摘要说明。
/// </summary>
public class SelectDeptStaff : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList ddlDept;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.DropDownList ddlStaff;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsClientScriptBlockRegistered("clientScript"))
{
String strScript = "<script>\r\n";
strScript += "function OpenWin(){\r\n";
strScript += "window.parent.returnValue=\r\n";
strScript += "window.parent.close()}\r\n";
strScript += "</script>\r\n";
RegisterClientScriptBlock("clientScript", strScript);
}

            if(!Page.IsPostBack)
{
RefurData();
}
} private void BindDept()
{
REB.DataAccess.DataBase db = new DataBase();
SqlDataReader dr = null; db.RunProc("GetAllDepartment",out dr);
DataTable dt = Tools.ConvertDataReaderToDataTable(dr); ddlDept.DataSource = dt.DefaultView;
ddlDept.DataTextField = "Position_Name";
ddlDept.DataValueField = "Position_ID";
ddlDept.DataBind();
ddlDept.Items.Insert(0,"请选择");
ddlDept.Items[0].Selected=true;
          ddlDept.Attributes.Add("onchange","Go(this);"); } private void BindStaff(string str)
{
string Mystr = ""; int i = Convert.ToInt32(str); REB.DataAccess.DataBase db = new DataBase();
SqlDataReader dr = null; SqlParameter[] prams = {
   db.MakeInParam("@DeptID",SqlDbType.Int,4,i)
   }; db.RunProc("GetSelectStaff",prams,out dr);
DataTable dt = Tools.ConvertDataReaderToDataTable(dr); if(dt.Rows.Count != 0)//判断是否有记录
          {
           for(int ii=0;ii<dt.Rows.Count;ii++)//循环DataTable里的记录
           {
             Mystr += "," + dt.Rows[ii][1].ToString() + "|" + dt.Rows[ii][0].ToString();
   //DataTable每一行都有编号和记录,使用,和|分隔,在XMLPOST中用到
           }
           Mystr = Mystr.Substring(1);
           }
           Response.Write(Mystr);//向HTTP输出内容流写入一个字符数组
           Response.End();//停止该页的执行
} public void RefurData()
{
string DeptID =Request.QueryString["DeptID"];//第一次请求结果为null

          if(DeptID + "Flag" != "Flag")//如果加Flag不等于Flag,表示ddlType改变过,触发过onchange事件
          {
          BindStaff(DeptID);
          }           if(!this.IsPostBack)
          {
          BindDept();
          }
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e); }

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void LinkButton1_Click(object sender, System.EventArgs e)
{
    string deptstaff = ddlDept.SelectedItem.Value;
    string st = ddlStaff.SelectedItem.Selected.ToString();//就是无法得到此值
             Response.Write(deptstaff);
}
}
}

解决方案 »

  1.   

    我在页面上放置两个DropDownList,其ID分别为DropDownList1和DropDownList2利用javascript 实现了无刷新联动,用户提交选择后,在codebehind中我利用this.Request["DropDownList2"]可以获取DropDownList2的选择值,这在一个aspx页面中没有任何问题而当我把此项功能写在一个ascx用户控件中时,问题来了,
    this.Request["DropDownList2"]的值总是为空
    连this.Request["DropDownList1"]的值也为空后来我查看调用该ascx控件的aspx页面的html源文件,
    发现两个DropDownList的id并不是DropDownList1和DropDownList2了,于是改用this.Request[this.DropDownList1.ClientID]和this.Request[this.DropDownList2.ClientID]获取用户选择值,依然为空================================
    解决方式:
    在两个DropDownList傍边又加了一个HtmlInputHidden,其id为myHidden设置为服务器端运行
    然后在Page_Load中做如下设置
    DropDownList2.Attributes.Add("onBlur", ""+this.myHidden.ClientID+".value=this.options[this.selectedIndex].value");
    用this.myHidden.Value便获取到了DropDownList2的用户选择值然后再用this.DropDownList1.SelectedValue 获取DropDownList1的选择值