[英]GridView -> Databinding from SQL. FindControl object reference not set to an instance in RowDataBound
I am trying to change one of my columns to show a picture instead of text. 我正在尝试更改我的一列以显示图片而不是文本。 The text is currently all gathered from SQL.
该文本当前全部来自SQL。
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow dr = ((DataRowView)e.Row.DataItem).Row;
HyperLink EditLink = (HyperLink)e.Row.FindControl("EditLink");
EditLink.NavigateUrl = "javascript:ShowDialogTest('2','" + dr[0].ToString() + "')";
HyperLink DeleteLink = (HyperLink)e.Row.FindControl("DeleteLink");
DeleteLink.NavigateUrl = "javascript:ShowDialogTest('3','" + dr[0].ToString() + "')";
System.Web.UI.WebControls.Image i = (System.Web.UI.WebControls.Image)e.Row.FindControl("Overall Status");
i.ImageUrl = "url";
}
So the EditLink and DeleteLink are in the ASP and these works. 因此EditLink和DeleteLink在ASP中,并且可以正常工作。 The [Overall Status] is one field from SQL and I can't change it to an image.
[总体状态]是SQL的一个字段,我无法将其更改为图像。
I thought it may be because it hasn't bound the data yet so can't find that field but I displayed count of FindControls which = SQL results + the 2 fields. 我认为可能是因为它还没有绑定数据,所以找不到该字段,但是我显示了FindControls的数量= SQL结果+ 2个字段。
Next I thought it might be my ..FindControl(THISNAME) is wrong. 接下来,我认为可能是我的..FindControl(THISNAME)错误。 However I do not know how to find the name of the control.
但是我不知道如何找到控件的名称。 It must be something like e.Row.Controls[index].Name (I know the index) or something but I can't figure out what attribute to take.
它必须类似于e.Row.Controls [index] .Name(我知道索引)之类的东西,但是我不知道该取哪个属性。
Thanks, 谢谢,
This is usually done in the RowCreated
method of the GridView (or at least I do it this way), as fas as i know on the RowDataBound
you only have the data for the row but it doesn't exist on the page yet, to find a control in a row use e.Row.Cells[X].FindControl("ControlName")
通常,这是在GridView的
RowCreated
方法中完成的(或者至少我是这样做的),正如我在RowDataBound
上所知道的那样,您仅具有该行的数据,但页面上尚不存在该数据。使用e.Row.Cells[X].FindControl("ControlName")
在一行中找到一个控件
Hope this helps... 希望这可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.