473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.Page
{
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Label lblX;
protected System.Web.UI.WebControls.Label lblY;
protected System.Web.UI.WebControls.Label lblBtnClicked;
protected dbTier dbt = new dbTier();
private int curX, curY, gridSize;

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

}

#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

private void btn_Command(object sender, CommandEventArgs e)
{
string[] cellIndex = e.CommandArgument.ToString().Split(new char[] {';'});
lblBtnClicked.Text = "CellClicked";

curX = int.Parse(cellIndex[0]);
curY = int.Parse(cellIndex[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.CausesValidation = true;
btn.Text = rowIndex.ToString() + ";" + colIndex.ToString();
btn.CommandArgument = rowIndex.ToString() + ";" + colIndex.ToString();
btn.Command += new CommandEventHandler(btn_Command);
}
}

}
}
Nov 18 '05 #1
1 2208
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**@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.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.Page
{
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Label lblX;
protected System.Web.UI.WebControls.Label lblY;
protected System.Web.UI.WebControls.Label lblBtnClicked;
protected dbTier dbt = new dbTier();
private int curX, curY, gridSize;

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

}

#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

private void btn_Command(object sender, CommandEventArgs e)
{
string[] cellIndex = e.CommandArgument.ToString().Split(new char[] {';'});
lblBtnClicked.Text = "CellClicked";

curX = int.Parse(cellIndex[0]);
curY = int.Parse(cellIndex[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.CausesValidation = true;
btn.Text = rowIndex.ToString() + ";" + colIndex.ToString();
btn.CommandArgument = rowIndex.ToString() + ";" + colIndex.ToString();
btn.Command += new CommandEventHandler(btn_Command);
}
}

}
}

Nov 18 '05 #2

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

Similar topics

1
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...
4
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 ...
3
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....
4
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...
3
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,...
2
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...
0
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...
0
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.