设置细节视图中的图像按钮在控制其他字段后可见
我有一个详细信息视图,其中有一个字段和一个图像按钮。如果该字段的值为空,我想将图像按钮设置为不可见,如果它不为空,我想看到图像按钮。
这是我的代码:
//for (int i = 0; i < DetailsView1.Fields.Count; i++)
//{
Label lbl1 = (Label)DetailsView1.FindControl("Label1");
ImageButton img = (ImageButton)DetailsView1.FindControl("ImageButton1");
if (lbl1 != null)
{
LabelABC.Text = lbl1.Text.ToString();
img.Visible = true;
}
else
{
img.Visible = false;
}
//}
我不确定这里是否需要 for 循环。我还尝试使用 .Rows[5].Cells[1].Find...
但出现超出范围的错误。
使用我在上面发布的代码,我得到的错误是:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
在行:img.Visible = false;
关于如何解决这个问题有什么建议吗?感谢您抽出时间。
I have this detailsview in which I have a field and an imagebutton. If the value of the field is empty i'd like to set the imagebutton invisible and if it's not empty I'd like to see the imagebutton.
Here's my code:
//for (int i = 0; i < DetailsView1.Fields.Count; i++)
//{
Label lbl1 = (Label)DetailsView1.FindControl("Label1");
ImageButton img = (ImageButton)DetailsView1.FindControl("ImageButton1");
if (lbl1 != null)
{
LabelABC.Text = lbl1.Text.ToString();
img.Visible = true;
}
else
{
img.Visible = false;
}
//}
I'm not sure if the for loop is needed here. I also tried working with .Rows[5].Cells[1].Find...
but I get an out of range error then.
With the code I posted above the error I get says:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
at the line: img.Visible = false;
Any tips on how to solve this please? Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的事情......它没有以其他方式找到该控件,然后您可以通过它的正常名称+行索引找到它。如果你的每一行都有一个 ImageButton 的话。那么你应该尝试用类似的 foreach 来找到它。
Try something like this... it doesn't find the control in other way, and then you can find it by it's normal name + the row index. That if you have an ImageButton for each of your rows. Then you should try to find it with a similar foreach.
您应该在调试中单步执行该代码,以确保在您尝试访问它时控件本身不为空。通常您希望在主控件的 DataBound 事件中执行此类操作。
You should step through that code in debug to make sure that the control itself is not null at the time you're trying to access it. Usually you want to do those kind of things in the DataBound events for the main control.