开发者

Having 2 dropdownlist problem

开发者 https://www.devze.com 2023-03-18 03:33 出处:网络
I have 2 dropdownlist and I needed a default value to appear as the 1st item but one of then ddlSize isn\'t getting the default value, I also tried to create 2 separate functions TypeColor & TypeS

I have 2 dropdownlist and I needed a default value to appear as the 1st item but one of then ddlSize isn't getting the default value, I also tried to create 2 separate functions TypeColor & TypeSize but it didn't help, any advice? Thanks:

 &nbsp;<asp:DropDownList ID="ddlColor" OnDataBinding='<%# TypeSelection((int)Eval("ProductID")) %>' runat="server" Width="109">
   <asp:ListItem Value="Select Color&nbsp;&nbsp;"></asp:ListItem>
   </asp:DropDownList>
   <br />
   &nbsp;<asp:DropDownList ID="ddlSize" OnDataBinding='<%# TypeSelection((int)Eval("ProductID")) %>' runat="server" Width="109">
   <asp:ListItem Value="Select Size&nbsp;&nbsp;"></asp:ListItem>
   </asp:DropDownList>


  protected string TypeSelection(int pID)
        {
            DropDownList ddlColor = (DropDownList)FormView_Product.Row.Cells[0].FindControl("ddlColor");
            DropDownList ddlSize = (DropDownList)FormView_Product.Row.Cells[0].FindControl("ddlSize");
            CommerceEntities db = new CommerceEntities();

            ddlColor.DataSource = from p in db.ProductTypes
                                 where p.ProductID == pID
                                  orderby p.Color 
                                 select new { p.ProductID, p.Color };
            ddlColor.DataTextField = "Color"; 
            ddlColor.Items.Insert(0, new ListItem("Select Color", "NA")); //----->Default value

            ddlSize.DataSource = from p in db.ProductTypes
                                 where p.ProductID == pID
                                 orderby p.Size descending
                                 select new { p.ProductID, p.Size };
     开发者_开发问答       ddlSize.DataTextField = "Size";
            ddlSize.Items.Insert(0, new ListItem("Select Size", "NA")); //----->Default value(can't get this into the DDL.)

            return null;
        }


You can do something like that.

<asp:DropDownList runat="server" ID="ddl" AppendDataBoundItems="true">
        <asp:ListItem Value="0" Text="Default Value"></asp:ListItem>
</asp:DropDownList>

This way the default item will be kept there after binding.


Call ddlSize.DataBind(); before inserting the Default value. e.g.

ddlSize.DataTextField = "Size";
ddlSize.DataBind(); // Add this
ddlSize.Items.Insert(0, new ListItem("Select Size", "NA"));

The second thing is not related to your problem, but I need to tell you that you don't need to define your method return type as String. Since you are not returning any value from this method, simply make this method return type Void


Try to call ddlSize.DataBind() and ddlColor.DataBind() before inserting the default item.

ddlColor.DataSource = ...
ddlColor.DataTextField = "....";
ddlColor.DataBind();
ddlColor.Items.Insert(0, new ListItem("Select Color", "NA"));

And so on ...

0

精彩评论

暂无评论...
验证码 换一张
取 消