读书人

GridView中TemplateField里的控件的属

发布时间: 2012-01-07 21:41:55 作者: rapoo

GridView中TemplateField里的控件的属性问题
我先在GridView里添加了个TemplateField列
然后在模板编辑的ItemTemplate里加了个Image控件
也设置了GridView的DataKeyNames
但是我在用FindControl想设置Image控件的Visable属性的时候总是报:
用户代码未处理 System.NullReferenceException
Message= "未将对象引用设置到对象的实例。 "
Source= "App_Web_-8bq0x99 "
StackTrace:
在 index.SelectTreeId(String ID) 位置 d:\My Web\Visual Studio 2005\MovieWeb\IndexRight.aspx.cs:行号 59
在 index.Page_Load(Object sender, EventArgs e) 位置 d:\My Web\Visual Studio 2005\MovieWeb\IndexRight.aspx.cs:行号 26
在 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
在 System.Web.UI.Control.OnLoad(EventArgs e)
在 System.Web.UI.Control.LoadRecursive()
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


我的前台是:

<asp:GridView ID= "Show " runat= "server " CellPadding= "5 " ForeColor= "#333333 " GridLines= "None "
PageSize= "15 " Width= "100% " AutoGenerateColumns= "False " ShowFooter= "True " DataKeyNames= "ID " OnDataBound= "Show_DataBound " >
<FooterStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<RowStyle BackColor= "#EFF3FB " Width= "100% " />
<EditRowStyle BackColor= "#2461BF " />
<SelectedRowStyle BackColor= "#D1DDF1 " Font-Bold= "True " ForeColor= "#333333 " />
<PagerStyle BackColor= "#2461BF " ForeColor= "White " HorizontalAlign= "Center " />


<HeaderStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<AlternatingRowStyle BackColor= "White " />
<Columns>
<asp:TemplateField HeaderText= "123 ">
<ItemTemplate>
<asp:Image ID= "NewImage " runat= "server " ImageUrl= "Image/New.gif " Visible= "False " />
</ItemTemplate>
</asp:TemplateField>
<asp:ImageField DataAlternateTextFormatString= "ImagePath:{0} " DataImageUrlField= "ImagePath ">
</asp:ImageField>
<asp:HyperLinkField DataTextField= "MovieName " DataNavigateUrlFields= "ID " DataNavigateUrlFormatString= "ShowDetails?id={0} " />
<asp:BoundField DataField= "MuluType " />


<asp:BoundField DataField= "CreateTime " DataFormatString= "{0:d} " />
<asp:BoundField DataField= "Clicknum " />
<asp:BoundField DataField= "Actor " />
</Columns>
</asp:GridView>


后台:


Adapter.Fill(DS, "_Main ");
Show.DataSource = DS.Tables[ "_Main "].DefaultView;
Show.DataBind();
if (DS.Tables[ "_Main "].Rows.Count > 0)
{
for (int i = 0; i < DS.Tables[ "_Main "].Rows.Count; i++)
{
if (DS.Tables[ "_Main "].Rows[i][12].ToString().Trim() == "1 ")
{

//就这句报错:
((Image)(this.Show.TemplateControl.FindControl( "NewImage "))).Visible = true;
}
else
{
//((Image)(Show.TemplateControl.FindControl( "NewImage "))).Visible = false;
}


}

return true;
}
else
{
return false;
}

帮帮忙吧...卡住一个晚上了....

[解决办法]
你的什么事件写的,要判断

if (e.Row.RowType == DataControlRowType.DataRow)
{

}
[解决办法]
this.Show.TemplateControl.
——————————————————————————————————————————
哪学的?回忆一下为什么会这样。

网上google一些正常的程序示例。

读书人网 >asp.net

热点推荐