使用更新面板更新部分页面

时间:2013-07-23 07:15:16

标签: html asp.net c#-4.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"));
    }

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

2 个答案:

答案 0 :(得分:0)

为第一个ddl

设置AutoPostBack = true

答案 1 :(得分:0)

你可以使用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" />

<强>更新

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