Asp.net dropdownlist getselectedvalue doesnt return anything

Im having a problem with a dropdownlist in asp.net. When i try to get the selected value of the list it doesnt return anything. The aspx looks like this

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

And the kode behind it looks like this

 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();
}

The only thing that works is the response.write(wow) not the selectedvalue

EDIT: The page_load code

 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", ""));
}

Upvotes: 0

Views: 61

Answers (2)

Dennis R
Dennis R

Reputation: 3237

You're doing the databinding for the dropdownlist in the pageload and not checking for IsPostBack. As a result when the button triggers the clik it resets the selectedvalue. Change you PageLoad like below

 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", ""));
 }

As a side note as Mathew suggested try adding using to better manage your connections objects so it's properly disposed after it's been used.

Upvotes: 1

Marcus Vinicius
Marcus Vinicius

Reputation: 1908

You must load your DropDownList data only if IsPostBack is false, otherwise you'll be reloading the control every postback. Once the data bound to the control is changed, the selected value is lost as well. Keep in mind that the Page_Load event is fired when the SelectedIndexChanged event occurs.

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", ""));
}

Upvotes: 1

Related Questions