0

我有一个带有以下 aspx 标记的小页面

<fieldset style="width:40%; margin-left:50px;">
    <legend> Site User Role Management</legend>
    <asp:Label ID="lblSiteUserDDl" runat="server" AssociatedControlID="ddlSiteUsers"
         Text="Manage the roles in which a user is registered by selecting the user from the dropdown list below."></asp:Label>
    <asp:DropDownList ID="ddlSiteUsers" runat="server" CssClass="dropdowns" AutoPostBack="True" ClientIDMode="Static" />
    <br /><br />
    <asp:UpdatePanel ID="updplRoleChange"  runat="server" ClientIDMode="Static">
            <ContentTemplate>
                <fieldset id="rolemanagement" style="width:80%;" Visible="false" runat="server" >
                    <legend id="rolemgmtlegend" runat="server"></legend>

                                <asp:Label ID="lblCurrentRole" runat="server"  CssClass="literaltext"></asp:Label><br />
                                <asp:Label ID="lblSiteUserRole" runat="server" CssClass="literaltext"></asp:Label><br /><br />
                                <asp:RadioButtonList id="rblstRoleChange" runat="server">
                                   <asp:ListItem selected="true">Add Role to User</asp:ListItem>
                                   <asp:ListItem>Remove Role from User</asp:ListItem>                                      
                                </asp:RadioButtonList>
                                <asp:DropDownList ID="ddlUserRoles" CssClass="dropdowns" runat="server" AutoPostBack="True" ClientIDMode="Static" /><br />
                                <asp:Button ID="submitrolechange" Text="Submit Role Change" CssClass="buttons"  runat="server" ClientIDMode="Static" Visible="False" />
                </fieldset>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="ddlSiteUsers" EventName="SelectedIndexChanged"/>
                <asp:AsyncPostBackTrigger ControlID="submitrolechange" EventName="Click"/>
            </Triggers>
        </asp:UpdatePanel>
</fieldset>

和下面的 JQuery

<script>
    $(document).ready(function () {
        $('#ddlSiteUsers').change(function() {
            $(this).css({ 'color': 'black', 'font-size': '1.1em', 'font-weight': 'bold' });
        });
        //

        $('#updplRoleChange select').change(function () {
            $('#submitrolechange').show();
            $(this).css({ 'color': 'black', 'font-size': '1.1em', 'font-weight': 'bold' });
        });
    });
</script>

第一个功能有效,第二个无效。使用第一个下拉选择器后,更新面板中没有为标记生成 html,因此我什至不确定选择器 id 是否实际上保持静态。

4

2 回答 2

0

一旦您更改了第二个下拉列表(如您设置的那样AutoPostBack="True"),颜色将变为粗体,然后回传到服务器。

从服务器返回时,它不会保持先前的客户端状态,这是预期的行为。

在此处输入图像描述

如果你想保持以前的客户端状态,你需要在页面加载时做一些这样的事情。

<fieldset style="width: 40%; margin-left: 50px;">
    <legend>Site User Role Management</legend>
    <asp:Label ID="lblSiteUserDDl" runat="server" AssociatedControlID="ddlSiteUsers"
        Text="Manage the roles in which a user is registered by selecting the user from the dropdown list below."></asp:Label>
    <asp:DropDownList ID="ddlSiteUsers" runat="server" CssClass="dropdowns" AutoPostBack="True" ClientIDMode="Static">
        <asp:ListItem Text="One" Value="One" />
        <asp:ListItem Text="Two" Value="Two" />
    </asp:DropDownList>
    <br />
    <br />
    <asp:UpdatePanel ID="updplRoleChange" runat="server" ClientIDMode="Static">
        <ContentTemplate>
            <fieldset id="rolemanagement" runat="server">
                <legend id="rolemgmtlegend" runat="server"></legend>

                <asp:Label ID="lblCurrentRole" runat="server" CssClass="literaltext"></asp:Label><br />
                <asp:Label ID="lblSiteUserRole" runat="server" CssClass="literaltext"></asp:Label><br />
                <br />
                <asp:RadioButtonList ID="rblstRoleChange" runat="server">
                    <asp:ListItem Selected="true">Add Role to User</asp:ListItem>
                    <asp:ListItem>Remove Role from User</asp:ListItem>
                </asp:RadioButtonList>
                <asp:DropDownList ID="ddlUserRoles" CssClass="dropdowns" runat="server" AutoPostBack="True" 
                    ClientIDMode="Static" OnSelectedIndexChanged="ddlUserRoles_SelectedIndexChanged">
                    <asp:ListItem Text="One" Value="One" />
                    <asp:ListItem Text="Two" Value="Two" />
                </asp:DropDownList>
                <asp:Button ID="submitrolechange" Text="Submit Role Change" CssClass="buttons" runat="server" ClientIDMode="Static" Visible="False" />
            </fieldset>

        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ddlSiteUsers" EventName="SelectedIndexChanged" />
            <asp:AsyncPostBackTrigger ControlID="submitrolechange" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
</fieldset>
<script>
    $(document).ready(function () {
        $('#ddlSiteUsers').change(function () {
            $(this).css({ 'color': 'black', 'font-size': '1.1em', 'font-weight': 'bold' });
        });
        //

        $('#updplRoleChange select').change(function () {
            $('#submitrolechange').show();
            $(this).css({ 'color': 'black', 'font-size': '1.1em', 'font-weight': 'bold' });
        });

    });

    function roleChange() {
        if ($('#updplRoleChange select').val() == "Two") {
            $('#updplRoleChange select').css({ 'color': 'black', 'font-size': '1.1em', 'font-weight': 'bold' });
        }
    }
</script>

protected void ddlUserRoles_SelectedIndexChanged(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), UniqueID, "roleChange();", true);
}
于 2013-03-12T17:58:25.117 回答
0

我认为您想像这样使用 live :

$('#updplRoleChange select').live("change", function(e) {
        // Do something exciting
});
于 2013-03-12T16:44:32.243 回答