Solved

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

Posted on 2007-11-20
11
994 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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