Solved

dropdownlist value javascript Request.Form

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now