Defining a Command for a LinkButton

Discussion in 'ASP .Net' started by =?Utf-8?B?Vmk=?=, Nov 1, 2004.

  1. Hi,
    I'm trying to dinamically add LinkButton Controls to a web form. I do
    something like:

    for(i=1;i<=someVariable;i++)
    {
    LinkButton PageLink = new LinkButton();
    PageLink.CommandName = i.ToString();
    PageLink.Text = "Page " + i;

    PageLink.Command += PageLink_Click;
    }
    The problem is with the last lane. What I'm trying to do there is to specify
    what methon in the code behind needs to called when the LinkButton is
    clicked. But it generates an error at compilation.
    Please Help,
    Thank you
     
    =?Utf-8?B?Vmk=?=, Nov 1, 2004
    #1
    1. Advertising

  2. LinkButton.Command requires a CommandEventHandler. Try chaning that line to:
    PageLink.Command += new CommandEventHandler(PageLink_Click);

    Hope that helps.

    --
    Jason Whitted



    "Vi" wrote:

    > Hi,
    > I'm trying to dinamically add LinkButton Controls to a web form. I do
    > something like:
    >
    > for(i=1;i<=someVariable;i++)
    > {
    > LinkButton PageLink = new LinkButton();
    > PageLink.CommandName = i.ToString();
    > PageLink.Text = "Page " + i;
    >
    > PageLink.Command += PageLink_Click;
    > }
    > The problem is with the last lane. What I'm trying to do there is to specify
    > what methon in the code behind needs to called when the LinkButton is
    > clicked. But it generates an error at compilation.
    > Please Help,
    > Thank you
     
    =?Utf-8?B?SldoaXR0ZWQ=?=, Nov 1, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?Vmk=?=

    Karl Seguin Guest

    Try:

    PageLink.Command += new CommandEventHandler(PageLink_Click);

    Also, I assume you only showed up part of the code, but you are adding this
    control to the page somehow..and finally, you need to make sure to recreate
    these on postback in order for the event handler to get hooked up and thus
    fire.

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Vi" <> wrote in message
    news:...
    > Hi,
    > I'm trying to dinamically add LinkButton Controls to a web form. I do
    > something like:
    >
    > for(i=1;i<=someVariable;i++)
    > {
    > LinkButton PageLink = new LinkButton();
    > PageLink.CommandName = i.ToString();
    > PageLink.Text = "Page " + i;
    >
    > PageLink.Command += PageLink_Click;
    > }
    > The problem is with the last lane. What I'm trying to do there is to

    specify
    > what methon in the code behind needs to called when the LinkButton is
    > clicked. But it generates an error at compilation.
    > Please Help,
    > Thank you
     
    Karl Seguin, Nov 1, 2004
    #3
  4. I did that, but when I'm clicking the LinkButton, the form gets posted back,
    but the handler method PageLink_Click is not called. Any idea why?

    Thanks again

    "JWhitted" wrote:

    > LinkButton.Command requires a CommandEventHandler. Try chaning that line to:
    > PageLink.Command += new CommandEventHandler(PageLink_Click);
    >
    > Hope that helps.
    >
    > --
    > Jason Whitted
    >
    >
    >
    > "Vi" wrote:
    >
    > > Hi,
    > > I'm trying to dinamically add LinkButton Controls to a web form. I do
    > > something like:
    > >
    > > for(i=1;i<=someVariable;i++)
    > > {
    > > LinkButton PageLink = new LinkButton();
    > > PageLink.CommandName = i.ToString();
    > > PageLink.Text = "Page " + i;
    > >
    > > PageLink.Command += PageLink_Click;
    > > }
    > > The problem is with the last lane. What I'm trying to do there is to specify
    > > what methon in the code behind needs to called when the LinkButton is
    > > clicked. But it generates an error at compilation.
    > > Please Help,
    > > Thank you
     
    =?Utf-8?B?Vmk=?=, Nov 1, 2004
    #4
  5. I do the controls to the form. But I'm not sure how to recreate them on
    postback, since I'm creating them dinamically (they're not defined in the
    aspx file) and if I try:

    If(IsPostBack)
    {
    PageLink.Command += new CommandEventHandler(PageLink_Click);
    }
    it fails because PageLink is not defined (Error: "Object reference not set
    to an instance of an object")

    I'm really stock here.
    Thanks

    "Karl Seguin" wrote:

    > Try:
    >
    > PageLink.Command += new CommandEventHandler(PageLink_Click);
    >
    > Also, I assume you only showed up part of the code, but you are adding this
    > control to the page somehow..and finally, you need to make sure to recreate
    > these on postback in order for the event handler to get hooked up and thus
    > fire.
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "Vi" <> wrote in message
    > news:...
    > > Hi,
    > > I'm trying to dinamically add LinkButton Controls to a web form. I do
    > > something like:
    > >
    > > for(i=1;i<=someVariable;i++)
    > > {
    > > LinkButton PageLink = new LinkButton();
    > > PageLink.CommandName = i.ToString();
    > > PageLink.Text = "Page " + i;
    > >
    > > PageLink.Command += PageLink_Click;
    > > }
    > > The problem is with the last lane. What I'm trying to do there is to

    > specify
    > > what methon in the code behind needs to called when the LinkButton is
    > > clicked. But it generates an error at compilation.
    > > Please Help,
    > > Thank you

    >
    >
    >
     
    =?Utf-8?B?Vmk=?=, Nov 1, 2004
    #5
  6. At what point are the LinkButtons getting added to the page's control
    collection?

    Seams like the the for loop should look something like:
    for(int i=1; i<someVariable; i++)
    {
    LinkButton PageLink = new LinkButton();
    PageLink.CommandName = i.ToString();
    PageLink.Text = "Page " + i;
    PageLink.Command += new CommandEventHandler(PageLink_Click);
    this.Controls.Add(PageLink);
    }



    "Vi" wrote:

    > I did that, but when I'm clicking the LinkButton, the form gets posted back,
    > but the handler method PageLink_Click is not called. Any idea why?
    >
    > Thanks again
    >
    > "JWhitted" wrote:
    >
    > > LinkButton.Command requires a CommandEventHandler. Try chaning that line to:
    > > PageLink.Command += new CommandEventHandler(PageLink_Click);
    > >
    > > Hope that helps.
    > >
    > > --
    > > Jason Whitted
    > >
    > >
    > >
    > > "Vi" wrote:
    > >
    > > > Hi,
    > > > I'm trying to dinamically add LinkButton Controls to a web form. I do
    > > > something like:
    > > >
    > > > for(i=1;i<=someVariable;i++)
    > > > {
    > > > LinkButton PageLink = new LinkButton();
    > > > PageLink.CommandName = i.ToString();
    > > > PageLink.Text = "Page " + i;
    > > >
    > > > PageLink.Command += PageLink_Click;
    > > > }
    > > > The problem is with the last lane. What I'm trying to do there is to specify
    > > > what methon in the code behind needs to called when the LinkButton is
    > > > clicked. But it generates an error at compilation.
    > > > Please Help,
    > > > Thank you
     
    =?Utf-8?B?SldoaXR0ZWQ=?=, Nov 1, 2004
    #6
  7. I have a Button that retrieves all the data from the DB and builds this
    LinkButtons as well based on how much data was retrieved.

    for(int i=1;i<=iPageCount;i++)
    {
    oCell = new HtmlTableCell();
    LinkButton PageLink = new LinkButton();
    PageLink.CommandName = i.ToString();
    PageLink.Text = "Page " + i;
    PageLink.Command += new CommandEventHandler(PageLink_Click);
    PageLink.CausesValidation = false;
    oCell.Controls.Add(PageLink);

    oTR.Cells.Add(oCell);
    }
    oTBL.Rows.Add(oTR);

    The data is retrieved and the list of pages looks fine.
    When I click on the page link, the form is posted back, but the method
    PageLink_Click is not called (only Page_Load runs).
    Karl Seguin also replied to this post and he suggested that I have " to make
    sure to recreate these on postback in order for the event handler to get
    hooked up and thus fire" - I guess this is what I'm missing, but I don't know
    how to do it, since all the LinkButtons are generated dinamically (are not
    defined in the aspx) and therefore
    If(IsPostBack)
    {
    PageLink.Command += new CommandEventHandler(PageLink_Click);
    }
    generates the error "Object reference not set to an instance of an object".
    The same thing happens if I try to put this code in InitializeComponent().
    Any idea how to solve this problem?

    Thanks a lot.


    "JWhitted" wrote:

    > At what point are the LinkButtons getting added to the page's control
    > collection?
    >
    > Seams like the the for loop should look something like:
    > for(int i=1; i<someVariable; i++)
    > {
    > LinkButton PageLink = new LinkButton();
    > PageLink.CommandName = i.ToString();
    > PageLink.Text = "Page " + i;
    > PageLink.Command += new CommandEventHandler(PageLink_Click);
    > this.Controls.Add(PageLink);
    > }
    >
    >
    >
    > "Vi" wrote:
    >
    > > I did that, but when I'm clicking the LinkButton, the form gets posted back,
    > > but the handler method PageLink_Click is not called. Any idea why?
    > >
    > > Thanks again
    > >
    > > "JWhitted" wrote:
    > >
    > > > LinkButton.Command requires a CommandEventHandler. Try chaning that line to:
    > > > PageLink.Command += new CommandEventHandler(PageLink_Click);
    > > >
    > > > Hope that helps.
    > > >
    > > > --
    > > > Jason Whitted
    > > >
    > > >
    > > >
    > > > "Vi" wrote:
    > > >
    > > > > Hi,
    > > > > I'm trying to dinamically add LinkButton Controls to a web form. I do
    > > > > something like:
    > > > >
    > > > > for(i=1;i<=someVariable;i++)
    > > > > {
    > > > > LinkButton PageLink = new LinkButton();
    > > > > PageLink.CommandName = i.ToString();
    > > > > PageLink.Text = "Page " + i;
    > > > >
    > > > > PageLink.Command += PageLink_Click;
    > > > > }
    > > > > The problem is with the last lane. What I'm trying to do there is to specify
    > > > > what methon in the code behind needs to called when the LinkButton is
    > > > > clicked. But it generates an error at compilation.
    > > > > Please Help,
    > > > > Thank you
     
    =?Utf-8?B?Vmk=?=, Nov 1, 2004
    #7
  8. I have a Button that retrieves all the data from the DB and builds this
    LinkButtons as well based on how much data was retrieved.

    for(int i=1;i<=iPageCount;i++)
    {
    oCell = new HtmlTableCell();
    LinkButton PageLink = new LinkButton();
    PageLink.CommandName = i.ToString();
    PageLink.Text = "Page " + i;
    PageLink.Command += new CommandEventHandler(PageLink_Click);
    PageLink.CausesValidation = false;
    oCell.Controls.Add(PageLink);

    oTR.Cells.Add(oCell);
    }
    oTBL.Rows.Add(oTR);

    The data is retrieved and the list of pages looks fine.
    When I click on the page link, the form is posted back, but the method
    PageLink_Click is not called (only Page_Load runs).
    Karl Seguin also replied to this post and he suggested that I have " to make
    sure to recreate these on postback in order for the event handler to get
    hooked up and thus fire" - I guess this is what I'm missing, but I don't know
    how to do it, since all the LinkButtons are generated dinamically (are not
    defined in the aspx) and therefore
    If(IsPostBack)
    {
    PageLink.Command += new CommandEventHandler(PageLink_Click);
    }
    generates the error "Object reference not set to an instance of an object".
    The same thing happens if I try to put this code in InitializeComponent().
    Any idea how to solve this problem?

    Thanks a lot.


    "JWhitted" wrote:

    > At what point are the LinkButtons getting added to the page's control
    > collection?
    >
    > Seams like the the for loop should look something like:
    > for(int i=1; i<someVariable; i++)
    > {
    > LinkButton PageLink = new LinkButton();
    > PageLink.CommandName = i.ToString();
    > PageLink.Text = "Page " + i;
    > PageLink.Command += new CommandEventHandler(PageLink_Click);
    > this.Controls.Add(PageLink);
    > }
    >
    >
    >
    > "Vi" wrote:
    >
    > > I did that, but when I'm clicking the LinkButton, the form gets posted back,
    > > but the handler method PageLink_Click is not called. Any idea why?
    > >
    > > Thanks again
    > >
    > > "JWhitted" wrote:
    > >
    > > > LinkButton.Command requires a CommandEventHandler. Try chaning that line to:
    > > > PageLink.Command += new CommandEventHandler(PageLink_Click);
    > > >
    > > > Hope that helps.
    > > >
    > > > --
    > > > Jason Whitted
    > > >
    > > >
    > > >
    > > > "Vi" wrote:
    > > >
    > > > > Hi,
    > > > > I'm trying to dinamically add LinkButton Controls to a web form. I do
    > > > > something like:
    > > > >
    > > > > for(i=1;i<=someVariable;i++)
    > > > > {
    > > > > LinkButton PageLink = new LinkButton();
    > > > > PageLink.CommandName = i.ToString();
    > > > > PageLink.Text = "Page " + i;
    > > > >
    > > > > PageLink.Command += PageLink_Click;
    > > > > }
    > > > > The problem is with the last lane. What I'm trying to do there is to specify
    > > > > what methon in the code behind needs to called when the LinkButton is
    > > > > clicked. But it generates an error at compilation.
    > > > > Please Help,
    > > > > Thank you
     
    =?Utf-8?B?Vmk=?=, Nov 1, 2004
    #8
  9. One solution is to put a hidden form field on the form. This hidden field
    can contain a comma seperated list of page ids that were initially loaded.
    Then on postback the linkbuttons can be recreated. I have included an
    example:

    #########################
    Test.aspx
    #########################
    <%@ Page language="c#" Codebehind="Test.aspx.cs" AutoEventWireup="false"
    Inherits="Testing.Test" %>
    <html>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:Table ID="table" Runat="server" />
    <input type="hidden" ID="pageLinks" runat="server" />
    </form>
    </body>
    </table>

    #########################
    Test.aspx.cs
    #########################
    using System;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace Testing
    {
    public class Test : System.Web.UI.Page
    {
    protected Table table;
    protected HtmlInputHidden pageLinks;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here

    int[] ids;
    if(!this.IsPostBack)
    {
    // Initial Load: Lookup the page id's
    ids = new int[]{1,3,5,7,9};

    // Need to populate the pageLinks hidden text field.
    StringBuilder sb = new StringBuilder();
    for(int x=0; x<ids.Length; x++)
    sb.Append((x==0 ? "" : ",") + ids[x]);
    pageLinks.Value = sb.ToString();
    }
    else
    {
    // PostBack: we need to parse the hidden text field.
    string[] vals = pageLinks.Value.Split(',');
    ids = new int[vals.Length];
    for(int x=0; x<ids.Length; x++)
    ids[x] = Convert.ToInt32(vals[x], 10);
    }

    foreach(int id in ids)
    {
    TableRow row = new TableRow();
    TableCell cell = new TableCell();

    LinkButton btn = new LinkButton();
    btn.Text = "Button " + id;
    btn.CommandName = id.ToString();
    btn.Command += new CommandEventHandler(btn_Command);

    cell.Controls.Add(btn);
    row.Controls.Add(cell);
    table.Rows.Add(row);
    }
    }

    private void btn_Command(object sender, CommandEventArgs e)
    {
    LinkButton button = sender as LinkButton;
    Response.Output.WriteLine("{0} was clicked. The command name
    was {1}.", button.Text, e.CommandName);
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }

    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
    }
    }


    Hope that helps.

    --
    Jason Whitted
     
    =?Utf-8?B?SldoaXR0ZWQ=?=, Nov 1, 2004
    #9
  10. =?Utf-8?B?Vmk=?=

    Karl Seguin Guest

    I'm not following the other branch of this conversation, but where are you
    initially doing your for loop?

    for(i=1;i<=someVariable;i++){
    LinkButton PageLink = new LinkButton();
    PageLink.CommandName = i.ToString();
    PageLink.Text = "Page " + i.ToString();
    PageLink.ID = "pageLink" + i.ToString(); //added by me

    PageLink.Command += new CommandEventHandler(PageLink_Click);
    Page.Controls.Add(PageLink) //added by me
    }

    Anyways, you need to do the same for loop when the page is posted back.


    For example, doing this should work:
    void page_load{
    for(i=1;i<=someVariable;i++){
    LinkButton PageLink = new LinkButton();
    PageLink.CommandName = i.ToString();
    PageLink.Text = "Page " + i.ToString();
    PageLink.ID = "pageLink" + i.ToString(); //added by me

    PageLink.Command += new CommandEventHandler(PageLink_Click);
    Page.Controls.Add(PageLink) //added by me
    }
    }


    but doing this won't:
    void page_load{
    if (!Page.IsPostBack){
    for(i=1;i<=someVariable;i++){
    LinkButton PageLink = new LinkButton();
    PageLink.CommandName = i.ToString();
    PageLink.Text = "Page " + i.ToString();
    PageLink.ID = "pageLink" + i.ToString(); //added by me

    PageLink.Command += new CommandEventHandler(PageLink_Click);
    Page.Controls.Add(PageLink) //added by me
    }
    }
    }
    because the controls aren't created on postback

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Vi" <> wrote in message
    news:...
    > I do the controls to the form. But I'm not sure how to recreate them on
    > postback, since I'm creating them dinamically (they're not defined in the
    > aspx file) and if I try:
    >
    > If(IsPostBack)
    > {
    > PageLink.Command += new CommandEventHandler(PageLink_Click);
    > }
    > it fails because PageLink is not defined (Error: "Object reference not set
    > to an instance of an object")
    >
    > I'm really stock here.
    > Thanks
    >
    > "Karl Seguin" wrote:
    >
    > > Try:
    > >
    > > PageLink.Command += new CommandEventHandler(PageLink_Click);
    > >
    > > Also, I assume you only showed up part of the code, but you are adding

    this
    > > control to the page somehow..and finally, you need to make sure to

    recreate
    > > these on postback in order for the event handler to get hooked up and

    thus
    > > fire.
    > >
    > > Karl
    > >
    > > --
    > > MY ASP.Net tutorials
    > > http://www.openmymind.net/
    > >
    > >
    > > "Vi" <> wrote in message
    > > news:...
    > > > Hi,
    > > > I'm trying to dinamically add LinkButton Controls to a web form. I do
    > > > something like:
    > > >
    > > > for(i=1;i<=someVariable;i++)
    > > > {
    > > > LinkButton PageLink = new LinkButton();
    > > > PageLink.CommandName = i.ToString();
    > > > PageLink.Text = "Page " + i;
    > > >
    > > > PageLink.Command += PageLink_Click;
    > > > }
    > > > The problem is with the last lane. What I'm trying to do there is to

    > > specify
    > > > what methon in the code behind needs to called when the LinkButton is
    > > > clicked. But it generates an error at compilation.
    > > > Please Help,
    > > > Thank you

    > >
    > >
    > >
     
    Karl Seguin, Nov 2, 2004
    #10
  11. It worked, you really saved me.
    Thanks a lot, J


    "JWhitted" wrote:

    > One solution is to put a hidden form field on the form. This hidden field
    > can contain a comma seperated list of page ids that were initially loaded.
    > Then on postback the linkbuttons can be recreated. I have included an
    > example:
    >
    > #########################
    > Test.aspx
    > #########################
    > <%@ Page language="c#" Codebehind="Test.aspx.cs" AutoEventWireup="false"
    > Inherits="Testing.Test" %>
    > <html>
    > <body MS_POSITIONING="GridLayout">
    > <form id="Form1" method="post" runat="server">
    > <asp:Table ID="table" Runat="server" />
    > <input type="hidden" ID="pageLinks" runat="server" />
    > </form>
    > </body>
    > </table>
    >
    > #########################
    > Test.aspx.cs
    > #########################
    > using System;
    > using System.Text;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    >
    > namespace Testing
    > {
    > public class Test : System.Web.UI.Page
    > {
    > protected Table table;
    > protected HtmlInputHidden pageLinks;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > // Put user code to initialize the page here
    >
    > int[] ids;
    > if(!this.IsPostBack)
    > {
    > // Initial Load: Lookup the page id's
    > ids = new int[]{1,3,5,7,9};
    >
    > // Need to populate the pageLinks hidden text field.
    > StringBuilder sb = new StringBuilder();
    > for(int x=0; x<ids.Length; x++)
    > sb.Append((x==0 ? "" : ",") + ids[x]);
    > pageLinks.Value = sb.ToString();
    > }
    > else
    > {
    > // PostBack: we need to parse the hidden text field.
    > string[] vals = pageLinks.Value.Split(',');
    > ids = new int[vals.Length];
    > for(int x=0; x<ids.Length; x++)
    > ids[x] = Convert.ToInt32(vals[x], 10);
    > }
    >
    > foreach(int id in ids)
    > {
    > TableRow row = new TableRow();
    > TableCell cell = new TableCell();
    >
    > LinkButton btn = new LinkButton();
    > btn.Text = "Button " + id;
    > btn.CommandName = id.ToString();
    > btn.Command += new CommandEventHandler(btn_Command);
    >
    > cell.Controls.Add(btn);
    > row.Controls.Add(cell);
    > table.Rows.Add(row);
    > }
    > }
    >
    > private void btn_Command(object sender, CommandEventArgs e)
    > {
    > LinkButton button = sender as LinkButton;
    > Response.Output.WriteLine("{0} was clicked. The command name
    > was {1}.", button.Text, e.CommandName);
    > }
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    > }
    > }
    >
    >
    > Hope that helps.
    >
    > --
    > Jason Whitted
     
    =?Utf-8?B?Vmk=?=, Nov 2, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sathyaish
    Replies:
    3
    Views:
    3,131
    Sathyaish
    Sep 8, 2005
  2. johny smith
    Replies:
    8
    Views:
    425
    Peter Koch Larsen
    Jul 2, 2004
  3. Sathyaish

    Linkbutton does not look like a linkbutton

    Sathyaish, Sep 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    201
    Sathyaish
    Sep 8, 2005
  4. Jeff Miller
    Replies:
    1
    Views:
    96
    Peña, Botp
    May 19, 2008
  5. Noob
    Replies:
    3
    Views:
    151
    Philip Lantz
    Sep 1, 2013
Loading...

Share This Page