Solved

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

Posted on 2007-11-20
11
989 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:pugmi16
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:pugmi16
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now