Solved

dropdownlist value javascript Request.Form

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 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