Saeed Areffard
Saeed Areffard

Reputation: 179

DropDownList Control asp.net SelectedValue

I am Working With DropdownList Control ... First I bind the dropdown Control
in Page_Load() using this Code

   combo.DataSource = ds3.Tables[0];//combo is the ID of DropDownList Control
            combo.DataTextField = ds3.Tables[0].Columns["ProjectName"].ColumnName.ToString();
            combo.DataValueField = ds3.Tables[0].Columns["ID"].ColumnName.ToString();
            combo.DataBind();

then I use the selected Value in Button click using this Code

   protected void btn_newUser_Click(object sender, EventArgs e)
    {


    cmd1 = new SqlCommand("SP_createUserFroProjects", con);
    cmd1.CommandType = CommandType.StoredProcedure;
    int m =Convert.ToInt32( combo.SelectedValue);
    cmd1.Parameters.AddWithValue("@projectID",m);
    cmd1.Parameters.AddWithValue("@userName", txt_User.Text);
    cmd1.Parameters.AddWithValue("@password", txt_pass.Text);
    cmd1.Parameters.AddWithValue("@name", txt_User_Name.Text);

    ds4 = new DataSet();
    adpt4 = new SqlDataAdapter(cmd1);
    adpt4.Fill(ds4);

When I check the result int m is always equal to 1 in the inspect of the page the html is

<select name="combo" id="combo">
    <option selected="selected" value="1">project1</option>
    <option value="2">project2</option>
    <option value="3">project3 </option>
    <option value="4">project4 </option>
    <option value="5">lubdbsljv</option>
    <option value="10">project5</option>
    <option value="1018">test4</option>
    <option value="1019">test5 </option>
    <option value="1020">test6</option>
    <option value="1021">test7</option>
    <option value="1022">testtt</option>
    <option value="1023">new</option>
    <option value="1024">new2</option>
    <option value="1025">new5</option>
    <option value="1026">next</option>
    <option value="1027">new nnn</option>
    <option value="1028">ttttt</option>
    <option value="1029">new project 5 </option>
    <option value="1030">newprj</option>
    <option value="1031">projectnewtest</option>

</select>

but it always sends first Value from selected value in the button click

Upvotes: 0

Views: 1272

Answers (2)

Coskun Ozogul
Coskun Ozogul

Reputation: 2469

In your Page_Load method, if you don't check if it is a postback or not, your code which fills the combobox will execute every time that you click a button or any other control which postbacks the page.

It means that every time that you click on a button, the combobox fill be filled and the selected index will be 0 and the selected value will be the first value of your list.

You should check like this :

private void Page_Load()
{

 // The code here will execute on every postback (button click etc..).

  if (!IsPostBack)
   {
      //The code will execute if the page load        
   }
}

Upvotes: 1

Gagan Deep
Gagan Deep

Reputation: 1509

Try this in your code file page load method.

if (!Page.IsPostBack)
    {
        combo.DataSource = ds3.Tables[0];//combo is the ID of DropDownList Control
        combo.DataTextField = "ProjectName";
        combo.DataValueField = "ID";
        combo.DataBind();
    }

Upvotes: 1

Related Questions