【问题标题】:How to have radio button selected after page refresh页面刷新后如何选择单选按钮
【发布时间】:2012-09-19 03:34:25
【问题描述】:

我有一个单选按钮列表,当用户选择一个按钮时,页面会重新加载并显示正确的信息,但该按钮已消失。

有没有办法避免这种情况?

这是 HTML:

        <div class="checkBoxesColumn">
            <label><input class="videoWallRadioCls" name="videoWallRadio" type="radio" id="viewAll" value="0" runat="server" />View All</label>
            <label><input class="videoWallRadioCls" name="videoWallRadio" type="radio" id="journey" value="1" runat="server"  />My MBC Journey</label>
            <label><input class="videoWallRadioCls" name="videoWallRadio" type="radio" id="challenges" value="2" runat="server"  />Challenges</label>
            <label><input class="videoWallRadioCls" name="videoWallRadio" type="radio" id="advice" value="3" runat="server"  />Positive Outlooks &amp; Advice</label>
            <label><input class="videoWallRadioCls" name="videoWallRadio" type="radio" id="thanks" value="4" runat="server"  />Giving Thanks</label>
            <label><input class="videoWallRadioCls" name="videoWallRadio" type="radio" id="perspective" value="5" runat="server"  />Family / Friend Perspective</label>
        </div>

这里是代码隐藏:

protected void Page_Load(object sender, EventArgs e)
    {
        // HIDE UNTIL 10/13
        if (DateTime.Now > new DateTime(2012, 9, 12))
        {
        mbcRadioList.Visible = true;
        }

    video myVideo = new video();

    string categoryID = "0";
    if (!string.IsNullOrEmpty(Request["category_id"])) categoryID = Request["category_id"];

    Hashtable ht = myVideo.ListingForWall(categoryID);
    //Response.Write("Test: " + ht.Count);
    //Response.Write("Test: " + ht["B-01"]);

    StringBuilder myStringbuilder = new StringBuilder();

    for (int i = 1; i <= 36; i++)
    {
        string iStr = i.ToString();

        if (iStr.Length == 1) iStr = iStr.PadLeft(2, '0');

        //Response.Write("A-" + iStr + "<br />");             
        //Response.Write("TEST: " + ht["A-" + iStr] + "<br />");

        string videoClass = "videoWallThumb thumb" + iStr;

        if (ht["A-" + iStr] != null)
        {
            string[] tmp = ht["A-" + iStr].ToString().Split('|');

            if (tmp[2] == "0") videoClass += " disabled ";

            myStringbuilder.AppendLine("<a class=\"" + videoClass + "\" href=\"videoWallDetail.aspx?video_id=" + tmp[0] + "\"><img src=\"images/video_images/" + tmp[1] + "\" alt=\"\"/></a>");
        }
        else
        {
            videoClass += " incomplete ";
            myStringbuilder.AppendLine("<a class=\"" + videoClass + "\" href=\"#\"><img src=\"images/placeholder.jpg\" alt=\"\"/></a>");
        }

    }

    mosaicA.Text = myStringbuilder.ToString();
}

如果我提供的代码不够或令人困惑,请告诉我,我正在努力解决这个小问题,以便继续处理下一个问题。

提前致谢!

【问题讨论】:

    标签: c# .net radio-button


    【解决方案1】:

    我认为这是一个 ViewState 问题。只有 Web 控件维护了视图状态,即在回发之间保留信息。

    检查这个: Which controls have ViewState maintained?

    【讨论】:

      【解决方案2】:

      您是否尝试在选中复选框时设置会话变量?这样在页面重新加载时,您可以看到选择了哪个并将其设置为 true。

      因此,您可以尝试使用单选按钮的 Id 设置会话变量,当它返回时,您读取变量并将会话变量持有的任何 Id 设置为 true。

      我不确定是否有更好的方法来解决这个问题,但这是我首先想到的。

      我唯一可以建议的另一件事是,如果您要使用 AJAX 调用进行部分更新,这样整个网页就不会重新加载。

      【讨论】:

        【解决方案3】:

        没关系,我在 category_id 上使用了 switch case...

            switch ((Request["category_id"]))
            {
                case "0": viewAll.Checked = true; break;
                case "1": journey.Checked = true; break;
                case "2": challenges.Checked = true; break;
                case "3": advice.Checked = true; break;
                case "4": thanks.Checked = true; break;
                case "5": perspective.Checked = true; break;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-24
          • 2021-07-27
          • 2017-06-02
          • 2014-03-16
          • 1970-01-01
          • 1970-01-01
          • 2018-10-27
          • 2013-01-21
          相关资源
          最近更新 更多