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

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
AzyureAsked:
Who is Participating?
 
ZvonkoSystems architectCommented:
Yeap!
0
 
ZvonkoSystems architectCommented:
In upper snippets are nowhere Cookies used. Where from and what for do you want to use cookies?
0
 
AzyureAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
AzyureAuthor Commented:
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
 
ZvonkoSystems architectCommented:
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
 
AzyureAuthor Commented:
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
 
ZvonkoSystems architectCommented:
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
 
AzyureAuthor Commented:
ASP.NET_SessionId=00bvdbmjjre0ca552xdisq45; .UKGNL=CFE1702786AEB416B5E985CB8DEE18451EEF08F11ABEA2CE5F697C6A3FC061E54C1CF395EB2469124FC64CCAD16DAB6178CC14842146C62F654FFD2C752493AB

the cookies encrypted
0
 
AzyureAuthor Commented:
I believe the value will need to be output to the page first (hidden?) and then the javascript function called purhaps?
0
 
ZvonkoSystems architectCommented:
Sorry, then your ShowForm(num) cannot get the num.
You have to control it from the back page scripts.

0
 
ZvonkoSystems architectCommented:
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
 
ZvonkoSystems architectCommented:
And be aware that div IDs that are plain numbers are very bad choice. Add at least one alpha character as prefix.



 
0
 
ZvonkoSystems architectCommented:
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
 
AzyureAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.