473,663 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Web Button command not firing

I've got a weird problem going on - I've got a table of dynamically created buttons. Each button has the X/Y value of the buttons position in the table assigned to it's CommandArgument property and the name of a common command (btn_Command) assigned to it's Command property. The creation of the table is done by a function called drawGeo. drawGeo is called during the initial Page_Load (!IsPostBack), but should be called by btn_Command on subsequent postbacks, however, it seems that btn_Command is never called when a button is clicked, as I also have two labels, lblX and lblY, that should be set when the btn_Command method is called. All I get is a blank screen with the X & Y values unchanged. I have tried a whole slew of changes, but nothing seems to work right. It's as if btn_Command is never called.

Code below:

public class GeoEdit : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Tabl e Table1;
protected System.Web.UI.W ebControls.Labe l lblX;
protected System.Web.UI.W ebControls.Labe l lblY;
protected System.Web.UI.W ebControls.Labe l lblBtnClicked;
protected dbTier dbt = new dbTier();
private int curX, curY, gridSize;

private void Page_Load(objec t sender, System.EventArg s e)
{
if (!IsPostBack)
{
Session["curX"] = 400;
Session["curY"] = 300;
Session["gridSize"] = 11;
drawGeo();
}

}

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

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

}
#endregion

private void btn_Command(obj ect sender, CommandEventArg s e)
{
string[] cellIndex = e.CommandArgume nt.ToString().S plit(new char[] {';'});
lblBtnClicked.T ext = "CellClicke d";

curX = int.Parse(cellI ndex[0]);
curY = int.Parse(cellI ndex[1]);

lblX.Text = "Row: " + curX.ToString() ;
lblY.Text = "Col: " + curY.ToString() ;

Session["curX"] = curX;
Session["curY"] = curY;

drawGeo();
}

private void drawGeo()
{
curX = (int)Session["curX"];
curY = (int)Session["curY"];
gridSize = (int)Session["gridSize"];

// calculate player location info
int gridShift = gridSize / 2; // ignore remainder
int x1 = curX - gridShift;
int x2 = curX + gridShift;
int y1 = curY - gridShift;
int y2 = curY + gridShift;

lblX.Text = "X: " + curX;
lblY.Text = "Y: " + curY;

// Get Geo info
// DataTable dtGeo = dbt.getGeo(x1, x2, y1, y2);

for (int rowIndex = y1; rowIndex <= y2; rowIndex++)
{
TableRow tr = new TableRow();
Table1.Rows.Add (tr);

for (int colIndex = x1; colIndex <= x2; colIndex++)
{
TableCell tc = new TableCell();
tr.Cells.Add(tc );
Button btn = new Button();
tc.Controls.Add (btn);
btn.CausesValid ation = true;
btn.Text = rowIndex.ToStri ng() + ";" + colIndex.ToStri ng();
btn.CommandArgu ment = rowIndex.ToStri ng() + ";" + colIndex.ToStri ng();
btn.Command += new CommandEventHan dler(btn_Comman d);
}
}

}
}
Nov 18 '05 #1
1 2227
Dyanmic buttons are a pain in ASP.Net. I have heard many people say that
ASP.Net, while it allows and supports it, was not really made for dynamic
controls to be inserted.. especially when an event is involved.

The short of it is that in order for an event to be fired on a dynamically
created control on Postback, the control must be re-added to the page at
postback. For you, this means that geoDraw() needs to be call in Page_Load
regardless of whether it is a postback or not.
Stupid? Yes. Inefficient? Yes. But I know of no other way to get an
event fired of a dynamic control.

HTH,
-Cliff

"Klom Dark" <Klom Da**@discussion s.microsoft.com> wrote in message
news:9A******** *************** ***********@mic rosoft.com...
I've got a weird problem going on - I've got a table of dynamically created buttons. Each button has the X/Y value of the buttons position in
the table assigned to it's CommandArgument property and the name of a common
command (btn_Command) assigned to it's Command property. The creation of the
table is done by a function called drawGeo. drawGeo is called during the
initial Page_Load (!IsPostBack), but should be called by btn_Command on
subsequent postbacks, however, it seems that btn_Command is never called
when a button is clicked, as I also have two labels, lblX and lblY, that
should be set when the btn_Command method is called. All I get is a blank
screen with the X & Y values unchanged. I have tried a whole slew of
changes, but nothing seems to work right. It's as if btn_Command is never
called.
Code below:

public class GeoEdit : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Tabl e Table1;
protected System.Web.UI.W ebControls.Labe l lblX;
protected System.Web.UI.W ebControls.Labe l lblY;
protected System.Web.UI.W ebControls.Labe l lblBtnClicked;
protected dbTier dbt = new dbTier();
private int curX, curY, gridSize;

private void Page_Load(objec t sender, System.EventArg s e)
{
if (!IsPostBack)
{
Session["curX"] = 400;
Session["curY"] = 300;
Session["gridSize"] = 11;
drawGeo();
}

}

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

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

}
#endregion

private void btn_Command(obj ect sender, CommandEventArg s e)
{
string[] cellIndex = e.CommandArgume nt.ToString().S plit(new char[] {';'});
lblBtnClicked.T ext = "CellClicke d";

curX = int.Parse(cellI ndex[0]);
curY = int.Parse(cellI ndex[1]);

lblX.Text = "Row: " + curX.ToString() ;
lblY.Text = "Col: " + curY.ToString() ;

Session["curX"] = curX;
Session["curY"] = curY;

drawGeo();
}

private void drawGeo()
{
curX = (int)Session["curX"];
curY = (int)Session["curY"];
gridSize = (int)Session["gridSize"];

// calculate player location info
int gridShift = gridSize / 2; // ignore remainder
int x1 = curX - gridShift;
int x2 = curX + gridShift;
int y1 = curY - gridShift;
int y2 = curY + gridShift;

lblX.Text = "X: " + curX;
lblY.Text = "Y: " + curY;

// Get Geo info
// DataTable dtGeo = dbt.getGeo(x1, x2, y1, y2);

for (int rowIndex = y1; rowIndex <= y2; rowIndex++)
{
TableRow tr = new TableRow();
Table1.Rows.Add (tr);

for (int colIndex = x1; colIndex <= x2; colIndex++)
{
TableCell tc = new TableCell();
tr.Cells.Add(tc );
Button btn = new Button();
tc.Controls.Add (btn);
btn.CausesValid ation = true;
btn.Text = rowIndex.ToStri ng() + ";" + colIndex.ToStri ng();
btn.CommandArgu ment = rowIndex.ToStri ng() + ";" + colIndex.ToStri ng();
btn.Command += new CommandEventHan dler(btn_Comman d);
}
}

}
}

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7565
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a dropdown in UC1 _________________________ 1) MainPage_Load 2) User Control_1 Load
4
4154
by: EvelynAnd Ethan | last post by:
Hi, ItemCommand event not firing from a dynamic user control ,WHERE A DATAGRID HAS BUTTON,when i click on the linkbutton first time the itemcommand event doesnt fire,second time event fires up any answers?? Regards,
3
13741
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
4
4189
by: TS | last post by:
I am creating a User control and i create some dynamic controls in the init handler. one of the controls is a custom validator which i assign a serverValidate event handler. I usally always do my controls as custom server controls and don't understand why this event won't fire. I figured if the creation of the control was in the init, it would be initialized and have its event handlers set up, then after Load, the control would call its...
3
18700
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however, working with complex reports is tricky Assumption: Reader of this article have basic knowledge of creating data reports. Creating a Parent-Child Command and create a DataReport Suppose we have a database called company with two tables ...
2
1907
by: Mufasa | last post by:
I have code to dynamically generate some link buttons (It's not know how many are needed until runtime.) I am adding the linkbutton to a cell in a table and the adding works fine. It's firing of the event doesn't seem to be happening. Am I missing something? Here's the code: To create the link button:
0
2937
by: CMELLO | last post by:
I have am dynamically loading a web user control based on the click of a tab strip I load the default control for the first tab in the page load event after checking page is not postback. After that the controls are loaded/unloaded based on the SelectionChanged event for the tab strip and again in Page load because with a dynamic load viewstate has to be reloaded. I have a datalist in the user control and I am trying to create the...
0
1910
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I have am dynamically loading a web user control based on the click of a tab strip I load the default control for the first tab in the page load event after checking page is not postback. After that the controls are loaded/unloaded based on the SelectionChanged event for the tab strip and again in Page load because with a dynamic load viewstate has to be reloaded. I have a datalist in the user control and I am trying to create the...
5
5879
by: Tony | last post by:
I am continuing to develop an Access 2007 application which was originally converted from Access 2003. In Access 2003 I was able to disable the Access Close button in the top righthand corner of the screen. I have been unable to find any way to disable this button in Access 2007 and subsequently I have been forced to find ways to detect and handle the situations after the Access Close button has been clicked. I have been largely...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.