C# 为什么e.findcontrol返回null
我试图在行数据绑定的gridiview模板字段中找到一个服务器控件,但它返回null。为什么? 我试图找到一个控件,该控件具有runat属性,但返回null 实际上,我正在尝试在这个控件中加载HTML文本C# 为什么e.findcontrol返回null,c#,asp.net,.net,gridview,webforms,C#,Asp.net,.net,Gridview,Webforms,我试图在行数据绑定的gridiview模板字段中找到一个服务器控件,但它返回null。为什么? 我试图找到一个控件,该控件具有runat属性,但返回null 实际上,我正在尝试在这个控件中加载HTML文本 protected void gvBidDetails_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType == DataControlRowType.Dat
protected void gvBidDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var paraAgreement = e.Row.FindControl("paraAgreement") as Literal;
paraAgreement.Text = "sahdbaskjdbasjbdjabsdbhasdbhasjbhdab<br/>sdbasdbhasdjabjdbasjdbjasbhdashhhdbasdbhab<br/>shdbhashbdashbdashdabshdbhasdbjabsdabsdbhasbhdashdbasd";
}
}
catch (Exception ex)
{
Utility.Msg_Error(Master, ex.Message);
}
}
受保护的void gvBidDetails\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
尝试
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
var paraAgreement=e.Row.FindControl(“paraAgreement”)作为文本;
paraAgreement.Text=“SAHBASKJDBASJBDJABSDBHASBHASBASJBHDAB
SDBASDBHASDJADBJDBASJDBJABSBHDASHHDBASDBHAB
SHDBHASHBDASHBADSHDBHABSDBHASBHASBHASBDASHDBHASBDASHDBHABSDBHASBHASBDASHDBHASDBASD”;
}
}
捕获(例外情况除外)
{
实用程序.Msg_错误(主程序,例如消息);
}
}
.aspx
-->
您正在使用FindControl
搜索文本。但是paraAgreement
不是实际的asp:Literal
控件。所以它总是返回null
改用HtmlGenericControl
HtmlGenericControl p = e.Row.FindControl("paraAgreement") as HtmlGenericControl;
p.InnerHtml = "Found the Control";
您正在使用FindControl
搜索Literal
。但是paraAgreement
不是实际的asp:Literal
控件。所以它总是返回null
改用HtmlGenericControl
HtmlGenericControl p = e.Row.FindControl("paraAgreement") as HtmlGenericControl;
p.InnerHtml = "Found the Control";
编辑1:
您试图将FindControl
结果强制转换为Literal
,由于下面的HTML段落服务器控件被识别为System.Web.UI.HtmlControls.HtmlGenericControl
和as
关键字InvalidCastException
,因此返回null:
无法强制转换类型为的对象
“System.Web.UI.HtmlControls.HtmlGenericControl”以键入
'System.Web.UI.WebControls.Literal'
因此,FindControl
赋值应如下示例所示:
protected void gvBidDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var paraAgreement = e.Row.FindControl("paraAgreement") as HtmlGenericControl;
paraAgreement.InnerText = "[sample text]";
}
}
catch (Exception ex)
{
Utility.Msg_Error(Master, ex.Message);
}
}
按控件ID查找嵌套子控件的另一种方法是使用自定义方法在父控件中执行递归搜索(归功于@Win):
//摘自/a/1570885/6378815
公共静态控件FindControlRecursive(控件parentControl,字符串id)
{
if(parentControl.ID==ID)
{
返回控制;
}
返回parentControl.Controls.Cast().Select(c=>FindControlRecursive(c,id)).FirstOrDefault(c=>c!=null);
}
//GridView事件方法
受保护的void gvBindDetails_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
尝试
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
var paraAgreement=新的HtmlGenericControl();
foreach(gvBindDetails.Rows中的控件)
{
paraAgreement=FindControlRecursive(控制,“paraAgreement”)作为HtmlGenericControl;
}
paraAgreement.InnerText=“[sample text]”;
}
}
捕获(例外情况除外)
{
实用程序.Msg_错误(主程序,例如消息);
}
}
附加参考:
(MSDN)
相关问题:
编辑1:
您试图将FindControl
结果强制转换为Literal
,由于下面的HTML段落服务器控件被识别为System.Web.UI.HtmlControls.HtmlGenericControl
和as
关键字InvalidCastException
,因此返回null:
无法强制转换类型为的对象
“System.Web.UI.HtmlControls.HtmlGenericControl”以键入
'System.Web.UI.WebControls.Literal'
因此,FindControl
赋值应如下示例所示:
protected void gvBidDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var paraAgreement = e.Row.FindControl("paraAgreement") as HtmlGenericControl;
paraAgreement.InnerText = "[sample text]";
}
}
catch (Exception ex)
{
Utility.Msg_Error(Master, ex.Message);
}
}
按控件ID查找嵌套子控件的另一种方法是使用自定义方法在父控件中执行递归搜索(归功于@Win):
//摘自/a/1570885/6378815
公共静态控件FindControlRecursive(控件parentControl,字符串id)
{
if(parentControl.ID==ID)
{
返回控制;
}
返回parentControl.Controls.Cast().Select(c=>FindControlRecursive(c,id)).FirstOrDefault(c=>c!=null);
}
//GridView事件方法
受保护的void gvBindDetails_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
尝试
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
var paraAgreement=新的HtmlGenericControl();
foreach(gvBindDetails.Rows中的控件)
{
paraAgreement=FindControlRecursive(控制,“paraAgreement”)作为HtmlGenericControl;
}
paraAgreement.InnerText=“[sample text]”;
}
}
捕获(例外情况除外)
{
实用程序.Msg_错误(主程序,例如消息);
}
}
附加参考:
(MSDN)
相关问题:
您的控件在面板内,然后在Div控件中,尝试先查找Panel,然后查找Div,然后查找id为paraAgreement的p。我认为FindControl
只搜索由父容器直接包含的控件,而不搜索嵌套控件的整个层次结构。有关详细信息,请参阅。您的控件位于面板内,然后位于Div控件中,请尝试先查找Panel,然后查找Div,然后查找id为paraAgreement的p。我认为FindControl
只搜索直接由父容器包含的控件,而不是整个嵌套控件的层次结构。有关详细信息,请参阅。