开发者

set imagebutton in detailsview visible after control on other field

开发者 https://www.devze.com 2023-03-08 21:31 出处:网络
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 imagebutto

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.


protected void DetailsView1_DataBound(object sender, EventArgs e)
        {
            DetailsView dv = sender as DetailsView;
            foreach (DetailsViewRow dvr in dv.Rows) {
                ImageButton img = (ImageButton)dvr.FindControl("ImageButton1");
                img.ID = img.ID + dvr.RowIndex;
            }
        }

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号