这是我第一次使用Ajax,这也是我的第一个C#.NET项目,所以我很新。我使用的是.NET 4.0。
我已经成功实现了Ajax级联下拉列表,并在提交后,数据存储在数据库中。但是,此页面还具有“编辑”功能,这意味着如果数据库中已填充所述下拉列表的值,则应“显示”,并允许用户更改它。这是我被卡住的地方。如果db中已存在这些下拉列表的值,我该如何显示?
我对其他非Ajax字段有一个try / catch,我已经尝试过这些,但无济于事。我也会列出那个。
代码背后:
public partial class Research : System.Web.UI.Page
{
//Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
/Create data table to populate fields with values from the selected record.
DataTable dt = new DataTable();
dt = selectDetails();
//Call Try/Catch Blocks to load fields.
tryRootCauseCategoryDD(cboRootCauseCategory, dt.Rows[0]["rootCauseCategory"].ToString());
tryRootCauseDD(cboRootCause, dt.Rows[0]["rootCause"].ToString());
}
}
protected void tryRootCauseCategoryDD(DropDownList cboRootCauseCategory, string ddSelected)
{
try
{
cboRootCauseCategory.SelectedValue = ddSelected;
}
catch
{
cboRootCauseCategory.SelectedIndex = 0;
}
}
protected void tryRootCauseDD(DropDownList cboRootCause, string ddSelected)
{
try
{
cboRootCause.SelectedValue = ddSelected;
}
catch
{
cboRootCause.SelectedIndex = 0;
}
}
protected DataTable selectDetails()
{
DataTable dt = new DataTable();
dt = dataAccess.ExecuteDataTable
(
"spRecordDetails", dataAccess.DEV, new SqlParameter[1]
{
new SqlParameter ("@vRecID", Request.QueryString["recID"].ToString())
}
);
return dt;
}
ASPX:
<asp:DropDownList ID="cboRootCauseCategory" runat="server"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="ccdRootCauseCategory" runat="server" Category="RootCauseCategory"
TargetControlID="cboRootCauseCategory" PromptText="(Please select:)" LoadingText="Loading.."
ServiceMethod="BindRootCauseCategoryDetails" ServicePath="CascadingDropDown.asmx">
</ajaxToolkit:CascadingDropDown>
<asp:DropDownList ID="cboRootCause" runat="server"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="ccdRootCause" runat="server" Category="RootCause" ParentControlID="cboRootCauseCategory"
TargetControlID="cboRootCause" PromptText="(Please select:)" LoadingText="Loading.."
ServiceMethod="BindRootCauseDetails" ServicePath="CascadingDropDown.asmx">
</ajaxToolkit:CascadingDropDown>
网络服务:
[WebService(Namespace = "http://microsoft.com/webservices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//To allow this Web Service to be called from script, using ASP.NET AJAX.
[System.Web.Script.Services.ScriptService()]
public class CascadingDropDown : System.Web.Services.WebService
{
//Database connection string
//private static string strconnection = ConfigurationManager.AppSettings["DEV"].ToString();
private static string strconnection = System.Configuration.ConfigurationManager.ConnectionStrings["DEV"].ConnectionString;
//database connection
SqlConnection conCategory = new SqlConnection(strconnection);
public CascadingDropDown()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
/// WebMethod to Populate Root Cause Category Dropdown
[WebMethod]
public CascadingDropDownNameValue[] BindRootCauseCategoryDetails(string knownCategoryValues, string category)
{
conCategory.Open();
SqlCommand cmdRootCauseCategory = new SqlCommand
("Select Distinct RootCauseCategory From RootCause", conCategory);
cmdRootCauseCategory.ExecuteNonQuery();
SqlDataAdapter daRootCauseCategory = new SqlDataAdapter(cmdRootCauseCategory);
DataSet dsRootCauseCategory = new DataSet();
daRootCauseCategory.Fill(dsRootCauseCategory);
conCategory.Close();
//create list and add items in it by looping through dataset table
List<CascadingDropDownNameValue> RootCauseCategoryDetails = new List<CascadingDropDownNameValue>();
foreach (DataRow dtrow in dsRootCauseCategory.Tables[0].Rows)
{
//string recID = dtrow["recID"].ToString();
string RootCauseCategory = dtrow["RootCauseCategory"].ToString();
string RootCauseCategoryValue = dtrow["RootCauseCategory"].ToString();
RootCauseCategoryDetails.Add(new CascadingDropDownNameValue(RootCauseCategory,RootCauseCategoryValue));
}
return RootCauseCategoryDetails.ToArray();
}
/// WebMethod to Populate Root Cause Dropdown
[WebMethod]
public CascadingDropDownNameValue[] BindRootCauseDetails(string knownCategoryValues, string category)
{
string rootCauseCategory;
//This method will return a StringDictionary containing the name/value pairs of the currently selected values
StringDictionary rootCauseCategoryDetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
rootCauseCategory = (rootCauseCategoryDetails["RootCauseCategory"]);
conCategory.Open();
SqlCommand cmdRootCause = new SqlCommand("select recID, rootCause from RootCause where rootCauseCategory= @vRootCauseCategory", conCategory);
cmdRootCause.Parameters.AddWithValue("@vRootCauseCategory", rootCauseCategory);
cmdRootCause.ExecuteNonQuery();
SqlDataAdapter daRootCause = new SqlDataAdapter(cmdRootCause);
DataSet dsRootCause = new DataSet();
daRootCause.Fill(dsRootCause);
conCategory.Close();
//create list and add items in it by looping through dataset table
List<CascadingDropDownNameValue> rootCauseDetails = new List<CascadingDropDownNameValue>();
foreach (DataRow dtrow in dsRootCause.Tables[0].Rows)
{
string recID = dtrow["recID"].ToString();
string rootCause = dtrow["rootCause"].ToString();
rootCauseDetails.Add(new CascadingDropDownNameValue(rootCause, recID));
}
return rootCauseDetails.ToArray();
}
}
答案 0 :(得分:0)
我决定废弃网络服务的想法,而是选择了更新面板。可能没有多余的方法可以做到这一点,但它正在发挥作用。
最终解决方案:
标记页:
<asp:TableCell Width="500">
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<p><asp:DropDownList ID="cboRootCauseCategory" runat="server" AutoPostBack="True" onselectedindexchanged="cboRootCauseCategory_SelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList ID="cboRootCause" runat="server" AutoPostBack="true"></asp:DropDownList></p>
</ContentTemplate>
</asp:UpdatePanel>
</asp:TableCell>
代码背后:
if (!IsPostBack)
{
//Create data table to populate fields with values from the selected record.
DataTable dt = new DataTable();
dt = selectDetails();
//Call Try/Catch Blocks to load fields.
tryRootCauseCategoryDD(cboRootCauseCategory, dt.Rows[0]["rootCauseCategory"].ToString());
tryRootCauseDD(cboRootCause, dt.Rows[0]["rootCause"].ToString());
}
protected void tryRootCauseCategoryDD(DropDownList cboRootCauseCategory, string ddSelected)
{
try
{
//Load Root Cause Category Drop Down
DataTable RootCauseCategories = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DEV"].ConnectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("Select recID, rootCauseCategory From RootCauseCategory", con);
adapter.Fill(RootCauseCategories);
cboRootCauseCategory.DataSource = RootCauseCategories;
cboRootCauseCategory.DataTextField = "RootCauseCategory";
cboRootCauseCategory.DataValueField = "recID";
cboRootCauseCategory.DataBind();
}
cboRootCauseCategory.Items.Insert(0, new ListItem("(Please select:)", "0"));
cboRootCauseCategory.SelectedValue = ddSelected;
}
catch
{
//Load Root Cause Category Drop Down
DataTable RootCauseCategories = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DEV"].ConnectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("Select recID, rootCauseCategory From RootCauseCategory", con);
adapter.Fill(RootCauseCategories);
cboRootCauseCategory.DataSource = RootCauseCategories;
cboRootCauseCategory.DataTextField = "RootCauseCategory";
cboRootCauseCategory.DataValueField = "recID";
cboRootCauseCategory.DataBind();
}
cboRootCauseCategory.Items.Insert(0, new ListItem("(Please select:)", "0"));
cboRootCauseCategory.SelectedIndex = 0;
}
}
protected void tryRootCauseDD(DropDownList cboRootCause, string ddSelected)
{
try
{
string cboRootCauseCategoryID = Convert.ToString(cboRootCauseCategory.SelectedValue);
DataTable RootCauses = new DataTable();
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["DEV"].ConnectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("Select recID, rootCause, rootCauseCategory From RootCause Where rootCauseCategory = '" + cboRootCauseCategoryID + "'", con2);
adapter.Fill(RootCauses);
cboRootCause.DataSource = RootCauses;
cboRootCause.DataTextField = "RootCause";
cboRootCause.DataValueField = "recID";
cboRootCause.DataBind();
}
cboRootCause.SelectedValue = ddSelected;
}
catch
{
cboRootCause.SelectedIndex = 0;
}
}
protected void cboRootCauseCategory_SelectedIndexChanged(object sender, EventArgs e)
{
string cboRootCauseCategoryID = Convert.ToString(cboRootCauseCategory.SelectedValue);
DataTable RootCauses = new DataTable();
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["DEV"].ConnectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("Select recID, rootCause, rootCauseCategory From RootCause Where rootCauseCategory = '" + cboRootCauseCategoryID + "'", con2);
adapter.Fill(RootCauses);
cboRootCause.DataSource = RootCauses;
cboRootCause.DataTextField = "RootCause";
cboRootCause.DataValueField = "recID";
cboRootCause.DataBind();
}
cboRootCause.Items.Insert(0, new ListItem("(Please select:)", "0"));