代码之家  ›  专栏  ›  技术社区  ›  Bhav

UpdatePanel中未更新RadioButtonList

  •  1
  • Bhav  · 技术社区  · 11 年前

    如果下拉列表中的选定值发生更改,我想查询数据库并将结果输出到单选按钮列表中。我想将其AJAX化,所以我添加了一个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>
    
    1 回复  |  直到 11 年前
        1
  •  1
  •   andleer    11 年前

    设置 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>