ASP.NET - DropDown list problem with the selected value

Asked By mostafa hamdy on 26-Jun-12 09:15 AM
Earn up to 10 extra points for answering this tough question.
Hello All
I have some web site build in asp.net 4 and contains some web page have a dropdow list like the following:


<ASP:DropDownList ID="ddlCustomer" runat="server" CssClass="StylDropDownList1" DataSourceID="ObjectDataSource1"

 DataTextField="Name" DataValueField="ID" >

 <asp:ListItem Value="0" meta:ResourceKey="Choose" Selected="True"></asp:ListItem>

 </ASP:DropDownList>

  


<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Get" TypeName="UniSoft.ACTCRM.BusinessLayer.CustomerManager">




</asp:ObjectDataSource>


and after the data source bind this controls it becomes like the following:

<option selected="selected" value="0">Choose...</option>
<option value="1">Ahmed ALi</option>
<option value="2">Emad Ahmed</option>
<option value="5">sas</option>
<option value="7">aaaaaaaa</option>
<option value="12">test</option>
<option value="13">ali </option>
<option value="14">hamada</option>
<option value="16">alihg</option>
<option value="17">Test customer</option>
<option value="19">hamada 2000</option>
<option value="20">Mustafa Hamdy</option>
<option value="21">Esmael</option>
<option value="22">Mircosoft</option>
<option value="23">ACT</option>
<option value="24">GizaSystems</option>
<option value="25">CompuTech</option>
<option value="26">MrcoTech</option>
<option value="27">QNet</option>
<option value="28">kkk</option>
<option value="29">3www</option>
<option value="32">sama</option>
<option value="33">NewOne</option>
<option value="34">Yousry</option>
<option value="35">Lila</option>
<option value="36">Omar Ahmed</option>
<option value="37">Moshen</option>
<option value="39">yasser</option>
<option value="40">Unisoft</option>
<option value="41">mostafa Hamdy</option>
<option value="42">gdfgdfgdfg</option>
<option value="43">hhhhhhhhhhhhhhhhhhh</option>
<option value="44">mostafaHamdy123</option>
<option value="45">woooooooooooooooooooow</option>
<option value="46">ahmed abdullhameen</option>

when I save the selected value in the DB correctly I wish to display it in dropdown list control I do like the following in the page Load:




string Mode = Request.QueryString["Mode"];


string ID = Request.QueryString["ID"];


if(!Page.IsPostBack)

{

if(Mode == "Edit")

{

FillData(


int.Parse(ID));


}

}



protected void FillData(int Id)


{

devMngr = new DeviceManager().GetByID(Id);


if (devMngr != null)  

{

ddlCustomer.SelectedValue = devMngr.CustomerID.ToString()//the value from data base is 12;

}
}
but after I do the above I found that the ddlCustomer.SelectedValue is 0 ?!!!!!
please if any body get what I mean and can help me please send me or tell me about some URL may help me to solve this issue
regards
Mostafa

Jitendra Faye replied to mostafa hamdy on 26-Jun-12 09:50 AM
Are you binding DropDownList in page_Load() event if yes then bind like this-

if(!IsPostBack)
  {
    //Bind DropDownList
  }


2nd thing after setting value for dropdownList check are you rebinding again id yes then it may be possible that value is replacing,
Vikram Singh Saini replied to mostafa hamdy on 26-Jun-12 12:35 PM
Hi,

In your dropdownlist declaration:

<ASP:DropDownList ID="ddlCustomer" runat="server" CssClass="StylDropDownList1" DataSourceID="ObjectDataSource1"
 DataTextField="Name" DataValueField="ID" >
 <asp:ListItem Value="0" meta:ResourceKey="Choose" Selected="True"></asp:ListItem>
</ASP:DropDownList


you have set to keep selected the Choose value that is the 0 (zero) index value.

You shared with us, "when I save the selected value in the DB correctly I wish to display it in dropdown list control I do like the following in the page Load:"

if(!Page.IsPostBack)
{
    if(Mode == "Edit")
   {
     FillData(int.Parse(ID));
    }
}


And here is the problem, the FillData() method would be call if the Page is not posted back to the server. But for saving the selected values in database, the page is already posted back to the server. So I think the FillData() would not get chance for execution anyhow.

I think you are saving selected value in querystring ID as

string ID = Request.QueryString["ID"];

So we can modify our code as:

if (!String.IsNullOrEmpty(ID))
      {
          if(Mode == "Edit")
                   {
             FillData(int.Parse(ID));
                   }
      }


Now I hope it would work. Let us know the action result.
Chintan Vaghela replied to mostafa hamdy on 27-Jun-12 01:26 AM

Hi Frndz,

 

Functionality:  Set DropDown Value

 

 

To achieve this task,

 

When you assign Dropdown value then first you need to set SelectedIndex  value is -1

 

ddlCustomer.SelectedIndex = -1;

 

 

 

Chnage your FillData Logic as following way

 

Logic ::

 

protected void FillData(int Id)

 

{

devMngr = new DeviceManager().GetByID(Id);

 

if (devMngr != null

{

    ddlCustomer.SelectedIndex = -1;

    ddlCustomer.SelectedValue = devMngr.CustomerID.ToString();//the value from data base is 12;

}

}

 

 

 

 

Hope this helpful!

Thanks