Link to home
Start Free TrialLog in
Avatar of mousemat24
mousemat24

asked on

Append a list using AJAX

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
Avatar of Randy Wilson
Randy Wilson
Flag of United States of America image

Attach your code...
Avatar of mousemat24
mousemat24

ASKER

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

Do you have your Dropdown in an AJAX Update Panel?  Don't see one in your attached code.
no I dont wrwillson
ASKER CERTIFIED SOLUTION
Avatar of Randy Wilson
Randy Wilson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You STAR wrwillson!!!
That worked.

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

Your are welcome.