Solved

ASP.net c# javascript - using cookie value to show/hide form on pageload

Posted on 2007-03-20
14
482 Views
Last Modified: 2010-04-16
Hi there,

I have an aspx page that I need to pass the value taken from the cookie - to an onload javascript function that will show/hide sections of the page.

I have all the individual sections working - but having trouble putting it all together.....

asp.net / aspx cookie:

    protected void Page_Load(object sender, EventArgs e)
    {
        FormsIdentity objIdentity;

        objIdentity = (FormsIdentity)User.Identity;

        UserId.Text = objIdentity.Ticket.Name;
        UserType.Text = objIdentity.Ticket.UserData;    
    }


Javascript:

<script type="text/javascript" language="JavaScript">
    function ShowForm(num) {
      if (num == 1){
            document.getElementById("1").style.display = '';
            document.getElementById("2").style.display = 'none';
            document.getElementById("3").style.display = 'none';
            document.getElementById("4").style.display = 'none';
      }
      else if (num == 2) {
etc etc

I originally had a radio button select to show/hide sections of the form which I called like this:

<script language="C#" runat="server">
        protected override void OnLoad(System.EventArgs e)
        {
           StdUser.Attributes.Add("Onclick","javascript: ShowForm('1');return false;");
           Company.Attributes.Add("Onclick", "javascript: ShowForm('2');return false;");
           Supplier.Attributes.Add("Onclick", "javascript: ShowForm('3');return false;");
           Contractor.Attributes.Add("Onclick", "javascript: ShowForm('4');return false;");      
            base.OnLoad(e);    
        }
</script>    
        <asp:RadioButton runat="server" ID="StdUser" GroupName="UserType" Text="User" />
        <asp:RadioButton runat="server" ID="Company" GroupName="UserType" Text="Company" />
        <asp:RadioButton runat="server" ID="Supplier" GroupName="UserType" Text="Supplier" />
        <asp:RadioButton runat="server" ID="Contractor" GroupName="UserType" Text="Contractor" />

But now I need the 'UserType' text/value from the cookie  to select which part of the form is shown/hidden

Can someone help me just put this all together?  I am using aspx pages with the script behind - c#

Thanks in advance!
Cassandra
0
Comment
Question by:Azyure
  • 8
  • 6
14 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 18755657
In upper snippets are nowhere Cookies used. Where from and what for do you want to use cookies?
0
 

Author Comment

by:Azyure
ID: 18755951
Sorry that was just to show how I am pulling the cookies into the page
- it works already -

objIdentity.Ticket.Name;
objIdentity.Ticket.UserData;  

are the two cookie values  

it's getting that value (objIdentity.Ticket.UserData - which happens to be a '3')  to the page and into the javascript that I need the help with please.
0
 

Author Comment

by:Azyure
ID: 18756175
I need the 'objIdentity.Ticket.UserData' (3) to be the 'num' value in 'function ShowForm(num)'

sorry if this is a little confusing

cookie = objIdentity.Ticket.UserData = 3

into page - into javascript onload to show/hide form

:o)
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18756351
So you believe that are cookies?
OK, then look wether your assumption is correct by adding this plain text html snippet anywhere on your page:

<script>alert(document.cookie);</script>

Tell us what you get as prompt on page load.

0
 

Author Comment

by:Azyure
ID: 18756425
I don't believe that it is a cookie - I know it is - I created it that way

protected void ProcessAuth(Int64 UserId,int UserType,int Noticeboard)
    {
        FormsAuthenticationTicket objTicket;
        HttpCookie objCookie;
        string strReturnUrl;

        objTicket = new FormsAuthenticationTicket(1, UserId.ToString(), DateTime.Now, DateTime.Now.AddDays(1), false, UserType.ToString());

        objCookie = new HttpCookie(".UKGNL",FormsAuthentication.Encrypt(objTicket));

        Response.Cookies.Add(objCookie);

        strReturnUrl = Request.Params["ReturnURL"];

        if (strReturnUrl != null)
        {            
                Response.Redirect(strReturnUrl);
        }
        else
        {
                Response.Redirect("secure/profile.aspx");
        }
    }

it's a cookie that is passed from the login page

after login - redirection to previous page or profile.aspx page takes place:

back page code:

 protected void Page_Load(object sender, EventArgs e)
    {
        FormsIdentity objIdentity;

        objIdentity = (FormsIdentity)User.Identity;

        UserId.Text = objIdentity.Ticket.Name;
        UserType.Text = objIdentity.Ticket.UserData;    
    }

front page code:

 User ID:<asp:Label ID="UserId" runat="server" Text=""></asp:Label><br />
 User Type:<asp:Label ID="UserType" runat="server" Text=""></asp:Label>

Outputs:

User ID:9
User Type:3

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18756489
Please do this snippet on your html page and copy the prompted output here:
<script>prompt("Cookie:",document.cookie);</script>

And we can talk about Grandma's cookies, back page cookies AND http browser page document.cookie String. For JavaScript are only the last cookies of intersset.

And sorry, your question is in C#, but I am answering it only from the browser JavaScript point of view.

0
 

Author Comment

by:Azyure
ID: 18756578
ASP.NET_SessionId=00bvdbmjjre0ca552xdisq45; .UKGNL=CFE1702786AEB416B5E985CB8DEE18451EEF08F11ABEA2CE5F697C6A3FC061E54C1CF395EB2469124FC64CCAD16DAB6178CC14842146C62F654FFD2C752493AB

the cookies encrypted
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Azyure
ID: 18756682
I believe the value will need to be output to the page first (hidden?) and then the javascript function called purhaps?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18756702
Sorry, then your ShowForm(num) cannot get the num.
You have to control it from the back page scripts.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18756763
What is the advantage of outputing a value, grabing that value and calling a script function with that value?
Either call the function directly like I proposed to you to call the alert() and prompt() method. The same way you can call also the ShowForm() function by posting that html text with the appropriate parameter.
<script>ShowForm("<% =  objIdentity.Ticket.UserData %>");</script>

But I would do that show and hide of that divs from the ASPX side, not from broser scripting.


0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18756790
And be aware that div IDs that are plain numbers are very bad choice. Add at least one alpha character as prefix.



 
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18756825
Also check for this function variant:

<script type="text/javascript" language="JavaScript">
function ShowForm(theId) {
  for(var i=1; theDiv=document.getElementById("id"+i);i++){
     theDiv.style.display = "none";
  }
  document.getElementById(theId).style.display = "block";
}
</script>

0
 

Author Comment

by:Azyure
ID: 18757753
Hi there,

Took your suggestion and moved the show/hide to the code-behind page - also changed it to Panels
which seems to work okay.

protected void Page_Load(object sender, EventArgs e)
    {
        FormsIdentity objIdentity;

        objIdentity = (FormsIdentity)User.Identity;

        UserId.Text = objIdentity.Ticket.Name;
        string UserType = objIdentity.Ticket.UserData;

        if (UserType == "3")
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = true;
            Panel4.Visible = false;
        }
etc etc

need to step back sometimes - easiest options are the best!
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 18757842
Yeap!
0

Featured Post

Highfive Gives IT Their Time Back

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 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

22 Experts available now in Live!

Get 1:1 Help Now