0

I'm trying to add href attribute to list controls in master page in the page load function based on the type of person logged in the website.

1.first step is to make a call to database to get name of the form,module of the form(i.e, in which category it belongs), and control id to which form gets added, this result i'm storing in list.

list.Add(new Tuple<string, string, string>(dr["FormName"].ToString(), dr["ModuleName"].ToString(), dr["controlid"].ToString()));

2.now i want to find the control in master page which i have received from the database. and add href attributes to only those id's.

  foreach(var v in list)
  {
   var cont= Page.FindControl(v.Item3);
   cont.Attributes.Add("href", "~/" + v.Item2 + "/" + v.Item1);
  }

but its not working i don't know how to achieve it. neither i'm able to find the control nor it i can add attributes to it because cont.Attributes.add is not a method.

my aspx code:

                           <li id="liCRM" runat="server" class="dropdown full-length">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">CRM</a>
                            <div class="dropdown-menu">

                                <ul>
                                    <li class="custom-header text-capitalize">          
                                     <a href="#"><i class="glyphicon glyphicon-chevron-right"></i>CRM</a></li>
                                <li class="topmenu"><a id="CRM1" runat="server">Leads / Enquiries</a></li>
                                <li class="topmenu"><a id="CRM2" runat="server">Tasks / Works</a></li>

i have already tried this :

 HtmlAnchor ct = (HtmlAnchor)Page.FindControl(v.Item3);
                        ct.Attributes.Add("href","~/" + v.Item2.Substring(4) + "/" + v.Item1);

ct is always null. :( please help

Mos
  • 11
  • 1
  • 4
  • instead of marking as duplicate please try to help people. – Mos Sep 12 '19 at 18:59
  • did you try the answers from the duplicate question? https://stackoverflow.com/questions/15573505/how-to-access-master-page-control-from-content-page – CodingSlayer Sep 12 '19 at 20:27
  • i tried that ..i found control by this--> Control mycontrol=FindControl(v.Item1)..but now i want to add href attribute to this control (mycontrol) – Mos Sep 12 '19 at 20:44
  • please help any one – Mos Sep 12 '19 at 20:45
  • try HtmlAnchor ct = (HtmlAnchor)Page.Master.FindControl(v.Item3); ct.Attributes.Add("href", "~/Test.aspx"); Even though you are in Masterpage code behind, the way masterpage works, you still have to use Page.Master.Findcontrol to find it. – CodingSlayer Sep 12 '19 at 21:08
  • thank you @imAbhi thank you so much im new to asp.net..this is working – Mos Sep 12 '19 at 21:26

0 Answers0