p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   Binding dropdown list in code behind with hand coding (http://p2p.wrox.com/showthread.php?t=86169)

sophia December 27th, 2011 04:58 PM

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

Imar December 27th, 2011 05:10 PM

Hi there,

Since this is not related to the book, please post this in a more general ASP.NET category. When you do, be sure to provide more relevant information and code. There's too much missing to understand your problem in the first place.

Cheers,

Imar


All times are GMT -4. The time now is 06:35 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.