2013-07-23 48 views
0

我正在创建“编辑我的个人档案页面”,其中我有一些文本框和下拉列表。我有一个要求,当我在第一个下拉列表中选择一个值时,第二个下拉列表应填写。但是这没有发生。部分页面更新使用更新面板

<asp:UpdatePanel runat="server" ID="updatepanel1" UpdateMode="Conditional"> 
         <ContentTemplate > 

          <asp:Label runat="server" Id="lbljobIndus" Text="Preferred Job Industry" Font-Names="Calibri" Font-Size="Small" ForeColor="Black"></asp:Label> 
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black; font-family: Calibri; font-size: small">:</span>&nbsp;&nbsp; 
          <asp:Label runat="server" ID="lblPreferredJobIndustry" Font-Names="Calibri" Font-Size="Small" ForeColor="Black"></asp:Label> 

          <asp:DropDownList ID="tbPreferredJobIndustry" runat="server" Height="19px" OnSelectedIndexChanged="ddlTargetedIndustry_SelectedIndexChanged"> 
           <asp:ListItem Selected="True" Value="-1">--Select Industry--</asp:ListItem> 
           <asp:ListItem Value="1">Administration</asp:ListItem> 
           <asp:ListItem Value="2">Hospital/HealthCare</asp:ListItem> 
           <asp:ListItem Value="3">Medical Transcription</asp:ListItem> 
          </asp:DropDownList> 

          <br /> 
          <br /> 
          <br /> 

          <asp:Label runat="server" ID="lblJobCat" Text="Preferred Job Category" Font-Names="Calibri" Font-Size="Small" ForeColor="Black"></asp:Label> 
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black; font-family: Calibri; font-size: small">:</span>&nbsp;&nbsp; 
          <asp:Label runat="server" ID="lblJobCategory" Font-Names="Calibri" Font-Size="Small" ForeColor="Black"></asp:Label> 
          <asp:DropDownList ID="tbJobCategory" runat="server"> 
           <asp:ListItem Selected="True" Value="-1">-Position Category-</asp:ListItem> 

          </asp:DropDownList> 
          <br /> 
          <br /> 
         </ContentTemplate> 
        </asp:UpdatePanel> 

这是填充第二个下拉列表中的代码: -

protected void ddlTargetedIndustry_SelectedIndexChanged(object sender, EventArgs e) 
    { 

     DataSet ds = new DataSet(); 
     SqlDataAdapter myda = new SqlDataAdapter("Select s_CategoryName,FK_TargetedIndustryID FROM [OfinityJobSearch].[dbo].[tm_JobCategory] where FK_TargetedIndustryID='" + tbPreferredJobIndustry.SelectedItem.Value + "'", con); 
     myda.Fill(ds); 
     tbJobCategory.DataSource = ds; 
     tbJobCategory.DataValueField = "FK_TargetedIndustryID"; 
     tbJobCategory.DataTextField = "s_CategoryName"; 
     tbJobCategory.DataBind(); 
     tbJobCategory.Items.Insert(0, new ListItem("--Select Job Category--", "0")); 
    } 

我使用的更新面板,这样在其他文本框中的值将不会在回来后清除。但现在,我认为回发没有发生。请你检查我的代码并告诉我错误在哪里?

回答

0

设置的AutoPostBack =真为第一DDL

+0

我将它设置为 “真”,但是,它仍然无法正常工作。现在发生的是,当我在第一个ddl中更改我的选择时,两个ddls都消失了! – Tannya

+0

可能存在一些验证错误请尝试CausesValidation ='false'来检查 – Moons

+0

这两个ddls的CausesValidation设置为false。 – Tannya

0

U可以使用AjaxToolkit CascadingDropDown这是非常有益的和有用的。但在CascadingDropDown的情况下,你需要额外的服务。

<ajaxToolkit:CascadingDropDown ID="CDD1" runat="server" 
    TargetControlID="DropDownList2" 
    Category="Model" 
    PromptText="Please select a model" 
    LoadingText="[Loading models...]" 
    ServicePath="CarsService.asmx" 
    ServiceMethod="GetDropDownContents" 
    ParentControlID="DropDownList1" 
    SelectedValue="SomeValue" /> 

UPDATE:

Full example here

你*的.aspx

<asp:DropDownList ID="DropDownList1" runat="server" Width="170" /> 
<asp:DropDownList ID="DropDownList2" runat="server" Width="170" /> 
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1" 
      Category="Make" PromptText="Please select a make" LoadingText="[Loading makes...]" 
      ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents" /> 

and add code to page

[WebMethod] 
    [System.Web.Script.Services.ScriptMethod] 
    public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues, string category) 
    { 
     return new CarsService().GetDropDownContents(knownCategoryValues, category); 
    } 

add web service

<%@ WebService 
    Language="C#" 
    CodeBehind="~/App_Code/CarsService.cs" 
    Class="CarsService" %> 

Code for service

[System.Web.Script.Services.ScriptService] 
public class CarsService : WebService 
{ 
    // 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/CarsService.xml")); 
       } 
      } 
      return _document; 
     } 
    } 

    public static string[] Hierarchy 
    { 
     get { return new string[] { "make", "model" }; } 
    } 

    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); 
    } 
+0

我有页码来填充第二个下拉列表。那么我应该把ServicePath和ServiceMethod设置为NULL吗? – Tannya

+0

我在哪里指定我的方法来填充第二个下拉列表? – Tannya