Hi,

This is the requirement. I am trying to create the controls dynamically based on the values in the Database.

I am able to create the controls successfully. I have a repeater control in the ASPX page.

On the Header Template, Item Template and Footer Template, call a class Template.
On Databinding of this Repeater control, calls a method inside the class Template.

This method creates the controls dynamically, & adds them to the place holder.

In creating the dynamic controls, there are dropdown list controls. Based on the value in the Dropdown, I wanted to hide certain controls.

For which I need to find the controls & their ids. This should happen as soon as the controls are created in the page.

I am also able to find the ID's but each time on loading of the page, the InstantiateIn method will be called and binded to the Repeater Control.

but, i wanted to trace the Dropdown Selected Index Changed event of a dropdaown..But the ID is not getiing changed as it is loaded everytime, the Selected Index is not gettin changed....

I tried the is Not post backed.. In this scenario, the controls are not loaded at all..

Help me out...



private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

try
{
dsQuestions = objDataAccessRead.get_questionstest();
Repeater1.HeaderTemplate = new Template(ListItemType.Header);
Repeater1.ItemTemplate = new Template(ListItemType.Item);
Repeater1.FooterTemplate = new Template(ListItemType.Footer);
Repeater1.DataSource = dsQuestions;
Repeater1.DataBind();

}
catch(Exception ex3)
{
Response.Write(ex3.Message);

}
finally
{

}

}


public class Template : System.Web.UI.ITemplate
{
System.Web.UI.WebControls.ListItemType templateType;

public Template(System.Web.UI.WebControls.ListItemType type)
{
templateType = type;
}

public void InstantiateIn(System.Web.UI.Control container)
{
PlaceHolder ph = new PlaceHolder();
PAR.DataAccessRead.PARDataAccessRead objDR = new PAR.DataAccessRead.PARDataAccessRead();
DataSet dsQuestions = new DataSet();
DataSet dsResponse = new DataSet();
DataRow dr;
string strResponseType;
int strResponseID;
int strQuestionID;

try
{
switch (templateType)
{
case ListItemType.Header:
ph.Controls.Add(new LiteralControl("<table border=0 width='100%' cellpadding='2px' cellspacing='5px' class='maintable'>" +
"<tr><td class='styleHeading'><b>quest</b></td></tr>"));
break;
case ListItemType.Item:
dsQuestions = objDR.get_questions();
if (dsQuestions.Tables[0].Rows.Count >0)
{
for(int intqc=0;intqc < dsQuestions.Tables[0].Rows.Count;intqc++)
{
Label lbl= new Label();
lbl.ID = dsQuestions.Tables[0].Rows[intqc]["QUESTION_ID"].ToString();
lbl.Text = dsQuestions.Tables[0].Rows[intqc]["QUESTIONTEXTPROMPT"].ToString();
lbl.CssClass ="stylefont";
ph.Controls.Add(new LiteralControl("<tr><td>"));
ph.Controls.Add(lbl);
ph.Controls.Add(new LiteralControl("</td></tr>"));

strQuestionID=Convert.ToInt32(lbl.ID);

dsResponse = objDR.get_Responses(strQuestionID);

if (dsResponse.Tables[0].Rows.Count >0)
{
DropDownList ddl = new DropDownList();
ListItem LItem ;
ddl.CssClass ="stylefont";
ddl.Items.Clear();

for(int i=0;i<dsResponse.Tables[0].Rows.Count;i++)
{
dr = dsResponse.Tables[0].Rows[i];
strResponseType = dr["RESPONSE_TYPE_ID"].ToString();
strResponseID = Convert.ToInt32(dr["RESPONSE_ID"]);

switch(strResponseType)
{
case "1":
RadioButton rb = new RadioButton ();
rb.ID= dr["RESPONSE_TAG"].ToString();
rb.CssClass ="stylecomment";
ph.Controls.Add(new LiteralControl("<tr><td>"));
ph.Controls.Add(rb);
break;

case "2":
DataSet dResp = new DataSet();
int strRespQuestionID;


dResp = objDR.get_QuestionIDFromResponse(strResponseID);

if (dResp.Tables[0].Rows.Count > 0 )
{
strRespQuestionID = Convert.ToInt32(dResp.Tables[0].Rows[0]["QUESTION_ID"]);

if (strQuestionID == strRespQuestionID )
{
ddl.ID= dr["RESPONSE_TAG"].ToString();
LItem = new ListItem( dr["RESPONSE_TEXT_PROMPT"].ToString());
ddl.Items.Add(LItem);
}
}
ph.Controls.Add(new LiteralControl("<tr><td>"));
ph.Controls.Add(ddl);
break;
case "3":
CheckBox cbo = new CheckBox();
cbo.ID= dr["RESPONSE_TAG"].ToString();
cbo.Text = dr["RESPONSE_TEXT_PROMPT"].ToString();
cbo.CssClass ="stylefont";
ph.Controls.Add(new LiteralControl("<tr><td>"));
ph.Controls.Add(cbo);
break;
case "4":
TextBox txt = new TextBox();
txt.ID= dr["RESPONSE_TAG"].ToString();
txt.CssClass ="styletxtbox";
ph.Controls.Add(new LiteralControl("<tr><td>"));
ph.Controls.Add(txt);
break;
}

}
}
}
}
break;
case ListItemType.Footer:
ph.Controls.Add(new LiteralControl("</table>"));
break;
}
container.Controls.Add(ph);
}
catch(Exception ex)
{
throw ex;
}
}

}


static void Item_DataBinding(object sender, System.EventArgs e)
{
try
{

// PlaceHolder ph = (PlaceHolder)sender;
// ((Literal)ph.FindControl("lTest")).Text = "<SCRIPT>JavaScript:test();</SCRIPT>";


// PlaceHolder ph = (PlaceHolder)sender;
// RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
// DropDownList ddl = (DropDownList)ph.NamingContainer ;
//
// if(((DropDownList)(ph).FindControl("ddl").FindControl("ddlQ1")).SelectedValue == "Yes")
// {
// ((Label)(ph).FindControl("lbl").FindControl("lblsubques1")).Visible = true;
// ((CheckBox)(ph).FindControl("cbo").FindControl("cbSub1")).Visible = true;
// ((CheckBox)(ph).FindControl("cbo").FindControl("cbSub1")).Visible = true;
// ((CheckBox)(ph).FindControl("cbo").FindControl("cbSub1")).Visible = true;
// }



}
catch(Exception ex2)
{
throw ex2;
}
finally
{

}

}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion



}
}

Can anybody help me out?

Thanks in advance..