none
使用<ajaxToolkit:ToolkitScriptManager>后DropDownList的OnSelectedIndexChanged事件无法触发 RRS feed

  • 常规讨论

  • 我是模拟ASP.NET Ajax Control Toolkit的CascadingDropDown写的

     <form id="form1" runat="server">
     <div>
     <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" EnablePartialRendering="true" runat="server">
         <Services>
             <asp:ServiceReference Path="~/WebService.asmx" />
         </Services>
       </ajaxToolkit:ToolkitScriptManager>
         <table>
        <tr>
        <td>省</td>
        <td>
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
        </td>
        </tr>
        <tr>
        <td>市</td>
        <td>
            <asp:DropDownList ID="DropDownList2" runat="server">
            </asp:DropDownList>
        </td>
        </tr>
        <tr>
        <td>县</td>
        <td>
            <asp:DropDownList ID="DropDownList3"  runat="server" AutoPostBack="true"
                OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
            </asp:DropDownList>
        </td>
        </tr>
        </table>
        <br/>
        <div>
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
                Category="province"  PromptText="Please select a Province"  LoadingText="[Loading makes...]"
                ServicePath="~/WebService.asmx" ServiceMethod="GetDropDownContents" />


            <ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
                Category="town" PromptText="Please select a town" LoadingText="[Loading townes...]"
                ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="DropDownList1" />

            <ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"
            Category="county" ServicePath="~/WebService.asmx"  ParentControlID="DropDownList2" PromptText="Please select a County" ServiceMethod="GetDropDownContents" LoadingText="[Loading townes...]">
            </ajaxToolkit:CascadingDropDown>
        </div>
        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" RenderMode="Block" runat="server">
             <ContentTemplate>
            <asp:Label ID="Label1" runat="server"  Text="Label"></asp:Label>
            </ContentTemplate>
            <Triggers>
            <asp:AsyncPostBackTrigger ControlID="DropDownList3" EventName="SelectedIndexChanged" />
            </Triggers>
        </asp:UpdatePanel>
        </div>
        </form>

    页面代码:

     protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            string province = DropDownList1.SelectedItem.Text;
            string town = DropDownList2.SelectedItem.Text;
            string county = DropDownList3.SelectedItem.Text;
            if (string.IsNullOrEmpty(province))
            {
                Label1.Text = "请选择一个省!";
            }
            else if (string.IsNullOrEmpty(town))
            {
                Label1.Text = "请选择一个市!";
            }
            else if (string.IsNullOrEmpty(county))
            {
                Label1.Text = "请选择一个县!";
            }
            else
            {
                Label1.Text = string.Format("你选择了:{0},{1},{2}!",province,town,county);
            }
        }
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues, string category)
        {
            return new WebService().GetDropDownContents(knownCategoryValues, category);
        }

    WebService的代码

    /// <summary>
    ///WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService {

        public WebService()
        {

            //如果使用设计的组件,请取消注释以下行
            //InitializeComponent();
        }
        // Member variables
        private static XmlDocument _document;
        private static Regex _inputValidationRegex;
        private static object _lock = new object();

        // we make these public statics just so we can call them from externally for the
        // page method call
        public static XmlDocument Document
        {
            get
            {
                lock (_lock)
                {
                    if (_document == null)
                    {
                        // Read XML data from disk
                        _document = new XmlDocument();
                        _document.Load(HttpContext.Current.Server.MapPath("~/App_Data/AddressService.xml"));
                    }
                }
                return _document;
            }
        }

        public static string[] Hierarchy
        {
            get { return new string[] { "province", "town" }; }
        }

        public static Regex InputValidationRegex
        {
            get
            {
                lock (_lock)
                {
                    if (null == _inputValidationRegex)
                    {
                        _inputValidationRegex = new Regex("^[0-9a-zA-Z \\(\\)]*$");
                    }
                }
                return _inputValidationRegex;
            }
        }

        /// <summary>
        /// Helper web service method
        /// </summary>
        /// <param name="knownCategoryValues">private storage format string</param>
        /// <param name="category">category of DropDownList to populate</param>
        /// <returns>list of content items</returns>
        [WebMethod]
        public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
        {
            // Get a dictionary of known category/value pairs
            StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

            // Perform a simple query against the data document
            return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category, InputValidationRegex);
        }
        [WebMethod]
        public string HelloWorld() {
            return "Hello World";
        }
        
    }

    App_Data的AddressService.XML文件

    <?xml version="1.0" encoding="utf-8" ?>
    <AddressService>
      <province name="henan" optionTitle="henan">
        <town name="zhumadian" optionTitle="zhumadian">
          <county name="zy" value="zy"/>
          <county name="xc" value="xc"/>
        </town>
        <town name="luoyang" optionTitle="luoyang">
          <county name="yc" value="yc"/>
          <county name="xa" value="xa"/>
        </town>
      </province>
      <province name="hebei" optionTitle="hebei">
        <town name="sjz" optionTitle="sjz">
          <county name="caq" value="caq"/>
          <county name="qdq" value="qdq"/>
        </town>
        <town name="qinhuangdao" optionTitle="qinhuangdao">
          <county name="hgq" value="hgq"/>
          <county name="shgq" value="shgq"/>
        </town>
      </province>
    </AddressService>

    当点击完DropDownList3的选项后,DropDownList3_SelectedIndexChanged不执行,Label1不显示预期内容

                        
    2013年12月24日 5:53

全部回复

  • Hi,

    It seems to be ajax issue. So I suggest that you can post it to asp.net ajax or data Control forum where you can get better answers:

    http://forums.asp.net/1022.aspx or http://forums.asp.net/24.aspx/1?Web+Forms+Data+Controls

    Hope it can help you.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    2013年12月25日 2:17