Skip to content

Databinding Linkbutton using data from Table Cell to Redirect to another page

October 30, 2012

Today, I had worked on finding a better solution to utilise a Link Button to be used as a databind object in a DataGrid Table.

Looking at a similar solution on the web, I found just that solution using TableCell that can be obtained on any column of the DataGrid Table, using a DataBind event. It is quite flexible and easy to use so you don’t have to loop through every Table Cell, as long as you know where the data is displayed on the DataGrid, you can obtain the text from the Table Cell.

Here’s how: –

    // Method that responds to the DataBinding event     
    protected void BindLinkButton(object sender, DataGridItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

                TableCell cell = (TableCell) e.Item.Cells[3];

                LinkButton lnkButton = new LinkButton();
                lnkButton.CommandName = “view”;
                lnkButton.PostBackUrl = “displayinfo.aspx?Customer=” + e.Item.Cells[0].Text + “&Bank=” + e.Item.Cells[2].Text;
                lnkButton.Text = e.Item.Cells[3].Text;
                //lnkButton.Width = 30;

                // Now add the Link Button to view the selected data

                cell.Controls.Add(lnkButton);
           
        }
   }

Notice that I have used TableCell cell = (TableCell) e.Item.Cells[i]; to obtain the TableCell reference, created a Link Buttonb object and added this to the Table Cell Control, this replaces anything previously before on that Table Cell generated from Data Source. You can then reference any other text form any other Table Cells in the DataGrid Table which are columns 0 and 2 then redirected to another page which is displayinfo.aspx: –

lnkButton.PostBackUrl = “displayinfo.aspx?Customer=” + e.Item.Cells[0].Text + “&Bank=” + e.Item.Cells[2].Text;

Hope that helps!

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: