View Single Post
  #1 (permalink)  
Old December 27th, 2011, 04:58 PM
sophia sophia is offline
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile Binding dropdown list in code behind with hand coding

Hi Imar,

In the hand coding for Category and Review scenario I am binding the Dropdownlist with code behind code. Actually I want to show some arbitrary no. of extra spaces with each Category for showing nesting effect. I am even getting those spaces but when I go for the edit section of Review then the associated category is not getting assigned for that review.

And if I Bind it to EntityDataSource in mark up view and comment those binding code in code behind then I see associated category is assigned but no space is shown.

my code while binding the DDL in code behind is

Code:
public partial class Management_AddEditReviewHandCoded : System.Web.UI.Page
{
  int _id = -1;
protected void Page_Load(object sender, EventArgs e)
{
   if (!string.IsNullOrEmpty(Request.QueryString.Get(“Id”)))
   {
      _id = Convert.ToInt32(Request.QueryString.Get(“Id”)); 
   }
   if (!Page.IsPostBack && _id > -1)
   {
      using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
     {
         var review = (from r in myEntities.Reviews
                            where r.Id == _id
                            select r).SingleOrDefault();
          if (review != null)
           {
               TitleText.Text = review.Title;
               SummaryText.Text = review.Summary; 
               BodyText.Text = review.Body;
               CatList.DataBind();
               ListItem myItem =
               CatList.Items.FindByValue(review.CategoryId.ToString());
               if (myItem != null)
               {
                    myItem.Selected = true;
               }
               Authorized.Checked = review.Authorized;
           }
      }
   }
}

// I am showing spaces here in both cases Edit and Insert, so I kept it separate

if (!Page.IsPostBack)
        {
            using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
            {
                var EntireTable = from cats in myEntities.Categories
                                  select cats;

                ListItemCollection colListItemCollection = new ListItemCollection();

                var results = from cat in EntireTable
                              orderby cat.Name
                              where cat.ParentId == 0
                              select cat;

                foreach (Category objCat in results)
                {
                    ListItem objListItem = new ListItem();
                    objListItem.Text = objCat.Name;
                    objListItem.Value = objCat.Id.ToString();
                    // Add a top level item to the final collection
                    colListItemCollection.Add(objListItem);

                    AddChildren(colListItemCollection, objCat);
                }

                // Bind the final collection to the drop downs
                CatList.DataSource = colListItemCollection;
                CatList.DataTextField = "Text";
                CatList.DataValueField = "Value";
                CatList.DataBind();
            }
        }

}
further many functions go after that but those are not important here.

So how I can show spaces along with assigning associated Category to Review...

Many Thanks
Reply With Quote