繁体   English   中英

RadioButtonList未在UpdatePanel中更新

[英]RadioButtonList not updating in UpdatePanel

如果下拉列表中的选定值已更改,我想查询数据库并将结果输出到单选按钮列表中。 我想对此进行AJAXify,所以我添加了UpdatePanel,但是,当我在下拉列表中选择一个值时,单选按钮列表中没有任何选择。 为什么是这样?

protected void ddlUserIDs_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlUserIDs.SelectedValue.Equals("blank"))
    {
        rblAccountType.ClearSelection();
    }
    else
    {
        getAccountType();
    }
}

protected void getAccountType()
{
    string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    MySqlConnection conn = new MySqlConnection(connStr);
    MySqlDataReader reader;

    string cmdText = "SELECT role FROM users WHERE user_id=@UserID";
    MySqlCommand cmd = new MySqlCommand(cmdText, conn);
    cmd.Parameters.Add("@UserID", MySqlDbType.VarChar);
    cmd.Parameters["@UserID"].Value = Convert.ToInt32(ddlUserIDs.SelectedValue);

    try
    {
        conn.Open();
        reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            int roleID = Convert.ToInt32(reader["role"]);
            if (roleID == 0)
            {
                rblAccountType.SelectedValue = "0";
                lblAccountType.Text = "admin";
            }
            else if (roleID ==1)
            {
                rblAccountType.SelectedValue = "1";
                lblAccountType.Text= "student";
            }
            else if (roleID ==2)
            {
                rblAccountType.SelectedValue = "2";
                lblAccountType.Text = "tutor";
            }
        }
        else
        reader.Close();
    }
    catch
    {
        lblError.Text = "Database connection error - failed to get account type.";
    }
    finally
    {
        conn.Close();
    }
}

HTML:

<div id="page">
    <h2>Modify Users' Account</h2>
    <p>Please select a user ID: <br />
        <asp:DropDownList ID="ddlUserIDs" runat="server" OnSelectedIndexChanged="ddlUserIDs_SelectedIndexChanged"></asp:DropDownList>
    </p>
    <p>Account type: <asp:Label ID="lblAccountType" runat="server" Text=""></asp:Label>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
        <ContentTemplate>
        <asp:RadioButtonList ID="rblAccountType" runat="server">
            <asp:ListItem Text="Student" Value="1"></asp:ListItem>
            <asp:ListItem Text="Tutor" Value="2"></asp:ListItem>
            <asp:ListItem Text="Admin" Value="0"></asp:ListItem>
        </asp:RadioButtonList>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ddlUserIDs" EventName="SelectedIndexChanged"/>
        </Triggers>
    </asp:UpdatePanel>
    </p>
    <p><asp:Button ID="btnUpdateAccount" runat="server" Text="Update account" OnClick="btnUpdateAccount_Click" /></p>
    <p><asp:Label ID="lblError" runat="server" ForeColor="Red"></asp:Label></p>
</div>

设置DropDownList AutoPostBack = true

        <p>Account type: <asp:Label ID="lblAccountType" runat="server" Text=""></asp:Label>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
            <ContentTemplate>
 <p>Please select a user ID: <br />
            <asp:DropDownList ID="ddlUserIDs" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlUserIDs_SelectedIndexChanged"></asp:DropDownList>
        </p>
            <asp:RadioButtonList ID="rblAccountType" runat="server">
                <asp:ListItem Text="Student" Value="1"></asp:ListItem>
                <asp:ListItem Text="Tutor" Value="2"></asp:ListItem>
                <asp:ListItem Text="Admin" Value="0"></asp:ListItem>
            </asp:RadioButtonList>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="ddlUserIDs" EventName="SelectedIndexChanged"/>
            </Triggers>
        </asp:UpdatePanel>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM