Solved

LoginView Linkbutton not being seen by document.getElementById() ... Linkbutton does not exist in the current context

Posted on 2007-11-20
11
991 Views
Last Modified: 2013-12-17
Here is the error I recieve : The name 'LinkButton5' does not exist in the current context      
   
<script type="text/javascript" language="javascript">  
    function MouseOver1(e)
    {
switch (e);
case1:
document.getElementById('<%=LinkButton1ClientID%>').style.color = "Blue";
break;
case 2:
document.getElementById('<%=LinkButton2.ClientID%>').style.color = "Blue";
break;..........etc on the case and breaks and } below just didnt want to type them :)
document.getElementById('<%=LinkButton3.ClientID%>').style.color = "Blue";
document.getElementById('<%=LinkButton4.ClientID%>').style.color = "Blue";
document.getElementById('<%=LinkButton5.ClientID%>').style.color = "Blue"; which references a linkbutton as you can see but LinkButton5 id is within a loginview unlike 1 through 4 which are right in the aspx page not imbeded within a loginview:

<asp:LoginView ID="LoginView2" runat="server">
                            <LoggedInTemplate>
                                        <asp:LinkButton ID="LinkButton5" runat="server" ForeColor="WhiteSmoke" onmouseover="MouseOver1(5)" onmouseout="MouseOver2(5)">Blah</asp:LinkButton>

linkbuttons 1 through 4 that are in the main aspx program and not in a loginview change color when moused over without issues.... but since linkbutton5 is with that LoginView it is somehow out of context any info or code snipit that show how I can bring it back in context??

Appreciate the help.

             Thanks in advance!!
0
Comment
Question by:pugmi16
  • 6
  • 3
  • 2
11 Comments
 
LVL 3

Expert Comment

by:john_steed
ID: 20326095
Hi there,

Try using UniqueID instead of ClientID. ClientID might only return the name of the control itself, but once rendered it will get an id like parentcontrol_control.

Hope this helps
0
 
LVL 4

Expert Comment

by:kirankumar_goura
ID: 20326106
Change your JavaScript function MouseOver1 to ...
function MouseOver1(e)
{      
       document.getElementById("LinkButton"+e).style.color = "Blue";
}
and then remove "onmouseover" event from all of your <asp:LinkButton> controls and add
the following code Page_Load event ...
 LinkButton1.Attributes.Add("onmouseover", "MouseOver1(1)");
 LinkButton2.Attributes.Add("onmouseover", "MouseOver1(2)");
 LinkButton3.Attributes.Add("onmouseover", "MouseOver1(3)");
 LinkButton4.Attributes.Add("onmouseover", "MouseOver1(4)");
 LinkButton5.Attributes.Add("onmouseover", "MouseOver1(5)");

Let me know for further assistance.

- Kiran G
0
 

Author Comment

by:pugmi16
ID: 20329606
Kirankumar I added the following to the codebehind page:

    protected void Page_Load(object sender, EventArgs e)
    {
        LinkButton1.Attributes.Add("onmouseover", "MouseOver1(1)");
        LinkButton2.Attributes.Add("onmouseover", "MouseOver1(2)");
        LinkButton3.Attributes.Add("onmouseover", "MouseOver1(3)");
        LinkButton4.Attributes.Add("onmouseover", "MouseOver1(4)");
        LinkButton5.Attributes.Add("onmouseover", "MouseOver1(5)");
    }

But am getting the error: The name 'LinkButton1' does not exist in the current context
                                        The name 'LinkButton2' does not exist in the current context....etc  out to to 5

but on the page I do have the id LinkButton1, 2...etc not sure why the code behind page doesn't see it:

                            <div style="height: 40px; margin-top: 10px; margin-left: 4px;">
                                <asp:LinkButton ID="LinkButton1" runat="server" ForeColor="WhiteSmoke">Guild Charter</asp:LinkButton>
                            </div>
                            <div style="height: 40px; margin-left: 4px;">
                                <asp:LinkButton ID="LinkButton2" runat="server" ForeColor="WhiteSmoke">Guild  Rules</asp:LinkButton>
                            </div>
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:pugmi16
ID: 20329834
John I also did what yous said and used uniqueID ... I belive :) I went and compiled the project ran it and then looked at the html it contained a unique ID of (ctl00_LoginView2_LinkButton5) instead of LinkButton5. I then pasted it in:

      case 5:
        document.getElementById('<%=ctl00_LoginView2_LinkButton5.ClientID%>').style.color = "Blue";
        break;
      }

and I still get:
The name 'ctl00_LoginView2_LinkButton5' does not exist in the current context
0
 

Author Comment

by:pugmi16
ID: 20329842
I am upping this another 50 points as I appreciate you guys working with me on this current context still popping up as and issue.
0
 

Author Comment

by:pugmi16
ID: 20329858
John, Sorry I forgot to mention I changed it to uniqueid  also and it still came up with out of current context:

      case 5:
        document.getElementById('<%=ctl00_LoginView2_LinkButton5.UniqueID%>').style.color = "Blue";
        break;


The name 'ctl00_LoginView2_LinkButton5' does not exist in the current context
0
 
LVL 3

Expert Comment

by:john_steed
ID: 20330200
Hey,
Did you get an error in your original code using <%=LinkButton5.ClientID%> before ? Or did it just return a name that is not the clientname of your control ? (You can check by doing "view source" in internet explorer)
If it works but doesn't gave you the right clientname, try using  <%=LinkButton5.UniqueID%> in your original code.

Otherwise you might have to try something like LoginView2.FindControl("Linkbutton5").ClientID


0
 

Author Comment

by:pugmi16
ID: 20330490
Hey John

 I sure did... When I used <%=LinkButton5.ClientID%> it came back with LinkButton5' does not exist in the current context.

I just tried:
document.getElementById(LoginView2.FindControl("Linkbutton5").ClientID).style.color = "Blue";
and
document.getElementById(LoginView2.FindControl("Linkbutton5").UniqueID).style.color = "Blue";
and
document.getElementById('<%=LoginView2.FindControl("Linkbutton5").UniqueID%>').style.color = "Blue";

Did not Give any compile errors of being out of current context yaaaa but doesn't turn blue LOL ...

0
 
LVL 3

Accepted Solution

by:
john_steed earned 175 total points
ID: 20330539
can you tell me what this
document.getElementById('<%=LoginView2.FindControl("Linkbutton5").UniqueID%>').style.color = "Blue";
becomes (between <% and %>) when you run the page ? (you can see when doing "view source")

also look for the linkbutton5 element when viewing source and tell me what his id is

thanks
0
 
LVL 4

Assisted Solution

by:kirankumar_goura
kirankumar_goura earned 175 total points
ID: 20332684
But am getting the error: The name 'LinkButton1' does not exist in the current context
                                        The name 'LinkButton2' does not exist in the current context....etc  out to to 5

but on the page I do have the id LinkButton1, 2...etc not sure why the code behind page doesn't see it:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.>>>>>>>>>>>
When LoginView control being added onto a page and LoggedInTemplate is applied on the Control
instance, so at that time, we can only retrieve the reference of those controls like
        Control ctrl = FindControl("loginView");
        LinkButton Link5 = (LinkButton)ctrl.FindControl("Link5");
So first you have to determine whether the user has been
authenticated or not and then use the LoginView.FindControl( stringId)
together with the sub control's ID to retrieve the control reference.
0
 

Author Closing Comment

by:pugmi16
ID: 31410292
Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question