locked
A problem with a control DropDownList in ASP.NET with C# RRS feed

  • 問題

  •  

    Hi everyone!

     

    I'm Florin G and I have a problem.

     

    My problem is:

     

    I have made an aspx page with a table in ASP.NET with C#.

     

    In this table I put some controls like label, textbox and a DropDownList.

     

    On Page_load method I load my DropDownList control

    (dropDown_DepartamentPers) with some data like 'FINANCIAL', 'ADMINISTRATIV', 'IT', 'COUNT' and 'DIVERS'.

     

    When I run the application to test it I load my page. First in my page in my control DropDownList appears ADMINISTRATIV (of course because is sorted load).

     

    My big problem is: I select my DropDownList and I change the value (for sample I choose FINANCIAL.)

    But when I click on the button Save my control DropDownList has always ADMINISTRATIV.

     

    I put a breakpoint and I debug the problem and after I change other value like FINANCIAL or IT it doesn’t work because dropDown_DepartamentPers has ADMINISTRATIV. Why?

     

    Why?

     

    My code aspx is:

    <asp:DropDownList ID="dropDown_DepartamentPers" runat="server"

    Style="position: relative; "

    Width="270px" >

    </asp:DropDownList>

     

    and in C# the code where I load my control DropDownList is:

    strComSql = "SELECT Departament FROM Departament ORDER BY Departament";

    daDepartamente = new SqlDataAdapter(strComSql, dbDateLiveBox);

    dtDepartamente = new DataTable("Departamente");

     

    daDepartamente.Fill(dtDepartamente);

     

    //Load the departaments ...

    dropDown_DepartamentPers.Items.Clear();

    foreach (DataRow drow in dtDepartamente.Rows)

    dropDown_DepartamentPers.Items.Add(drow["Departament"].ToString().Trim());

     

    Please help me!

    Thank you very much!

     

     

    2008年10月14日 上午 08:35

解答

  • Hi Florin,

     

    Well, as you said, you found out that the General ASP.NET Page life cycle, it is Page Load before the Control Event Handler. So if you do inserting the items within the page_load, you have to make sure that the dropDownList won't be clear and insert again. You were then try to use an IF block to test if the items.Count == 0. Well, Generally, we should use

     

    Code Snippet

    If (!Page.IsPostBack)

    {

    //Do you init on the controls.

    }

     

     

     

    2008年10月20日 上午 02:52

所有回覆

  • Hi,

     

    I have found the answer. my problem it was to me because I have made a mistake.

     

    On Page_load I was loading my drop_downlist in this mode:

     

    dropDown_LocatiePers.Items.Clear();

    foreach (DataRow drow in dtLocatii.Rows)

    dropDown_LocatiePers.Items.Add(drow["Den_LocSecundar"].ToString().Trim());

     

    But in this mode it isn't correct because when I click on Sqve button it executes first Page_load and then the metod for OnClick_btnSave. So in Page_load correct is the next code:

    //Load dropDown_LocatiePers

    if (dropDown_LocatiePers.Items.Count == 0)

    {

    foreach (DataRow drow in dtLocatii.Rows)

    dropDown_LocatiePers.Items.Add(drow["Den_LocSecundar"].ToString().Trim());

    }

     

     

    Thanks anyway,

    2008年10月14日 上午 09:16
  • Hi Florin,

     

    Well, as you said, you found out that the General ASP.NET Page life cycle, it is Page Load before the Control Event Handler. So if you do inserting the items within the page_load, you have to make sure that the dropDownList won't be clear and insert again. You were then try to use an IF block to test if the items.Count == 0. Well, Generally, we should use

     

    Code Snippet

    If (!Page.IsPostBack)

    {

    //Do you init on the controls.

    }

     

     

     

    2008年10月20日 上午 02:52