Adding a LinkButton to a gridview, dynamically.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jack

    #1

    Adding a LinkButton to a gridview, dynamically.

    Hi,
    Consider the following handler:

    protected void gridView_RowDat aBound(object sender,
    GridViewRowEven tArgs e)
    {
    GridViewRow row = e.Row;
    if (row.RowType != DataControlRowT ype.DataRow)
    return;

    Label lblRowNr = (Label)row.Find Control("lblRow Nr");
    lblRowNr.Text = String.Format(" {0}.", row.RowIndex + 1);

    LinkButton lnk = new LinkButton();
    lnk.ID = "lnk" + row.RowIndex;
    lnk.Text = "Ooops!";
    lnk.Click += new EventHandler(ln k_Click); //This handler
    (lnk_Click) is never get called!
    row.Cells[0].Controls.Add(l nk);

    }

    I really don't get why the lnk_Click function is never get called.

    Would you please let me know how am I supposed to do this?

    Thanks
    Jack
  • Teemu Keiski

    #2
    Re: Adding a LinkButton to a gridview, dynamically.

    Hi,

    the controls should be added in RowCreated because RowDataBound fires only
    when DataBind() is called, and it could be a postback when no databinding
    happens, when button is clicked...essen tially on postback, it might not
    exist at that point... and for a control to raise the event, it must exist
    in Controls collection at the proper time

    Basic idea is described in following post on ASP.NET Forums:
    A broad category of Microsoft tools, languages, and frameworks for software development. Designed to support developers in building, debugging, and deploying applications across various platforms.


    --
    Teemu Keiski
    AspInsider, ASP.NET MVP




    "jack" <ata@mailinator .comwrote in message
    news:600b7546-3afb-46b0-9e51-38cde8b90611@t5 4g2000hsg.googl egroups.com...
    Hi,
    Consider the following handler:
    >
    protected void gridView_RowDat aBound(object sender,
    GridViewRowEven tArgs e)
    {
    GridViewRow row = e.Row;
    if (row.RowType != DataControlRowT ype.DataRow)
    return;
    >
    Label lblRowNr = (Label)row.Find Control("lblRow Nr");
    lblRowNr.Text = String.Format(" {0}.", row.RowIndex + 1);
    >
    LinkButton lnk = new LinkButton();
    lnk.ID = "lnk" + row.RowIndex;
    lnk.Text = "Ooops!";
    lnk.Click += new EventHandler(ln k_Click); //This handler
    (lnk_Click) is never get called!
    row.Cells[0].Controls.Add(l nk);
    >
    }
    >
    I really don't get why the lnk_Click function is never get called.
    >
    Would you please let me know how am I supposed to do this?
    >
    Thanks
    Jack

    Comment

    • Eliyahu Goldin

      #3
      Re: Adding a LinkButton to a gridview, dynamically.

      You are creating the link dynamically. If you want to use dynamically
      created object in postbacks, you need to re-create them on every postback in
      Page_PreInint or _Init event.

      I would recommend including the link into ItemTemplate and hiding it as
      necessary. Then you can setup the event handler in the markup. No need to
      create the link dynamically.


      --
      Eliyahu Goldin,
      Software Developer
      Microsoft MVP [ASP.NET]




      "jack" <ata@mailinator .comwrote in message
      news:600b7546-3afb-46b0-9e51-38cde8b90611@t5 4g2000hsg.googl egroups.com...
      Hi,
      Consider the following handler:
      >
      protected void gridView_RowDat aBound(object sender,
      GridViewRowEven tArgs e)
      {
      GridViewRow row = e.Row;
      if (row.RowType != DataControlRowT ype.DataRow)
      return;
      >
      Label lblRowNr = (Label)row.Find Control("lblRow Nr");
      lblRowNr.Text = String.Format(" {0}.", row.RowIndex + 1);
      >
      LinkButton lnk = new LinkButton();
      lnk.ID = "lnk" + row.RowIndex;
      lnk.Text = "Ooops!";
      lnk.Click += new EventHandler(ln k_Click); //This handler
      (lnk_Click) is never get called!
      row.Cells[0].Controls.Add(l nk);
      >
      }
      >
      I really don't get why the lnk_Click function is never get called.
      >
      Would you please let me know how am I supposed to do this?
      >
      Thanks
      Jack

      Comment

      • jack

        #4
        Re: Adding a LinkButton to a gridview, dynamically.

        On Jul 14, 6:21 pm, "Teemu Keiski" <jot...@aspalli ance.comwrote:
        Hi,
        >
        the controls should be added in RowCreated because RowDataBound fires only
        when DataBind() is called, and it could be a postback when no databinding
        happens, when button is clicked...essen tially on postback, it might not
        exist at that point... and for a control to raise the event, it must exist
        in Controls collection at the proper time
        >
        Basic idea is described in following post on ASP.NET Forums:http://forums.asp.net/p/745417/745492.aspx
        >
        --
        Teemu Keiski
        AspInsider, ASP.NET MVPhttp://blogs.aspadvice .com/jotekehttp://teemukeiski.net
        >
        "jack" <a...@mailinato r.comwrote in message
        >
        news:600b7546-3afb-46b0-9e51-38cde8b90611@t5 4g2000hsg.googl egroups.com...
        >
        Hi,
        Consider the following handler:
        >
        protected void gridView_RowDat aBound(object sender,
        GridViewRowEven tArgs e)
        {
        GridViewRow row = e.Row;
        if (row.RowType != DataControlRowT ype.DataRow)
        return;
        >
        Label lblRowNr = (Label)row.Find Control("lblRow Nr");
        lblRowNr.Text = String.Format(" {0}.", row.RowIndex + 1);
        >
        LinkButton lnk = new LinkButton();
        lnk.ID = "lnk" + row.RowIndex;
        lnk.Text = "Ooops!";
        lnk.Click += new EventHandler(ln k_Click); //This handler
        (lnk_Click) is never get called!
        row.Cells[0].Controls.Add(l nk);
        >
        }
        >
        I really don't get why the lnk_Click function is never get called.
        >
        Would you please let me know how am I supposed to do this?
        >
        Thanks
        Jack
        Well, thank you. I now understand how the gridview control works.
        Based on those articles I've already read, I've got two options:

        1. In the occurrence of RowDataBound event, I need to save what I need
        to create dynamically, in a ViewState. Then, in the RowCreated, I need
        to recreate the controls based on the saved ViewStates.

        2. On the Load event, I'll call the gridView.DataBi nd method, on
        PostBack conditions.

        The ViewState thing is really hard to do (in my scenario), since the
        controls I'm creating are a lot.

        Is there any better way to do this? Please note that I need the
        DataItem to create my controls dynamically.

        Comment

        • Teemu Keiski

          #5
          Re: Adding a LinkButton to a gridview, dynamically.

          Hi,

          using a template e.g implementing ITemplate or custom field/column might
          offer a bit cleaner route.

          --
          Teemu Keiski
          AspInsider, ASP.NET MVP




          "jack" <ata@mailinator .comwrote in message
          news:5da2b84f-294b-4ed9-b440-02891557eb8b@x4 1g2000hsb.googl egroups.com...
          On Jul 14, 6:21 pm, "Teemu Keiski" <jot...@aspalli ance.comwrote:
          >Hi,
          >>
          >the controls should be added in RowCreated because RowDataBound fires
          >only
          >when DataBind() is called, and it could be a postback when no databinding
          >happens, when button is clicked...essen tially on postback, it might not
          >exist at that point... and for a control to raise the event, it must
          >exist
          >in Controls collection at the proper time
          >>
          >Basic idea is described in following post on ASP.NET
          >Forums:http://forums.asp.net/p/745417/745492.aspx
          >>
          >--
          >Teemu Keiski
          >AspInsider, ASP.NET
          >MVPhttp://blogs.aspadvice .com/jotekehttp://teemukeiski.net
          >>
          >"jack" <a...@mailinato r.comwrote in message
          >>
          >news:600b754 6-3afb-46b0-9e51-38cde8b90611@t5 4g2000hsg.googl egroups.com...
          >>
          Hi,
          Consider the following handler:
          >>
          protected void gridView_RowDat aBound(object sender,
          GridViewRowEven tArgs e)
          {
          GridViewRow row = e.Row;
          if (row.RowType != DataControlRowT ype.DataRow)
          return;
          >>
          Label lblRowNr = (Label)row.Find Control("lblRow Nr");
          lblRowNr.Text = String.Format(" {0}.", row.RowIndex + 1);
          >>
          LinkButton lnk = new LinkButton();
          lnk.ID = "lnk" + row.RowIndex;
          lnk.Text = "Ooops!";
          lnk.Click += new EventHandler(ln k_Click); //This handler
          (lnk_Click) is never get called!
          row.Cells[0].Controls.Add(l nk);
          >>
          }
          >>
          I really don't get why the lnk_Click function is never get called.
          >>
          Would you please let me know how am I supposed to do this?
          >>
          Thanks
          Jack
          >
          Well, thank you. I now understand how the gridview control works.
          Based on those articles I've already read, I've got two options:
          >
          1. In the occurrence of RowDataBound event, I need to save what I need
          to create dynamically, in a ViewState. Then, in the RowCreated, I need
          to recreate the controls based on the saved ViewStates.
          >
          2. On the Load event, I'll call the gridView.DataBi nd method, on
          PostBack conditions.
          >
          The ViewState thing is really hard to do (in my scenario), since the
          controls I'm creating are a lot.
          >
          Is there any better way to do this? Please note that I need the
          DataItem to create my controls dynamically.

          Comment

          Working...