kiran kumar

kiran kumar

  • NA
  • 1k
  • 270.9k

helpme

Mar 18 2010 9:05 AM

i am struggling a bit on this,i have loaded controls dynamically upon a button click,the same i wanted to do on dropdown selected index changed event(this dropdown is added dynamically),i wrote the code to add controls dynamically in selectedindex changed event,but as soon as the index gets changed,the contrtols which i added previously (i.e upon button click)gets disappeared from the page.
      help me,any help is very much appreciated
take a look at my code
protected
void Button1_Click1(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection("Data Source=sudheerkumar\\sqlexpress;Initial Catalog=police;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select vehno from rto where vehno='" + TextBox1.Text + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (dr["vehno"].ToString() == TextBox1.Text)
{
Label2.Text =
"record found";
Label2.Visible =
true;
td =
new Table();
td.ID =
"tab";
td.Attributes.Add(
"style", "border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; background-color: #5b80cc; Alignment: Centre; border-top: 1px solid black");
Page.Form.Controls.Add(td);
tr =
new TableRow();
TableCell tc = new TableCell();
Label lb = new Label();
lb.Text =
"case id";
tr.Cells.Add(tc);
tc.Controls.Add(lb);
TableCell tc2 = new TableCell();
TextBox tb = new TextBox();
tr.Cells.Add(tc2);
tc2.Controls.Add(tb);
td.Rows.Add(tr);

tr1 =
new TableRow();
TableCell tc1 = new TableCell();
Label lb1 = new Label();
lb1.Text =
"case type";
tr1.Cells.Add(tc1);
tc1.Controls.Add(lb1);
TableCell tc3 = new TableCell();
DropDownList dd = new DropDownList();

dd.Items.Add(
"traffic violation");
dd.Items.Add(
"missing vehicles");
dd.Items.Add(
"accident");
dd.AutoPostBack =
true;
dd.EnableViewState =
false;
dd.Attributes.Add(
"style","Height:20px;Width:118px");
Page.Form.Controls.Add(dd);
dd.SelectedIndexChanged +=
new EventHandler(dd_SelectedIndexChanged);
Page.Form.EnableViewState =
true;
Page.EnableViewState =
true;
// tr1.Cells.Add(tc3);
//tc3.Controls.Add(dd);
//td.Rows.Add(tr1);
}

}
else
{
Label2.Text =
"record not found";
Label2.Visible =
true;
}


}
public void dd_SelectedIndexChanged(object sender, EventArgs e)
{

if (dd.SelectedItem.Text =="accident")
{

Table td1 = new Table();
td1.ID =
"hi";
td1.Attributes.Add(
"style2", "border-bottom: 1px solid black;Alignment:right");
Page.Form.Controls.Add(td1);
tr2 =
new TableRow();
TableCell tc4 = new TableCell();
Label lb3 = new Label();
lb3.Text =
"find";
tr2.Cells.Add(tc4);
tc4.Controls.Add(lb3);
td1.Rows.Add(tr2);
//TableCell tc5 = new TableCell();
//TextBox txt3 = new TextBox();
//tr2.Cells.Add(tc5);
//tc5.Controls.Add(txt3);
//td.Rows.Add(tr2);
}
}