从asp.net Web表单后面的代码中获取Select2选择的值。

时间:2022-11-27 19:37:09

I have a problem with Select2 V 4.02

我对Select2 v4.02有问题

Here is my code

这是我的代码

<select id="MySelect" class="form-control" runat="server" ClientIDMode="static">
    <option></option>
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
</select>
<asp:Button Text="Show Select2 Result" runat="server" ID="btnShow" OnClick="btnShow_Click"/>

jQuery:

jQuery:

$('#MySelect').select2({
        placeholder: "-Select-"
});

My question is: Can I get the "MySelect" selected value from ASP .Net Code behind? I tried this code from code behind asp .net webform

我的问题是:我能从ASP .Net代码中获得“MySelect”选择的值吗?我从asp.net webform后面的代码中尝试了这段代码

protected void btnShow_Click(object sender, EventArgs e)
{
    Response.Write(MySelect.Value);
}

But it returns empty string.

但它返回空字符串。

1 个解决方案

#1


4  

you can use standard hidden field like this :

您可以使用标准的隐藏字段如下:

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

then in your JavaScript :

然后在JavaScript中:

$('#MySelect').on('change', function () {
   $('#<%=hf1.ClientID%>').val($(this).val());
}

in your code behind :

在你的代码后面:

protected void btnShow_Click(object sender, EventArgs e)
{
    Response.Write(hf1.Value);
}

i hope working for you

我希望为你工作

#1


4  

you can use standard hidden field like this :

您可以使用标准的隐藏字段如下:

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

then in your JavaScript :

然后在JavaScript中:

$('#MySelect').on('change', function () {
   $('#<%=hf1.ClientID%>').val($(this).val());
}

in your code behind :

在你的代码后面:

protected void btnShow_Click(object sender, EventArgs e)
{
    Response.Write(hf1.Value);
}

i hope working for you

我希望为你工作