Newbie question - Runtime Button and EventHandler

  • Thread starter Mr Not So Know It All
  • Start date
M

Mr Not So Know It All

I'm new to NET. This is probably a simple question. I have a table i
build at runtime (in the page CS file). inside the table is a button.
i built a simple click event handler that isnt firing (not getting the
"I'm Back" response write"). can someone please help me figure out
what im doing wrong?

here is the code

aspx file :
<%@ Page Language="C#" AutoEventWireup="true" Debug="true"
Inherits="outage_default" %>

<!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>Outage Site</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="tbl_main" BorderWidth="1" BorderColor="Black"
Width="100%" runat="server" />
</div>
</form>
</body>
</html>

+++++++++++++++++++++++++++++++++++++++++++++++++

CS File

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.HtmlControls;
using System.Data.SqlClient;
using System.Drawing;
using dbConnection; // database connection objects

public class outage_default : Page
{

protected Table tbl_main;

void Page_Load(object s, EventArgs e)
{

DropDownList ddl_company = new DropDownList();

ddl_company.SelectedIndexChanged += new
EventHandler(ddl_company_SelectedIndexChanged);
ddl_company.Items.Insert(0,new ListItem("Select a
Company","0"));
ddl_company.Items.Insert(1,new ListItem("Company A","1"));

TableRow tr = new TableRow();
TableCell tc = new TableCell();

Literal lit_label = new Literal();
lit_label.Text = "Company ";
tc.Controls.AddAt(0, lit_label);
tc.Controls.AddAt(1, ddl_company);
tc.ColumnSpan = 2;
tr.Cells.Add(tc);

tbl_main.Rows.Add(tr);
}

void ddl_company_SelectedIndexChanged(object sender, EventArgs e)
{
TableRow tr_01 = new TableRow();
TableCell tc_0101 = new TableCell();

Button btn_submit = new Button();
btn_submit.Text = "Submit";
btn_submit.Click += new EventHandler(btn_SubmitClick);

tc_0101.ColumnSpan = 2;
tc_0101.Controls.AddAt(0, btn_submit);
tc_0101.HorizontalAlign = HorizontalAlign.Center;

tr_01.Cells.AddAt(0, tc_0101);
tbl_main.Rows.Add(tr_01);
}

void btn_SubmitClick(object sender, EventArgs e)
{
Response.Write("I'm Back");
}
}
 
M

Mr Not So Know It All

because the button is inside the dropdown event handler somehow the
button's event handler is not firing? anyone know why this is and a
solution?
 
G

Guest

Howdy,

Dynamic controls are not persited in page's control tree, therefore you have
to recreate them on every postback. In other words, to make postback events
fire, you have to reinstantiate the button with the same table structure.
 
M

Mr Not So Know It All

are you saying i'll have to recreate the table and the button within
the table again? please provide a code example. thanks for helping
 
G

Guest

Howdy,

Yes i am. Here's the example:

protected void Page_Load(object sender, EventArgs e)
{
CreateTableRowWithDropDownList();

if (RecreateButton)
CreateTableRowWithButton();
}

private void CreateTableRowWithDropDownList()
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
DropDownList dropDown = new DropDownList();

dropDown.ID = "ddlWhatever";
dropDown.AutoPostBack = true;
dropDown.SelectedIndexChanged += new
EventHandler(dropDown_SelectedIndexChanged);
dropDown.Items.Add(new ListItem("Please Select", "0"));
dropDown.Items.Add(new ListItem("Item1", "1"));
dropDown.Items.Add(new ListItem("Item2", "2"));
dropDown.Items.Add(new ListItem("Item3", "3"));

cell.Controls.Add(dropDown);
row.Cells.Add(cell);
table.Rows.Add(row);
}

private void CreateTableRowWithButton()
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
Button button = new Button();

button.ID = "btnWhatever";
button.Text = "Click me";
button.Click += new EventHandler(button_Click);

cell.Controls.Add(button);
row.Cells.Add(cell);
table.Rows.Add(row);
}

private void button_Click(object sender, EventArgs e)
{
Response.Write("i was clicked!");
}

private void dropDown_SelectedIndexChanged(object sender, EventArgs e)
{
CreateTableRowWithButton();
RecreateButton = true;
}

private bool RecreateButton
{
get
{
object value = ViewState["RecreateButton"];
return value == null ? false : (bool) value;
}
set
{
ViewState["RecreateButton"] = value;
}
}

Hope it helps
 
M

Mr Not So Know It All

thanks for the help. your example makes a lot of sense. i appreciate
the time and effort. again many thanks
 
M

Mr Not So Know It All

thanks again,
i replaced your recreatebutton in pageload with if(ispostback) and it
worked.

could you please explain this part of your code?
private bool RecreateButton
{
get
{
object value = ViewState["RecreateButton"];
return value == null ? false : (bool) value;
}
set
{
ViewState["RecreateButton"] = value;
}

}
 
G

Guest

Hi there again,

Yes, of course. It's used to indicate button should be recreated after next
postback. If I got you right, the logic is to show button only if an item in
the drop down list is selected (after the first occurrence of the
selectedindexchanged event). ViewState is a collection that is serialized to
a hidden field and persisted between postbacks (HTTP protocol is stateless)
so next time page is posted back, information stored in the viewstate is
available for the execution. I recommend reading some articles or tutorials
about page execution basics (lifecycle, viewstate, HTTP request, etc) which
will help you understand ASP.NET and make your life easier.
 
M

Mr Not So Know It All

again thanks for the help. do you have any site or book
recommendations? it seems like a lot of the books or tutorials i see
dont discuss run-time execution as much as they discuss execution at
design time.

am i wrong to think that a lot of the coding should be done at run-
time( the code-behind-cs'compiled' files) and not at design time (ascx
or aspx files)? again your help is greatly appreciated.

dj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top