0

I want to find the control(hyperlink) in the gridview. Based on the value of the control I want to enable or disable the hyperlink. I tried like this. But I am always getting null.

protected void gridResult_RowDataBound(object sender, GridViewRowEventArgs e) { 
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink  status = e.Row.FindControl("id") as HyperLink;
        if ( status != null && status.Text == "AAAA" ) {
            status.Enabled = false; 
        }
    }
}

Please help.

Tuan
  • 5,382
  • 1
  • 22
  • 17
Ranjith
  • 549
  • 4
  • 10
  • 20
  • 2
    `FindControl()` is not recursive, so if the hyperlink isn't a direct child of `Row`, then it won't be found. You may need to implement your own recursive version to get the functionality you want. See http://msdn.microsoft.com/en-us/library/486wc64h.aspx for some more info. – dlev Jun 22 '12 at 21:28
  • hello, you can send your code aspx – Aghilas Yakoub Jun 22 '12 at 21:36
  • As Aghilas stated, your ASPX code for the GridView is required if the answers below did not solve your issue. – Trisped Jun 25 '12 at 17:19

2 Answers2

2

Your "id" value is highly suspicious. My money is on the fact that you are supplying the wrong control name: FindControl("id!!!!!!!").

I would expect to see something like:

HyperLink  status = e.Row.FindControl("hlStatus") as HyperLink;

If you are indeed supplying the correct control name (yuck), then it may be that your hyperlink control is nested too deep, in which case, you will need to 'crawl' your control hierarchy looking for it.

Jeremy
  • 8,902
  • 2
  • 36
  • 44
0

@dlev is absolutely correct, controls are often nested so you will need to create your own function of sorts to find what youre looking for, you could pass this function your control collection (e.Row.Controls()) and your id

    private HyperLink FindControl(ControlCollection page, string myId)
    {
        foreach (Control c in page)
        {
            if ((HyperLink)c.ID == myId)
            {
                return (HyperLink)c;
            }
            if (c.HasControls())
            {
                FindControl(c.Controls, myId);
            }
        }
        return null; //may need to exclude this line
    }
e wagness
  • 301
  • 2
  • 13