Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript pass value to asp.net

  $(document).ready(function () {
    $("#MainContent_ddlFieldName").live("change", function () {
                 var id = $(this).val();
                 var name = $(this + "option:selected").text();

                 $('#<%= lblValue.ClientID %>').text(name);
                 $('#<%= lblType.ClientID %>').text(id);
             });
         });


<asp:Label ID="lblValue" runat="server" Text="" Visible="true"></asp:Label>
 <asp:Label ID="lblType" runat="server" Text="" Visible="true"></asp:Label>



protected void btnSearch_Click(object sender, EventArgs e)
        {
             string strValue = lblValue.Text;
             string strType = lblType.Text;
        }

Im using javascript and Asp.Net to get the value of a dropdownlist and put it in a label. It actually show the text to the label and when i click a button or event i'm getting its previous value w/c is ""

Can anyone help me with this.

Thx

like image 898
user2530833 Avatar asked Jul 04 '13 07:07

user2530833


People also ask

Can you pass by value in JavaScript?

In JavaScript, all function arguments are always passed by value. It means that JavaScript copies the values of the variables into the function arguments. Any changes that you make to the arguments inside the function do not reflect the passing variables outside of the function.

How pass data from client side to server side in ASP NET?

To pass the value of client side, you can use javascript to pass that value to a <asp:HiddenField > first, then do a post back. At the server side you can retrive the value.


1 Answers

try to use hidden field

aspx page

<asp:HiddenField ID="hType" runat="server" ViewStateMode="Enabled" Value="" />
<asp:HiddenField ID="hValue" runat="server" ViewStateMode="Enabled" Value="" />
<asp:Label ID="lblValue" runat="server" Text="" Visible="true"></asp:Label>
<asp:Label ID="lblType" runat="server" Text="" Visible="true"></asp:Label>
<asp:Button Text="text" OnClick="btnSearch_Click" runat="server" />
<script type="text/javascript">
    $(document).ready(function () {
        $("#MainContent_ddlFieldName").live("change", function () {
            var id = $(this).val();
            var name = $(this + "option:selected").text();

            $('#<%= lblValue.ClientID %>').text(name);
            $('#<%= hValue.ClientID %>').val(name);
            $('#<%= lblType.ClientID %>').text(id);
            $('#<%= hType.ClientID %>').val(id);
        });
    });
</script>

code behind

    protected void btnSearch_Click(object sender, EventArgs e)
    { 
        //server side code
        string strValue = hValue.Value;
        string strType = hType.Value;


    }
like image 83
sangram parmar Avatar answered Sep 28 '22 08:09

sangram parmar