Sakthi
Sakthi

Reputation: 103

How to get value in C# from java script?

I need to get a value that I got in Javascript to my code behind file..

My design code is:

<asp:DropDownList ID="ddlStatusOverview" runat="server" Width="95px" onchange="CheckSelectedItem(this)" >

</asp:DropDownList>

I'm using the dropwonlist in "ItemTemplate" of asp:ListView.

My javascript coding is:

<script type="text/javascript">
function CheckSelectedItem(ddl) {
alert(ddl.value);
}

I want to get that "dll.value" value in code behind.. I already used Webmethod concept. But my problem is I need to get the value in ".ascx" page.. How to do that..?

I don't know how to use hidden field value concept too..

Upvotes: 1

Views: 197

Answers (2)

Royi Namir
Royi Namir

Reputation: 148514

Do this :

Add a hidden field element :

<input name='lala' id='lala' type='hidden'/>

Add this :

function CheckSelectedItem(ddl) {
  document.getElementByID('lala').value=ddl.value;
}

On server side :

you can get the value by :

Request.Form["lala"].ToString();


Accoring to your comment :

If I want to call a server side function from javascript with this "ddl.value" means, how to do that?

Please read this

ASP.NET pass a javascript value in server side

Upvotes: 1

nphx
nphx

Reputation: 1427

Introduce a hidden field.

<script type="text/javascript">
    function CheckSelectedItem(ddl) {
        document.getElementByID('hfStatus').value = ddl.value;

        // if using jQuery
        // $("hfStatus").val(ddl.value);
    }
</script>

<asp:HiddenField runat="server" ID="hfStatus" />

Now you can directly access hfStatus from code-behind.

Upvotes: 0

Related Questions