簡體   English   中英

Bootstrap ASP.Net按鈕組在回發時保留值

[英]Bootstrap ASP.Net button group retain value on postback

我有一個帶有三個單選按鈕的Bootstrap按鈕組。 我也有一個隱藏字段,因此可以在我的代碼中獲取所選的值。 按鈕的代碼為:

<div class="btn-group" data-toggle="buttons" id="parentofit">
    <asp:HiddenField ID="hidValue" runat="server" Value="Beginning" />
    <label class="btn btn-primary" id="label1" runat="server">
        <input type="radio" name="options" id="option1" runat="server">
        Radio 1
    </label>
    <label class="btn btn-primary" id="label2" runat="server">
        <input type="radio" name="options" id="option2" runat="server">
        Radio 2
    </label>
    <label class="btn btn-primary" id="label3" runat="server">
        <input type="radio" name="options" id="option3" runat="server">
        Radio 3
    </label>
</div>

我也有Javascript來設置隱藏字段:

$(document).ready(function () {
    $('.btn').click(function () {
        var parent = '#' + $(this).parent().attr('id');
        $(parent).find('input').val($(this).text());
        $(this).addClass("active").siblings().removeClass("active");
    });

當我提交頁面時,隱藏字段具有正確的值。 重新加載頁面獲取后,按鈕組將重置為第一個字段。

如何保留回發的價值? 最初如何通過編程設置該值?

TIA

傑夫

您可以捕獲Form Post數據並在頁面加載中手動設置單選按鈕的值。

if (Page.IsPostBack && Request.Form["options"] != null)
{
    HtmlInputRadioButton hirb = new HtmlInputRadioButton();
    hirb.Value = Request.Form["options"];
}

一個更復雜的解決方案是創建您自己的控制適配器以覆蓋單選按鈕的設計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM