我在updatepanel中有一个动态添加的下拉回发,它总是会导致回发。
我已将ddl构建移至page_init,也试过给ddl一个id。我尝试使更新面板有条件并在触发ddl选择后发出更新。
有什么想法吗?
<body>
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server" Height="309px">
<div id="ctldiv">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </asp:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Panel Created"></asp:Label><br />
<asp:Button ID="Button1" runat="server" Text="Refresh Panel 1" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Panel>
</form>
</body>
背后的代码
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
buildddl();
}
protected void message(string message)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "dspmsg('" + message + "');", true);
}
private void buildddl()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("col1", typeof(string)));
dt.Columns.Add(new DataColumn("col2", typeof(string)));
dr = dt.NewRow();
dr["col1"] = "1";
dr["col2"] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["col1"] = "2";
dr["col2"] = "2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["col1"] = "3";
dr["col2"] = "3";
dt.Rows.Add(dr);
DropDownList ddl = new DropDownList();
ddl.DataTextField = "col1";
ddl.DataValueField = "col2";
ddl.DataSource = dt;
ddl.DataBind();
ddl.AutoPostBack = true;
ddl.ID = "ddl1";
ddl.SelectedIndexChanged += new EventHandler(ddl_select);
Panel1.Controls.Add(ddl);
}
protected void ddl_select(Object sender, EventArgs e)
{
DropDownList ComboBox = (DropDownList)sender;
Label1.Text = "selected :" + ComboBox.SelectedItem;
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Panel refreshed at " +
DateTime.Now.ToString();
}
}
答案 0 :(得分:0)
把
UpdatePanel1.Triggers.Add(new AsyncPostBackTrigger() { ControlID = "ddl1" });
将下拉列表添加到控制面板(行Panel1.Controls.Add(ddl);
之后)