【问题标题】:Javascript pass value to asp.netJavascript将值传递给asp.net
【发布时间】:2013-07-02 02:43:51
【问题描述】:
  $(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;
        }

我使用 javascript 和 Asp.Net 来获取下拉列表的值并将其放入标签中。 它实际上将文本显示到标签上,当我单击按钮或事件时,我得到它以前的值 w/c 是“”

谁能帮帮我。

谢谢

【问题讨论】:

  • var id = $(this).val();什么……?提供完整的 jqeury
  • 检查我的答案 使用隐藏字段在后面的代码中获取价值

标签: c# javascript asp.net dynamic


【解决方案1】:

尝试使用隐藏字段

aspx 页面

<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>

后面的代码

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


    }

【讨论】:

    猜你喜欢
    • 2018-09-05
    • 1970-01-01
    • 2020-12-18
    • 2012-01-07
    • 2014-01-17
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    相关资源
    最近更新 更多