Asp.net dropdownlist getselectedvalue不会返回任何内容

时间:2014-07-30 17:29:12

标签: c# asp.net

我在asp.net中遇到了下拉列表问题。 当我尝试获取列表的选定值时,它不会返回任何内容。 aspx看起来像这样

    <div class="form-signin">
        <h2 class="form-signin-heading">Slet besked</h2>
        <div class="input-group">
            <span class="input-group-addon">ID</span>
            <asp:dropDownList runat="server" CssClass="form-control" ID="sletBox" />
        </div>
        <asp:Button runat="server" CssClass="btn btn-lg btn-block btn-danger" Text="Slet" OnClick="Slet" />
    </div>

它背后的kode看起来像这样

 protected void Slet(object sender, EventArgs e)
{
    Response.Write("wow der sker noget");
    Response.Write(sletBox.SelectedItem.Value);
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection myconnection = new SqlConnection();
    SqlCommandBuilder cmdBuilder = new SqlCommandBuilder();
    myconnection.ConnectionString = constr;
    myconnection.Open();
    string sqlcmd = "DELETE FROM messages WHERE messageid = '" + sletBox.SelectedValue.ToString() + "'";
    SqlCommand messageDelete = new SqlCommand(sqlcmd, myconnection);
    messageDelete.ExecuteNonQuery();
    myconnection.Close();
}

唯一有效的是response.write(哇)而不是selectedvalue

编辑: page_load代码

 protected void Page_Load(object sender, EventArgs e)
{
    DataTable subjects = new DataTable();

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
    {


        SqlDataAdapter adapter = new SqlDataAdapter("SELECT messageId, messageText FROM messages", con);
        adapter.Fill(subjects);

        sletBox.DataSource = subjects;
        sletBox.DataTextField = "messageText";
        sletBox.DataValueField = "messageId";
        sletBox.DataBind();



    }


    sletBox.Items.Insert(0, new ListItem("Vælg besked", ""));
}

2 个答案:

答案 0 :(得分:1)

只有在IsPostBack为false时才必须加载DropDownList数据,否则每次回发都会重新加载控件。一旦绑定到控件的数据发生更改,选定的值也会丢失。请记住,当SelectedIndexChanged事件发生时会触发Page_Load事件。

if(!Page.IsPostBack) 
{
    DataTable subjects = new DataTable();

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
    {


        SqlDataAdapter adapter = new SqlDataAdapter("SELECT messageId, messageText FROM messages", con);
        adapter.Fill(subjects);

        sletBox.DataSource = subjects;
        sletBox.DataTextField = "messageText";
        sletBox.DataValueField = "messageId";
        sletBox.DataBind();
    }

    sletBox.Items.Insert(0, new ListItem("Vælg besked", ""));
}

答案 1 :(得分:1)

您正在为dropdownlist中的pageload进行数据绑定,而不是检查IsPostBack。因此,当按钮触发clik时,它会重置selectedvalue。如下所示更改PageLoad

 if (!IsPostBack)
 {
   DataTable subjects = new DataTable();
   using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString)
   {
      SqlDataAdapter adapter = new SqlDataAdapter("SELECT messageId, messageText FROM messages", con);
      adapter.Fill(subjects);

      sletBox.DataSource = subjects;
      sletBox.DataTextField = "messageText";
      sletBox.DataValueField = "messageId";
      sletBox.DataBind();
  }

    sletBox.Items.Insert(0, new ListItem("Vælg besked", ""));
 }

作为旁注,Mathew建议尝试添加using以更好地管理您的连接对象,以便它在使用后正确处理。