对象引用未设置为对象的实例 [英] object refrence not set to an instance of and object
本文介绍了对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好先生,
i有数据控制权。
hello sir,
i have datalist control.
<asp:DataList ID="DataList1" runat="server"/>
<HeaderTemplate>
CATEGORY</HeaderTemplate>
<itemstyle backcolor="White" forecolor="Black" borderwidth="2px" />
<itemtemplate>
<asp:Image ID="Image1" runat="server" Height="150px" Width="100px" ImageUrl='<%# "hangler_reward.ashx?category=" + Eval("category") %>' style="padding-left:40px;"/><br />
CATEGORY NAME
<asp:LinkButton ID="lnkcat" runat="server" Text='<%# Bind("CATEGORY") %>'
onclick="lnkcat_Click">
<br />
</itemtemplate>
这是linkbutton click事件。
this is linkbutton click event.
protected void lnkcat_Click(object sender, EventArgs e)
{
// string text = ((LinkButton)DataList1.FindControl("lnkcat")).Text;
string text = ((LinkButton)DataList1.FindControl("lnkcat")).Text;
}
i想找到链接按钮text但是有错误
对象refrence未设置为对象的实例
i want to find link button "text" but there is error
"object refrence not set to an instance of and object"
推荐答案
因为DataList1.FindControl(lnkcat)返回null:DataList1找不到名为lnkcat的控件 -
甚至更糟:DataList1可能为空。
为什么不使用click事件的sender参数?
Something喜欢
Because DataList1.FindControl("lnkcat") returned null: DataList1 did not find a control with the name "lnkcat" -
or even worse: DataList1 could be null.
Why don't you use the sender parameter of the click event?
Something like
protected void lnkcat_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
if (btn == null)
{
// write it to the log or deal somehow with it, then
return;
}
string text = btn.Text;
}
string text ="";
LinkButton lbtn= (LinkButton )this.DataList1.FindControl("lnkcat");
text = lbtn.Text.toString();
这篇关于对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文