简体   繁体   中英

How to add a href for a button/link in ASP.NET GridView

I have an ASP.NET web app that processes some data from other sites and displays the information in a gridview. I don't know how many rows that gridview is going to have, nor how many columns.

In this gridview I have a templatefield which I used to add a Checkbox. The rest of the columns are added using a DataTable that I bind to this gridview. The problem now is that after 10 columns, I have an URL which I want to display as a button, or a link. After this link I have x nr of columns.

How do I add this URL which is resting between static columns and dynamic columns, in a GridView with dynamic rows ?

I tried writing a href=link.. in the DataTable but it displays it as text. I found an article that suggested something with HtmlDecode, but for that to work I would need to use boundfields which set the htmlencode = false..or something like that.

Is there any way of doing this ? or should I just move the link in the itemtemplate that has the checkbox as well and try to set it there ?

在此处输入图片说明

You can use the MatchGrid_RowDataBound method to look over the columns and add the buttons as you need.
Here is an example of how to add a button on RowDataBound:

How do I programmatically add a button to a gridview and assign it to a specific code-behind function?

You can use the OnRowDataBound for that. It will insert extra cells into the GridView. You could also insert extra column into the DataTable before binding it to the GridView.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //header
    if (e.Row.RowType == DataControlRowType.Header)
    {
        //add 6 cells
        for (int i = 1; i <= 6; i++)
        {
            TableHeaderCell headerCell = new TableHeaderCell();

            if (i == 6)
            {
                headerCell.Text = "URL";
            }
            else
            {
                headerCell.Text = "Static " + i;
            }

            //add the new cell to the gridview
            e.Row.Cells.AddAt(i, headerCell);
        }
    }

    //normal row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the current row to a datarowview
        DataRowView row = e.Row.DataItem as DataRowView;

        //add 6 cells
        for (int i = 1; i <= 6; i++)
        {
            TableCell cell = new TableCell();

            if (i == 6)
            {
                cell.Text = string.Format("<a target=\"_blank\" href=\"{0}\">{0}</a>", row["myURL"]);
            }
            else
            {
                cell.Text = "Enter stuff here...";
            }

            //add the new cell to the gridview
            e.Row.Cells.AddAt(i, cell);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM