在后台动态创建CascadingDropDown以便实现级联效果,但提交后dropdownlist值无法取得,被刷新清空,怎么回事,请高手赐教,不胜感激!
代码如下:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" EnableEventValidation="false" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:DropDownList id="drpkehubie" runat="server">
</asp:DropDownList>
<cc1:CascadingDropDown ID="caskehubie" runat="server"
Category="kehubie" ServicePath="~/AjaxControl/Ajaxcas.asmx"
ServiceMethod="bindkehubieerp" TargetControlID="drpkehubie"
LoadingText="正在加载客户" PromptText="客户别" >
</cc1:CascadingDropDown>
<asp:DropDownList id="drpjizhong" runat="server">
</asp:DropDownList>
<cc1:CascadingDropDown ID="casjizhong" runat="server"
Category="jizhong" ServicePath="~/AjaxControl/Ajaxcas.asmx"
ServiceMethod="bindjizhongerp" TargetControlID="drpjizhong" ParentControlID="drpkehubie"
LoadingText="正在加载机种" PromptText="机种"
></cc1:CascadingDropDown>
<asp:Button ID="btndaochu" runat="server" OnClick="btndaochu_Click" Text="导出" />
<asp:Panel ID="Panel1" runat="server" Width="753px">
</asp:Panel>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using AjaxControlToolkit;
public partial class Default3 : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
if (!IsPostBack)
{
}
create();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btndaochu_Click(object sender, EventArgs e)
{
if (ViewState["CreateControl"] == null)
{
ViewState["CreateControl"] = true;
create();
}
}
public void create()
{
if (ViewState["CreateControl"] == null)
{
return;
}
DropDownList drp; TextBox txt;
CascadingDropDown cas;
Panel pan = this.Panel1;
#region 取得料号 品名 用量
txt = new TextBox();
txt.ID = "aa";
pan.Controls.Add(txt);
AutoCompleteExtender auto = new AutoCompleteExtender();
auto.ID = "auto";
auto.ServiceMethod="GetGangmohao";
auto.ServicePath="~/AjaxControl/AjaxAutoCom.asmx";
auto.TargetControlID="aa";
auto.MinimumPrefixLength=1;
auto.CompletionInterval=100;
auto.CompletionSetCount = 30;
pan.Controls.Add(auto);
drp = new DropDownList();
drp.ID = "drpliaohao";
drp.Width = 140;
pan.Controls.Add(drp);
cas = new CascadingDropDown();
cas.ID = "casliaohao";
cas.Category = "liaohao";
cas.ServicePath = "~/AjaxControl/Ajaxcas.asmx";
cas.ServiceMethod = "bindliaohaoerp";
cas.TargetControlID = "drpliaohao";
cas.ParentControlID = "drpjizhong";
cas.LoadingText = "正在加载料号";
cas.PromptText = "料号";
pan.Controls.Add(cas);
drp = new DropDownList();
drp.ID = "drppinming";
drp.Width = 100;
pan.Controls.Add(drp);
cas = new CascadingDropDown();
cas.ID = "caspinming";
cas.Category = "pinming";
cas.ServicePath = "~/AjaxControl/Ajaxcas.asmx";
cas.ServiceMethod = "bindzwpinmingerp";
cas.TargetControlID = "drppinming" ;
cas.ParentControlID = "drpliaohao";
cas.LoadingText = "正在加载品名";
cas.PromptText = "品名";
pan.Controls.Add(cas);
#endregion
}
protected void Button1_Click(object sender, EventArgs e)
{
string liaohao, pinming;
liaohao = pinming = "";
if ((this.Panel1.FindControl("drpliaohao") as DropDownList).Items.Count > 0)
{
liaohao = (this.Panel1.FindControl("drpliaohao") as DropDownList).SelectedItem.Text;
}
if ((this.Panel1.FindControl("drppinming") as DropDownList).Items.Count > 0)
{
pinming = (this.Panel1.FindControl("drppinming") as DropDownList).SelectedItem.Text;
}
string cnt = "料号:"+liaohao+(this.Panel1.FindControl("drpliaohao") as DropDownList).Items.Count +"品名:"+pinming+ (this.Panel1.FindControl("drppinming") as DropDownList).Items.Count;
cnt += (this.Panel1.FindControl("aa") as TextBox).Text;
this.Label1.Text = cnt;
}
}