簡體   English   中英

如何在C#中將LiteralControl強制轉換為LinkBut​​ton

[英]How to cast LiteralControl to LinkButton in C#

protected void gvDispMsg_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        GridView hGrid = (GridView)sender;
        GridViewRow gvrRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);

        TableHeaderCell tcCellSub1 = new TableHeaderCell();
        tcCellSub1.Controls.Add(lblDDLBlank);
        gvrRow.Cells.Add(tcCellSub1);

        gvDispMsg.Controls[0].Controls.AddAt(0, gvrRow);

        LinkButton btnSort;
        System.Web.UI.WebControls.Image image;
        //iterate through all the header cells
        foreach (TableCell cell in e.Row.Cells)
        {
            //check if the header cell has any child controls
            if (cell.HasControls())
            {
                //get reference to the button column
                btnSort = (LinkButton)cell.Controls[0];
                image = new System.Web.UI.WebControls.Image();
                if (ViewState["sortExp"] != null)
                {
                    //Do something
                }
            }
        }
    }
}

我在btnSort = (LinkButton)cell.Controls[0];行中收到以下錯誤btnSort = (LinkButton)cell.Controls[0];

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.LinkButton'.

我該如何解決該錯誤。

我知道這是一個古老的問題,但是如果以后有人在搜索中找到它,這是從網格視圖投射鏈接按鈕的正確方法:

var btnSort = (LinkButton)GridView1.Rows[i].Cells[k].Controls[1];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM