简体   繁体   English

在Page_Load中设置DropDownList.SelectedIndex

[英]Set DropDownList.SelectedIndex in Page_Load

I am trying to selet the correct choice in DropDownList ddlMealType with the appropriate value depending on the selection in DropDownList ddlMeals . 我想selet在DropDownList的正确选择ddlMealType与取决于DropDownList中选择适当的值ddlMeals This works fine when I manually select a Meal, but not when the page is originally loaded (since selectedIndex = -1 for ddlMeal ). 当我手动选择一ddlMeal时,这很好用,但是最初加载页面时却没有(因为ddlMeal selectedIndex = -1)。

I therefore try to set the selected index to the first Meal in the list in Page_Load, but when adding a breakpoint on following row, I can see that the value of SelectedIndex is still -1. 因此,我尝试将选定的索引设置为Page_Load中列表中的第一个Meal,但是在下一行添加断点时,我可以看到SelectedIndex的值仍为-1。 Is it not possible to programatically set the SelectedIndex property of a dropdownList? 是否无法以编程方式设置dropdownList的SelectedIndex属性?

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlMeals.SelectedIndex = 0;
            ddlMeals_SelectedIndexChanged(this, EventArgs.Empty);
        }
   }

    protected void ddlMeals_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Fetch details for selected Meal
        SqlDataReader reader = null;
        String ConnectString = System.Configuration.ConfigurationManager.ConnectionStrings["Kunskapshjulet"].ConnectionString;
        SqlConnection connection1 = new SqlConnection(ConnectString);
        SqlCommand selectCommand = new SqlCommand("SELECT MealType FROM Meals WHERE MealID = " + ddlMeals.SelectedValue, connection1);
        try
        {
            connection1.Open();
            reader = selectCommand.ExecuteReader();

            reader.Read();
            string strMealtype = reader[0].ToString();
                ddlMealTypes2.SelectedValue = reader[0].ToString();
        }


        <asp:DropDownList ID="ddlMeals" runat="server" OnSelectedIndexChanged="ddlMeals_SelectedIndexChanged"
                      AutoPostBack="True" DataSourceID="SqlMealsPerUser" DataTextField="MealName" DataValueField="MealID" Width="180px">
        </asp:DropDownList>

您需要在PageLoad中设置SelectedIndex之前将数据绑定到DropDownList。

In your aspx code, SqlMealsPerUser is your datasource ID. 在aspx代码中, SqlMealsPerUser是您的数据源ID。 Is there any value in it? 有任何价值吗? I believe that only after the code behind Page_Load finishes, the data is bound to the control. 我相信只有在Page_Load背后的代码完成后,数据才会绑定到控件。

In that case, you could use 在这种情况下,您可以使用

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlMeals.DataSource=BindData(); // this function gets the data u need to bind to your drop down.
            ddlMeals.DataBind();
            if(ddlMeals.Items.Count > 0)
            {
                ddlMeals.SelectedIndex = 0;
                ddlMeals_SelectedIndexChanged(null, EventArgs.Empty);
            }
        }
    }

Para hacer que funcione el SelectIndex de un DropDownList en C#, hay que llamar el evento de SelectIndexChanged de la siguiente manera: 在C#中,通过SelectIndex更改了SelectIndex的功能,并在SelectIndexChanged的操作方法中添加了以下代码:

  protected void Page_Load(object sender, EventArgs e)
  {
     if (!IsPostBack)
     {
        ddlMeals.DataBind();
        if(ddlMeals.Items.Count > 0)
        {
            ddlMeals.SelectedIndex = 0;
            ddlMeals_SelectedIndexChanged(null, EventArgs.Empty);
        }
  }

Saludos!! 礼炮!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM