Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Newbie question - Runtime Button and EventHandler

Reply
Thread Tools

Newbie question - Runtime Button and EventHandler

 
 
Mr Not So Know It All
Guest
Posts: n/a
 
      03-21-2007
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");
}
}

 
Reply With Quote
 
 
 
 
Mr Not So Know It All
Guest
Posts: n/a
 
      03-21-2007
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?


 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      03-22-2007
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.
--
Milosz


"Mr Not So Know It All" wrote:

> 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?
>
>
>

 
Reply With Quote
 
Mr Not So Know It All
Guest
Posts: n/a
 
      03-22-2007
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

 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      03-22-2007
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

--
Milosz


"Mr Not So Know It All" wrote:

> 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
>
>

 
Reply With Quote
 
Mr Not So Know It All
Guest
Posts: n/a
 
      03-22-2007
thanks for the help. your example makes a lot of sense. i appreciate
the time and effort. again many thanks

 
Reply With Quote
 
Mr Not So Know It All
Guest
Posts: n/a
 
      03-22-2007
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;
}

}

 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      03-22-2007
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.
--
Regards

Milosz


"Mr Not So Know It All" wrote:

> 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;
> }
>
> }
>
>

 
Reply With Quote
 
Mr Not So Know It All
Guest
Posts: n/a
 
      03-23-2007
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

 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      03-28-2007
Good afternoon,

Depends on the knowledge and area you want to cover, i would recommend any
tutorials or/and books by Dino Esposito (intermidiate and advanced level), or
books from Microsoft press library (from rookie to advanced):
http://www.microsoft.com/learning/bo...t/default.mspx

Best regards
--
Milosz


"Mr Not So Know It All" wrote:

> 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
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Eventhandler in dynamically loaded and bound controls Christian Robert Schulz ASP .Net 2 01-21-2008 02:42 PM
EventHandler AddressOf Question Rob Meade ASP .Net 2 06-04-2007 02:30 PM
.Net 2.0: Button, OnClientClick, and Click EventHandler not all working nicely... Dan Christensen ASP .Net Web Controls 1 08-09-2006 08:31 PM
add eventhandler at runtime in VB SteveR ASP .Net Web Controls 3 02-15-2005 07:31 AM
Adding Eventhandler for a Button in datagrid... in runtime? Lars Netzel ASP .Net 0 07-09-2004 10:09 AM



Advertisments