Append a list using AJAX

mousemat24
mousemat24 used Ask the Experts™
on
Hi there

Wonder if you can help me, I've using microsoft AJAX. I have a dropdown list with items, next to it, I have a input box with a button (Add). If I enter some text in the inputbox and click on the Add button, I want it to add the text to the dropdown list.

It works, but it does a refresh. Can someone please help me with some code so that I dont get the refresh using AJAX?

Thankyou
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Randy Wilson.Net Developer

Commented:
Attach your code...

Author

Commented:
Thank you so much for replying to me. Here is the code. Please note, my dropdown list is using the CascadingDropDown example from the AJAX toolkit:


<asp:DropDownList ID="drdArea" runat="server" OnSelectedIndexChanged="drdArea_SelectedIndexChanged" AutoPostBack="False" /> 
<asp:TextBox runat="server" ID="txtArea" />
<asp:Button runat="server" ID="Button1" Text="Add" OnClick="btnAreaAdd_Click" />
 
		<ajaxToolkit:CascadingDropDown 
			ID="CascadingDropDown1" 
			runat="server" 
			Category="area"
			TargetControlID="drdArea" 
			PromptText="Select a Curriculum Area"
			LoadingText="Loading Curriculum areas ..." 
			ServiceMethod="GetDropDownCurriculumArea">
		</ajaxToolkit:CascadingDropDown>
 
 
 
 
===========================================
CODE BEHIND FILE:
 
protected void btnAreaAdd_Click(object sender, EventArgs e)
{
      AddCurriculumArea();
}
 
 
public void AddCurriculumArea()
{
      if (IsPostBack)
      {
        string name = txtArea.Text.ToString();
        using (MiddleTierServices.WebService1 ws = new PwC.UK.WebSite.MiddleTierServices.WebService1())
        {
          ws.AddFieldToCurriculumArea(name);
        }
      }
}
 
 
in my webservice I have
 
	[WebMethod]
    public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownCurriculumLoS(string knownCategoryValues, string category)
		{		
			int categoryID;
			StringDictionary categoryValues = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
      categoryID = Convert.ToInt32(categoryValues["area"]);
			SqlConnection sqlConn = new SqlConnection(conString);
			sqlConn.Open();
			SqlCommand sqlSelect = new SqlCommand("SELECT * from books WHERE Fea = @ID", sqlConn);
			sqlSelect.CommandType = System.Data.CommandType.Text;
      sqlSelect.Parameters.Add("@ID", SqlDbType.Int).Value = categoryID;
			SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlSelect);
			DataSet myDataset = new DataSet();
			sqlAdapter.Fill(myDataset);
			sqlConn.Close();
			List<AjaxControlToolkit.CascadingDropDownNameValue> cascadingValues = new List<AjaxControlToolkit.CascadingDropDownNameValue>();
 
			foreach (DataRow dRow in myDataset.Tables[0].Rows)
			{
				string productID = dRow["PK_PD"].ToString();
				string productName = dRow["Na"].ToString();
				cascadingValues.Add(new AjaxControlToolkit.CascadingDropDownNameValue(productName, productID));
			}			
			return cascadingValues.ToArray();
		}

Open in new window

Randy Wilson.Net Developer

Commented:
Do you have your Dropdown in an AJAX Update Panel?  Don't see one in your attached code.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
no I dont wrwillson
.Net Developer
Commented:
Try changing your asp.net code to:

 <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
       
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
           
           <asp:DropDownList ID="drdArea" runat="server" OnSelectedIndexChanged="drdArea_SelectedIndexChanged" AutoPostBack="False" />
            <asp:TextBox runat="server" ID="txtArea" />
            <asp:Button runat="server" ID="Button1" Text="Add" OnClick="btnAreaAdd_Click" />
           
                <ajaxToolkit:CascadingDropDown
                      ID="CascadingDropDown1"
                      runat="server"
                      Category="area"
                      TargetControlID="drdArea"
                      PromptText="Select a Curriculum Area"
                      LoadingText="Loading Curriculum areas ..."
                      ServiceMethod="GetDropDownCurriculumArea">
                </ajaxToolkit:CascadingDropDown>
            
            </ContentTemplate>
        </asp:UpdatePanel>
 

Author

Commented:
You STAR wrwillson!!!
That worked.

Thankyou so much for your help and replying so quickly!!

Randy Wilson.Net Developer

Commented:
Your are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial