?
Solved

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

Posted on 2007-03-20
14
Medium Priority
?
492 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
[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
  • 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
Technology Partners: 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!

 
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
 

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 1500 total points
ID: 18757842
Yeap!
0

Featured Post

Independent Software Vendors: 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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

764 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