Wednesday, March 28, 2012

Dynamic controls problem.

dear All

I have a checkbox (dynamically generated) and a label.

when the checkbox is changed, I want to change the labels text property. and also put a dynamic DropDownlist box on thr form.

when the Dropdownlist box selected value is changed, I want to again change the value of the labels text to something else. The checkbox behaves properly, but the DropDownList box when selected doesnt do ANYTHING. NOTE: I put the label in a update panel and call the panel.update() method. I put both the .cs and .aspx codes below.

aspx code

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <div> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <ContentTemplate> <asp:PlaceHolder ID="PlaceHolder1" runat="server"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </asp:PlaceHolder> </ContentTemplate> </asp:UpdatePanel> </div> </form></body></html>


aspx.cs code

using System;using System.Data;using System.Configuration;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;public partialclass _Default : System.Web.UI.Page { CheckBox CheckBox1 =new CheckBox(); DropDownList Dropdownlist1 =new DropDownList();protected void Page_Load(object sender, EventArgs e) { Label1.Text ="page has loaded"; addCheckBox(); CheckBox1.AutoPostBack =true; ScriptManager1.RegisterAsyncPostBackControl(CheckBox1);//adddropdownlist(); CheckBox1.CheckedChanged +=new EventHandler(CheckBox1_CheckedChanged); Dropdownlist1.AutoPostBack =true; ScriptManager1.RegisterAsyncPostBackControl(Dropdownlist1); Dropdownlist1.SelectedIndexChanged +=new EventHandler(DropDownList1_SelectedIndexChanged); }private void adddropdownlist() { Dropdownlist1.Items.Add("first"); Dropdownlist1.Items.Add("second"); Dropdownlist1.Items.Add("thirs"); PlaceHolder1.Controls.Add(Dropdownlist1); }private void addCheckBox() { CheckBox1.Text ="tickme"; CheckBox1.ID ="checkbox1"; PlaceHolder1.Controls.Add(CheckBox1); }void CheckBox1_CheckedChanged(object sender, EventArgs e) { Label1.Text ="checkbox has been ticked"; adddropdownlist(); UpdatePanel1.Update(); }void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text ="Page has reloaded"; UpdatePanel1.Update(); }}

Thanks in advance,

prasad..

bumping--please help me.

This doesn't have to do with AJAX (try removing the UpdatePanel to see).

I highly recommend this 4-part series on dynamic controls in ASP.NET:http://devsushi.com/2006/08/27/aspnet-dynamic-control-creation-part-1/

The behavior I see with your code is that when I change the dropdown, the page goes back to its initial state ("page has loaded" and a checkbox). This makes sense, because when you change the dropdown, the page posts back, and in that postback, you don't create the dropdown. That means there's no control to hook up to, and the SelectedIndexChanged event doesn't fire. What you need to do is create the dropdown on every postback where you need to use it.

I recommend adding the dropdown to the placeholder in Page_Loadall the time, and setting Dropdownlist1.Visible = false. Then set Dropdownlist1.Visible = true when you want to display it. That way its events will still hook up properly when it causes a postback.

I hope that makes sense, but if it doesn't, the article I pointed to above should help a great deal. This can be confusing.


thank you for the link to the article. now I realise...what really a postback is. This was my first attempt at programming...:)

thank you,

prasad.

No comments:

Post a Comment