Gridview dataBound从后面的代码添加到列数据的超链接

时间:2015-07-24 06:43:42

标签: c# databound rowdatabound itemdatabound databound-controls

使用网格视图从后面的代码绑定它:

我想将特定列数据绑定到超链接中,所以当它点击它时应该进行下载。

怎么做?

以下是我的代码:

for (int i = 0; i <= tbl.Columns.Count - 1; i++) 
{
    Telerik.Web.UI.GridBoundColumn boundfield = new Telerik.Web.UI.GridBoundColumn();

    if (tbl.Columns[i].ColumnName.ToString() == "Row") 
    {

        LinkButton lkbtn = new LinkButton();
        lkbtn.CommandName = i;
        lkbtn.CommandArgument = "dwnld";
        lkbtn.Font.Underline = true;
        lkbtn.Text = tbl.Columns(i).ColumnName.ToString();

        boundfield.DataField = tbl.Columns(i).ColumnName.ToString()
        boundfield.HeaderText = tbl.Columns(i).ColumnName.ToString();

        GridView2.MasterTableView.Columns.Add(boundfield);

    }
}

1 个答案:

答案 0 :(得分:0)

为什么不使用带有链接按钮的网格模板列。

<telerik:GridTemplateColumn>
       <ItemTemplate>
          <asp:LinkButton ID="btnDownload" OnClick="btnDownload_Click" runat="server">Download Something</asp:LinkButton>
        </ItemTemplate>
   </telerik:GridTemplateColumn>


protected void btnDownload_Click(object sender, EventArgs e)
{
  LinkButton lbBtn = sender as LinkButton;
  GridDataItem item = (GridDataItem)(sender as LinkButton).NamingContainer;
  // Use item to get other details
...
...
}