Javascript passes asp.net value

$(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 the dropdown list and put it in a shortcut. It actually shows the text on the shortcut, and when I press a button or event, I get its previous w / c value, this is ""

Can anyone help me with this.

thanks

+6
source share
1 answer

try using a 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 for

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

Source: https://habr.com/ru/post/948685/


All Articles