Solved

dropdownlist value javascript Request.Form

Posted on 2007-03-28
5
745 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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