Search code examples
c#asp.netasp.net-ajaxupdatepanelradiobuttonlist

RadioButtonList not updating in UpdatePanel


If the selected value in a dropdownlist is changed, I want to query a database and output the result in a radiobuttonlist. I want to AJAXify this so I've added an UpdatePanel, however, when I select a value in the dropdownlist, nothing gets selected in the radiobuttonlist. Why is this?

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>

Solution

  • Set the 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>