Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

dropdownlist value javascript Request.Form

Posted on 2007-03-28
5
Medium Priority
?
748 Views
Last Modified: 2012-05-05
In c#, in the page load, I want to add the below onchange event to a dropdownlist. I need to pass the selected value of the dropdownlist to the javascript function.

I want this to work without a postback, so I am pretty sure I need to use something like Request.Form["selectStyle"], but nothing comes back. As you can see I've added alerts to help me see what is coming back and the first one returns the right value, but the other 3 that can be used on the server side return an empty alert.

selectStyle.Attributes.Add("onchange", "alert(document.getElementById('selectStyle').value); alert(" + Request.Params["selectStyle"] + "); alert(" + Request.Form["selectStyle"] + "); alert(" + Request["selectStyle"] + "); showPreview('" + Crypto.EncryptInt(Convert.ToInt32(selectStyle.SelectedValue)) + "');");      }      
0
Comment
Question by:Gezna
[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
5 Comments
 
LVL 12

Expert Comment

by:viralypatel
ID: 18813538
try this

var categoriesArray = TrendCategoriesHolder[intTrendIndex];
for ( i = 0; i <= categoriesArray.length - 1; i++)
{ var categoriesObject = categoriesArray[i];
var categoriesText = categoriesObject[0];
var categoriesValue = categoriesObject[2];
ddlTrendCategory.options[i] = new Option(categoriesText,
categoriesValue);
}
var ddlTrendType = document.getElementById("ddlTrendType");
ddlTrendCategory.options[ddlTrendCategory.options.length] = new
Option("All Categories", 0);
0
 
LVL 14

Expert Comment

by:steveberzins
ID: 18813554
I'm completely confused as to what you're trying to do.

It looks to me like you are completely confused as to the lifecycle of an .aspx page, and the difference between client vs. server side code.

I think a little more detail, maybe post your page .aspx and .cs files code, and what you envision the 'lifecycle' of this page to be, if the page is large, just strip out all the other garbage, but what you expect to be dealing with for this select control, and those directly related to this piece of functionality to are trying to create.
0
 
LVL 5

Accepted Solution

by:
mirmansoor earned 2000 total points
ID: 18814644
put this in ur page_load
selectStyle.Attributes.Add("onchange", "javaScriptFunction(this);");
the "this" keyword will give u a reference to the dropdown on the client side... then in ur javascript function will look somethin like this
function javaScriptFunction(dd)
{
alert(ddl.options[ddl.selectedIndex].value);
}
0
 

Author Comment

by:Gezna
ID: 18817884
mirmansoor,

This would work, except I need to run the Crypto.EncryptInt() function on the selected value.

Any suggestions? Obviously the below doesn't work, but something like it?

alert( <%Crypto.EncryptInt(%> ddl.options[ddl.selectedIndex].value <%);%>);
0
 
LVL 14

Expert Comment

by:steveberzins
ID: 18820259
you can't run (in a web application) c# code in the browser, on the client.

again, I don't think you understand the difference between code running on the client, and the server, and the lifecycle of your page...if you don't post back, or call somehow, code that runs on the server, you can't do what you want to do, If I'm understanding what you're trying to do anyway...

if you need to use C# functions from your client side code, you need to either live with postbacks, or look into using something like AJAX, either writing it yourself, or if you are using ASP.NET 2.0, look into ASP.NET AJAX, which will do this for you by just decorating your page with some special controls, usually just update panels, to wrap the part of the form you want to manipulate, and it will look like it is not posting back, but you can run server side code, and your page will 'look' like it is not updating from a postback, even though it is... it really is a pretty cool hack of ASP.NET, and how they make it work, with almost no effort on your part, you can make a page that looks like it is updating in place, client/server like, but it will actually be calling server side code, and only updating the parts of the page you want, with server side code, and leaving all the rest alone...
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

730 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