Table of dynamic buttons implemeting IPostBackEventHandler

Discussion in 'ASP .Net' started by Guest, Jun 17, 2004.

  1. Guest

    Guest Guest

    I'm trying to figure out how to get a table of dynamically created buttons to actually call the command associated with it. It never seems to work, but doing more research it looks like I may need to implement IPostBackEventHandler. However, I cannot find any examples of doing so.

    Does anyone have an example of using IPostBackEventHandler/RaisePostBackEvent with an array of controls?
     
    Guest, Jun 17, 2004
    #1
    1. Advertisements

  2. Guest

    avnrao Guest

    how r u dynamically creating the buttons?
    if you are using Button bt = new Button() code in your codebehind file, you
    can very well create a delegate and add it to the click event.
    you can post your code for us to understand the problem better.

    Av.
     
    avnrao, Jun 17, 2004
    #2
    1. Advertisements

  3. Guest

    Guest Guest

    Here's the code

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

    }
    }
     
    Guest, Jun 17, 2004
    #3
  4. Guest

    avnrao Guest

    in your drawGeo method, try this
    btn.Click += new System.EventHandler(this.Button1_Click);

    instead of
    btn.Command += new CommandEventHandler(btn_Command);

    Av.
     
    avnrao, Jun 17, 2004
    #4
  5. Guest

    Guest Guest

    I tried that first, didn't change anything. It seems that once the postback occurs, ASP.NET loses the references to the original controls, and cannot fire off the event that was specified without the reference. Why? I dunno. But it's looking like implementing IPostBackEventHandler might be the way to go, but I can't find any idiot's guide to IPostBackEventHandler, the MSDN docs are pretty much worthless to a beginner to the concept.

     
    Guest, Jun 17, 2004
    #5
  6. Guest

    avnrao Guest

    hey..sorry..didnt observe for the first time..
    you have to create these controls even in post back also..
    put your drawGeo outside IsPostBack.

    should work.
    Av.
     
    avnrao, Jun 17, 2004
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.