Hi all
I have a form view which uses a SQL data source control to retrieve it's data from the sql express database
the form view is used for view,edit,delete and insert data into the database
in the insert mode I have two dropdownlists, where the second one is depending on the first one to retrieve the correct data from the data baseBUT
when selecting a value in the first dropdownlist it give me the following erro:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Please How can do this
any help is appriciated
bye
You cannt do this using this method. There are many work arrounds out there, but the easiest I believe is the CascadingDropDown control which is part of the AJAX.NET Control Toolkit.
http://www.asp.net/ajax/ -- Install the AJAX.NET FRAMEWORK AND configure your site to use it.
You will need to:
1.) First Create a Web Service Class like as followed:
using System;using System.Web;using System.Collections;using System.Configuration;using System.Web.Services;using System.Web.Services.Protocols;using System.Collections.Generic;using AjaxControlToolkit;using System.Data;using System.Data.SqlClient;using System.Web.Script.Services;/// <summary>/// Cascading DropDownList AjaxControlToolkit Web Service/// </summary>[WebService(Namespace ="http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.Web.Script.Services.ScriptService]public class DataService : System.Web.Services.WebService { private string connectionString =ConfigurationManager.ConnectionStrings["CarrierDBConnectionString"].ConnectionString; public DataService () {} [WebMethod] [System.Web.Script.Services.ScriptMethod()] public CascadingDropDownNameValue[] GetCarriers(string knownCategoryValues, string category) { List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand("CARRIER_SELECT_ALL", conn)) { cmd.CommandType = CommandType.StoredProcedure; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (dr.Read()) { values.Add(new CascadingDropDownNameValue((string)dr["NAME"], dr["ID"].ToString())); } } } return values.ToArray(); } [WebMethod] [System.Web.Script.Services.ScriptMethod()] public CascadingDropDownNameValue[] GetSubsidiariesByCarrierID(string knownCategoryValues, string category) { List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand("CARRIER_SELECT_SUBSIDIARY_BYID", conn)) { string[] categoryValues = knownCategoryValues.Split(':', ';'); int id = Convert.ToInt32(categoryValues[1]); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@.id",id); conn.Open(); using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { if (dr.HasRows) { while (dr.Read()) { values.Add(new CascadingDropDownNameValue((string)dr["NAME"], dr["ID"].ToString())); } } } } }return values.ToArray(); }}
2.) Create the Actual Web Service
Click on the Project in the 'Solution Explorer' and 'Add New Item' and then select Web Service (for instance in this example: DataService.asmx)
3.) Hook up your ajax controls and dropdownlist
<asp:UpdatePanel ID="updatePnlInsert" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlCarrierInsert" runat="server" AutoPostBack="True" Width="100%" DataSourceID="sqldsCarriersInsert" DataTextField="NAME" DataValueField="ID" SelectedValue='<%# Eval("CARRIER_ID") %>' AppendDataBoundItems="true">
<asp:ListItem Value="" Text="<select carrier>" />
</asp:DropDownList>
<br />
<asp:SqlDataSource ID="sqldsCarriersInsert" runat="server" ConnectionString="<%$ ConnectionStrings:CarrierDBConnectionString %>"
SelectCommand="CARRIER_SELECT_ALL" SelectCommandType="StoredProcedure" />
<asp:DropDownList ID="ddlSubInsert" runat="server" Width="100%" /><br />
<cc1:CascadingDropDown ID="cddCarrierInsert" runat="server"
TargetControlID="ddlSubInsert"
ParentControlID="ddlCarrierInsert"
Category="CARRIER"
LoadingText="[Loading Subsidiaries...]"
PromptText="<select subsidary>"
ServiceMethod="GetSubsidiariesByCarrierID"
ServicePath="DataService.asmx"
SelectedValue='<%# Eval("SUBSIDIARY_ID") %>'>
</cc1:CascadingDropDown>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCarrierInsert" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Hope this helps.
No comments:
Post a Comment