在RadioButtonList中更新选定的值,ASP.NET C#

时间:2016-08-05 10:47:16

标签: c# asp.net radiobuttonlist

你能帮帮我吗?我的想法是从数据库获取数据到radiobuttonlist。如下面的代码

 if (RadioButtonList2.Items.FindByValue(myReader["LO_interested"].ToString()) != null)
 {
    RadioButtonList2.SelectedValue = myReader["LO_interested"].ToString();
 }

接下来,用户可以通过选择radiobuttonlist中的另一个值来更新其先前的选择,但是在更新新的选定值之前存在一个条件。 我的问题是radioButtonList没有选择用户点击它的值,系统仍保留旧值。 这是源代码:

<asp:RadioButtonList ID="RadioButtonList2" runat="server" TextAlign="Left" ToolTip="1-Video OR 2-Text OR 3-Game"  style="margin-bottom: 0px"  >
    <asp:ListItem Value="video/mp4">video</asp:ListItem>
    <asp:ListItem Value="application/pdf">text</asp:ListItem>
    <asp:ListItem Value="game/swf">game</asp:ListItem>
</asp:RadioButtonList>

以下代码是C#

背后的代码
//update student preference and save it in database
void UpdateInfo()
{
    string interested = "";   
        if (RadioButtonList2.Items[0].Selected == true)
        { interested = "video/mp4"; }
        else if (RadioButtonList2.Items[1].Selected == true)
        { interested = "application/pdf"; }
        else if (RadioButtonList2.Items[2].Selected == true)
        { interested = "game/swf"; }
    string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["AHSConnection"].ToString();
    using (MySqlConnection cn = new MySqlConnection(connStr))
    {
        cn.Open();
        if (analyzeResultTable(lblStudentId.Text) != interested) 
        {
            Response.Write("<script>alert(' ');</script>");
            MySqlCommand cmd = new MySqlCommand("Update student SET LO_interested='" + analyzeResultTable(lblStudentId.Text) + "' where S_Id = '" + Session["StudentId"].ToString() + "'", cn);
            var i = cmd.ExecuteNonQuery();
            i = cmd.ExecuteNonQuery();
            cn.Close();
        }
        else
        {
            //Update
            MySqlCommand cmd = new MySqlCommand("Update student SET LO_interested='" + interested + "' where S_Id = '" + Session["StudentId"].ToString() + "'", cn);
            var i = cmd.ExecuteNonQuery();
            i = cmd.ExecuteNonQuery();
            cn.Close();             
        }
    }
}

这里调用更新功能:

 protected void Update_Click(object sender, EventArgs e)
 {
    UpdateInfo();
    RadioButtonList2.SelectedIndex = -1;
 }

0 个答案:

没有答案