设置RadioButtonList的选定值不能按预期工作

时间:2014-03-05 10:21:30

标签: c# javascript jquery asp.net

我有一个div,它根据某些条件显示或隐藏。在那Div我有一些代码如下

<div id="addPopUp" runat="server" style="padding: 30px; height: auto; width: 450px;
    overflow: hidden; display: none; background-color: White; border: 1px solid black;">
    <table width="500px;">
        <tr>
            <td valign="top" align="left" style="width: 27%;">
                <input type="hidden" runat="server" id="hfLevel" />
                <input type="hidden" runat="server" id="hfLevel1Id" />
                <input type="hidden" runat="server" id="hfLevel2Id" />
                <input type="hidden" runat="server" id="hfLevel3Id" />
            </td>
            <td style="width: 80%;">
            </td>
        </tr>
        <tr>
            <td style="width: 20%;" align="left" class="topics">
                &nbsp;
            </td>
            <td style="width: 80%;">
                <asp:RadioButtonList ID="rbtnMode" runat="server" RepeatDirection="Horizontal" CssClass="mylist"
                    AutoPostBack="true" OnSelectedIndexChanged="rtbnMode_SelectedIndexChanged">
                    <asp:ListItem Selected="True" Value="Add">Add Link</asp:ListItem>
                    <asp:ListItem Value="Edit">Edit Link</asp:ListItem>
                </asp:RadioButtonList>
            </td>
        </tr>
        <tr>
            <td style="width: 20%;" align="left" class="topics">
                <strong>Link Name :</strong>
            </td>
            <td style="width: 80%;">
                <asp:TextBox ID="txtLinkName" CssClass="txtbox" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 20%;" class="topics">
                <strong>Url :</strong>
            </td>
            <td style="width: 80%;">
                <asp:TextBox ID="txtUrl" CssClass="txtbox" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 20%;" class="topics">
                <strong>Position :</strong>
            </td>
            <td style="width: 80%;">
                <asp:TextBox ID="txtPosition" CssClass="txtbox" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 20%;" class="topics">
                <strong>Show on same page :</strong>
            </td>
            <td style="width: 80%;">
                <asp:CheckBox ID="chkSamePage" runat="server" Checked="true" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
            <td class="topics" align="left">
                <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="sfCustomButton" OnClick="btnSave_Click" />
                &nbsp;&nbsp;
                <asp:Button ID="btnHide" runat="server" Text="Hide Link" CssClass="sfCustomButton"
                    Visible="false" />
                &nbsp;&nbsp;
                <input type="button" id="btnCancel" value="Cancel" class="sfCustomButton" onclick="hide()" />
            </td>
        </tr>
    </table>
</div>

其中主要代码来到这里

<asp:RadioButtonList ID="rbtnMode" runat="server" RepeatDirection="Horizontal" CssClass="mylist"
                    AutoPostBack="true" OnSelectedIndexChanged="rtbnMode_SelectedIndexChanged">
    <asp:ListItem Selected="True" Value="Add">Add Link</asp:ListItem>
    <asp:ListItem Value="Edit">Edit Link</asp:ListItem>
</asp:RadioButtonList>

以下是一些Javascript代码:

function showPopUp(Level, Level1Id, Level2Id, Level3Id, id) {
        var radAddEdit = document.getElementById('<%=rbtnMode.ClientID %>');
        var radio = radAddEdit.getElementsByTagName("input");
        for (var i = 0; i < radio.length; i++) {
            if (radio[i].value == 'Add') {
                radio[i].checked = true;
            }           
        }     
        document.getElementById("txtLinkName").value = '';
        document.getElementById("txtUrl").value = '';
        document.getElementById("txtPosition").value = '';
        document.getElementById("chkSamePage").checked = true;
        document.getElementById("hfLevel").value = Level;
        document.getElementById("hfLevel1Id").value = Level1Id;
        document.getElementById("hfLevel2Id").value = Level2Id;
        document.getElementById("hfLevel3Id").value = Level3Id;
        document.getElementById("hfEditableId").value = id;
        Popup.showModal('addPopUp', null, null, { 'screenColor': '#cccccc', 'screenOpacity': .6 });
    }
    function hide() {       
        Popup.hide('addPopUp');  
    }
    // function BindEvents() {
    $(document).ready(function () {
        var hf = document.getElementById("hfShow");
        if (hf.value == "1") {
            Popup.showModal('addPopUp', null, null, { 'screenColor': '#cccccc', 'screenOpacity': .6 });
        }
    });

在链接上调用函数ShowPopUp以显示Div并重置其值。 从Hide调用btnCalcel来隐藏Div。 在某种情况下调用document.ready()来显示Div。

代码背后

 protected void rtbnMode_SelectedIndexChanged(Object sender, EventArgs e)
        {
            if (rbtnMode.SelectedValue == "Edit")
            {
                CatalystEntities context = new CatalystEntities();
                btnHide.Visible = true;
                btnSave.Visible = false;
                hfShow.Value = "1";
                string editValue = hfEditableId.Value;
                string levelNumber = editValue.Substring(0, 6);
                long id = Convert.ToInt64(editValue.Substring(6));
                if (levelNumber.Contains("1"))
                {
                    var linkInfo = context.Navigation_level1.
                        Where(i => i.LinkID == id)
                        .Select(i => new
                        {
                            i.LinkName,
                            i.Position,
                            i.Url
                        }).FirstOrDefault();
                    txtLinkName.Text = linkInfo.LinkName;
                    txtPosition.Text = linkInfo.Position.ToString();
                    txtUrl.Text = linkInfo.Url;
                }
            }
            else
            {
                btnSave.Visible = true;
                btnHide.Visible = false;
                hfShow.Value = "1";
                txtLinkName.Text = String.Empty;
                txtPosition.Text = String.Empty;
                txtUrl.Text = String.Empty;
            }
        }

问题

当我打开div时,转到Edit收音机,它运行正常。但是,当我点击btnHide (请记住,当前选择是编辑单选按钮)并单击其他链接时。 ShowPopUp()被调用并重置所有控件,当我点击EDIT Radio它没有回发时,我点击ADD Radio,它是的,现在,如果我点击编辑,它就会。

意味着,如果弹出窗口隐藏了选定的值Edit,则当我选择编辑收音机时,它不会首次回发。 请问我是否不清楚。 感谢

1 个答案:

答案 0 :(得分:0)

您可能需要考虑通过属性窗口或C#将控件的 AutoPostBack 属性设置为 false