Azyure
asked on
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.Identi ty;
UserId.Text = objIdentity.Ticket.Name;
UserType.Text = objIdentity.Ticket.UserDat a;
}
Javascript:
<script type="text/javascript" language="JavaScript">
function ShowForm(num) {
if (num == 1){
document.getElementById("1 ").style.d isplay = '';
document.getElementById("2 ").style.d isplay = 'none';
document.getElementById("3 ").style.d isplay = 'none';
document.getElementById("4 ").style.d isplay = '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("On click","ja vascript: ShowForm('1');return false;");
Company.Attributes.Add("On click", "javascript: ShowForm('2');return false;");
Supplier.Attributes.Add("O nclick", "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
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.Identi
UserId.Text = objIdentity.Ticket.Name;
UserType.Text = objIdentity.Ticket.UserDat
}
Javascript:
<script type="text/javascript" language="JavaScript">
function ShowForm(num) {
if (num == 1){
document.getElementById("1
document.getElementById("2
document.getElementById("3
document.getElementById("4
}
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("On
Company.Attributes.Add("On
Supplier.Attributes.Add("O
Contractor.Attributes.Add(
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
In upper snippets are nowhere Cookies used. Where from and what for do you want to use cookies?
ASKER
Sorry that was just to show how I am pulling the cookies into the page
- it works already -
objIdentity.Ticket.Name;
objIdentity.Ticket.UserDat a;
are the two cookie values
it's getting that value (objIdentity.Ticket.UserDa ta - which happens to be a '3') to the page and into the javascript that I need the help with please.
- it works already -
objIdentity.Ticket.Name;
objIdentity.Ticket.UserDat
are the two cookie values
it's getting that value (objIdentity.Ticket.UserDa
ASKER
I need the 'objIdentity.Ticket.UserDa ta' (3) to be the 'num' value in 'function ShowForm(num)'
sorry if this is a little confusing
cookie = objIdentity.Ticket.UserDat a = 3
into page - into javascript onload to show/hide form
:o)
sorry if this is a little confusing
cookie = objIdentity.Ticket.UserDat
into page - into javascript onload to show/hide form
:o)
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.coo kie);</scr ipt>
Tell us what you get as prompt on page load.
OK, then look wether your assumption is correct by adding this plain text html snippet anywhere on your page:
<script>alert(document.coo
Tell us what you get as prompt on page load.
ASKER
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",FormsA uthenticat ion.Encryp t(objTicke t));
Response.Cookies.Add(objCo okie);
strReturnUrl = Request.Params["ReturnURL" ];
if (strReturnUrl != null)
{
Response.Redirect(strRetur nUrl);
}
else
{
Response.Redirect("secure/ profile.as px");
}
}
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.Identi ty;
UserId.Text = objIdentity.Ticket.Name;
UserType.Text = objIdentity.Ticket.UserDat a;
}
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
protected void ProcessAuth(Int64 UserId,int UserType,int Noticeboard)
{
FormsAuthenticationTicket objTicket;
HttpCookie objCookie;
string strReturnUrl;
objTicket = new FormsAuthenticationTicket(
objCookie = new HttpCookie(".UKGNL",FormsA
Response.Cookies.Add(objCo
strReturnUrl = Request.Params["ReturnURL"
if (strReturnUrl != null)
{
Response.Redirect(strRetur
}
else
{
Response.Redirect("secure/
}
}
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.Identi
UserId.Text = objIdentity.Ticket.Name;
UserType.Text = objIdentity.Ticket.UserDat
}
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
Please do this snippet on your html page and copy the prompted output here:
<script>prompt("Cookie:",d ocument.co okie);</sc ript>
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.
<script>prompt("Cookie:",d
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.
ASKER
ASP.NET_SessionId=00bvdbmj jre0ca552x disq45; .UKGNL=CFE1702786AEB416B5E 985CB8DEE1 8451EEF08F 11ABEA2CE5 F697C6A3FC 061E54C1CF 395EB24691 24FC64CCAD 16DAB6178C C14842146C 62F654FFD2 C752493AB
the cookies encrypted
the cookies encrypted
ASKER
I believe the value will need to be output to the page first (hidden?) and then the javascript function called purhaps?
Sorry, then your ShowForm(num) cannot get the num.
You have to control it from the back page scripts.
You have to control it from the back page scripts.
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.UserDat a %>");</script>
But I would do that show and hide of that divs from the ASPX side, not from broser scripting.
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.UserDat
But I would do that show and hide of that divs from the ASPX side, not from broser scripting.
And be aware that div IDs that are plain numbers are very bad choice. Add at least one alpha character as prefix.
Also check for this function variant:
<script type="text/javascript" language="JavaScript">
function ShowForm(theId) {
for(var i=1; theDiv=document.getElement ById("id"+ i);i++){
theDiv.style.display = "none";
}
document.getElementById(th eId).style .display = "block";
}
</script>
<script type="text/javascript" language="JavaScript">
function ShowForm(theId) {
for(var i=1; theDiv=document.getElement
theDiv.style.display = "none";
}
document.getElementById(th
}
</script>
ASKER
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.Identi ty;
UserId.Text = objIdentity.Ticket.Name;
string UserType = objIdentity.Ticket.UserDat a;
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!
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.Identi
UserId.Text = objIdentity.Ticket.Name;
string UserType = objIdentity.Ticket.UserDat
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.