Anonymous Types in C#

October 22, 2008 at 11:06 pm (1)

  1. Anonymous types in C# are a new concept which shipped with .net framework 3.5
  2. Anonymous types are nothing but it simply states that we dont have to define a type and that type is defined automatically by C# analysing the right hand side.
  3. Anonymous Types are very important in the LINQ word.
  4. Keyword var is used to declare an anonymous type.
  5. They are immutable and thus dont require any property getter or setter methods
  6. They must always be initialized so that the compiler can build the type defination and bind the anonymous type with that , and it is only possible only when we have any thing in the right hand side of an anonymous type thus anonymous types should always be initialized.

//1.Shows Simple anonymous varible declarations and iterating using for loop

   1: var fabonici = new int[]{1,1,2,3,5,8,13,21}; 
   2: for( var i = 0; i<fabonici.Length; i++)  
   3:    Console.WriteLine(fabonici[i]); 
   4:  

//2.Show Simple anonymous variable declarations and iterating using foreach loop

   1: var fabonici =  new int[]{1,1,2,3,5,8,13,21}; 
   2: foreach (var fibo in fabonici) 
   3: Console.WriteLine(fibo); 

//3.Linq query used as iterand in foreach statement.

   1: var fabonici = new int[]{1,1,2,3,5,8,13,21}; 
   2: foreach (var fibo in 
   3:          from f in fabonici 
   4:          where f%3 == 0 
   5:          select f) 
   6: Console.WriteLine(fibo);

Permalink Leave a Comment

DropDownList asp.net Control problems and challanges faced using appenddatabound items and autopostbacks

October 22, 2008 at 11:01 pm (1)

Sometimes we need a solution where we have to append certain hardcoded values which are given in the markup and then we have to append some values from the database after these hardcoded values.

These hardcoded value can be a single item saying “Select” or “Remove” or a list of asp list items.

So if we are not following the right steps in the sequence of databinding and clearing items from the dropdownlist control, this control may act a little different as we would have thought and create havoc in our programming life.

Today while building an application i came accross the similar situation where i have two dropdownlists one is “ddlCategories ” where current categories to which a post belongs are coming form the datasource it alos has a listitem value which is hardcoded in the markup saying Remove Existing Categories which have a value of -1 and index value 0.

There is one more list called “ddlAddUserCategories” in which all the categories related to particular user are databound using the datasource.

Here also we are having a hardcoded value same as above but having a different text saying that Add Existing Categories….

Markup is give below

 1: <td>
 2: <asp:DropDownList ID="ddlCategories" runat="server" AutoPostBack="true" SkinID="ddlBlue"
 3: EnableViewState="true" OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged">
 4: <asp:ListItem Text="Remove Existing Categories...." Value="-1" Selected="True" />
 5: </asp:DropDownList>
 6: <span class="spanAddCategory">Remove Existing Categories</span>
 7: </td>
 8: <td>
 9: <asp:DropDownList ID="ddlUserCategories" runat="server" AutoPostBack="true" SkinID="ddlBlue"
 10: EnableViewState="true" OnSelectedIndexChanged="ddlUserCategories_SelectedIndexChanged">
 11: <asp:ListItem Text="Apply Existing Categories....." Value="-1" Selected="True" />
 12: </asp:DropDownList>
 13: <span class="spanAddCategory">Apply Existing Categories</span>
 14: </td>

Now i want that whenever i postback using the autopostback property of the dropdownlist

i should have changes reflected in both the dropdownlists.

So first of all remember to keep the datasource and databinding code sequence inside

if (!Page.IsPostBack).If you simply keep it inside the page load event,every time the page loads it will again overwrite the values and you may lead to a situation in which even if you selecte different values, then also the same value for the selectedindex is returned.So to prevent this always keep your datasource code for the dropdownlist inside if(!Page.IsPostBack)

 1: protected void Page_Load(object sender, EventArgs e)
 2: {
 3:  
 4: if (!Page.IsPostBack)
 5: {
 6:  
 7: ddlCategories.DataSource = CategoryManager.PopulateCategoriesOfPost(post.PostID);
 8: ddlCategories.DataTextField = "CategoryName";
 9: ddlCategories.DataValueField = "CategoryID"; 
 10: ddlCategories.AppendDataBoundItems = true; 
 11: ddlCategories.DataBind(); 
 12: ddlUserCategories.DataSource = CategoryManager.PopulateUserCategory(BasePage.UserID); 
 13: ddlUserCategories.DataTextField = "CategoryName"; 
 14: ddlUserCategories.DataValueField = "CategoryID"; 
 15: ddlUserCategories.AppendDataBoundItems = true; ddlUserCategories.DataBind();
 16:  
 17: }
 18:  
 19: }

Now just go through the code for ddlCategories_SelectedIndexChanged and ddlUserCategories_selectedIndexChanged

 1: protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
 2: { 
 3: int postID = Int32.Parse(Request.QueryString["postID"].ToString());
 4: string categoryName = ddlCategories.SelectedItem.ToString();
 5: CategoryManager.RemoveCategoryFromPost(categoryName, postID); 
 6: ddlCategories.Items.Clear(); 
 7: ddlCategories.Items.Add("RemoveExistingCategories...."); 
 8: ddlCategories.Items[0].Value = "-1"; 
 9: ddlCategories.DataSource = CategoryManager.PopulateCategoriesOfPost(postID); 
 10: ddlCategories.DataTextField = "CategoryName"; 
 11: ddlCategories.DataValueField = "CategoryID"; 
 12: ddlCategories.AppendDataBoundItems = true; 
 13: ddlCategories.SelectedIndex = 0; 
 14: ddlCategories.DataBind(); 
 15: } 
 16:  
 17:  
 18: protected void ddlUserCategories_SelectedIndexChanged(object sender, EventArgs e)
 19: {
 20: int postID = Int32.Parse(Request.QueryString["postID"].ToString()); 
 21: string categoryName = ddlUserCategories.SelectedItem.ToString(); 
 22: CategoryManager.AddCategoryToPost(categoryName, postID); 
 23: ddlUserCategories.SelectedIndex = 0; 
 24: ddlCategories.Items.Clear(); 
 25: ddlCategories.Items.Add("RemoveExistingCategories...."); 
 26: ddlCategories.Items[0].Value = "-1"; 
 27: ddlCategories.DataSource = CategoryManager.PopulateCategoriesOfPost(postID); 
 28: ddlCategories.DataTextField = "CategoryName"; 
 29: ddlCategories.DataValueField = "CategoryID"; 
 30: ddlCategories.AppendDataBoundItems = true; 
 31: ddlCategories.DataBind();
 32: }

In this donot forget to clear the items otherwise you will again get creepy results which are far beyond your expectations.

So the tip of the day is whenever you want to refresh your list with the new changed database values just clear the existing values and than only start inserting the most recent values from the database.

Some people will suggest to Disable the viewstate but i have tried that it can overcome the problem of appending values i.e it stops the appendingvalues which means now it will not remember your existing state but it will cause problems in the situation mentioned above so for me this is the best solution.

If you have a better solution do leave a comment.

Permalink Leave a Comment

CSS Property !important

October 22, 2008 at 11:00 pm (1)

There are basically two uses of !important css property

First use is when you want a particular style to be always applied if its also exist at some other place in the Cascading Style Sheets.

As the name suggests “Cascading Style Sheets” this means that the styles will cascade and the browser will render the styles which are applied at the last.

i.e if we have a <a> element in our markup and we define CSS like this:-

   1: a
   2: {
   3:     font-size:20px;
   4: }
   5:  

and than again at another position below than this if we define

   1: a
   2: {
   3:     font-size:40px;
   4: }
   5:  

So it will take font-size of <a> element as 40px not as 20px so if you want that for some elements the style should not change than you should give the a an !important attribute.

   1: a
   2: {
   3:     font-size:20px !important;
   4: }
   5:  

Another use of this came into existance with the CSS2.0 whenever a user gives his stylesheets properties a value containing this !important keyword than user’s stylesheet properties override the author’s style sheet properties.

Permalink Leave a Comment

PostBack Events are not firing in asp.net…………….Why????????

October 22, 2008 at 4:26 pm (1)

This seems to be a problem with all the beginners at one or the other moment in their coding profession they face this problem at one or the other stage.

One of the silly mistake due to which this problem can occur is that you have some validation controls on your page which you are initialy not showing up on the page ie there might be a situation wherein you might have various divs on a page and you might be using tabbed navigaiton to show one particular div’s contents at one time and hiding rest of the div’s.So sometimes you might have some validation controls in one particular div and you might be working on the contents of another div which doesnot have any validation controls but do have postback controls ie asp controls.

But what happens in this situation is that although the divs are hidden yet the markup is processed and loaded on the page now if you click on any postback buttons or linkbuttons or any control to do postback it will not postback because you have not met the required validation fulfilment reuqirements as the text boxes which you are using for validation might be empty  or there might be some other problem in there which is causing validation to be false, thus no postback to the server.

So if such is your scenario then just enter  a property of the asp control called “CausesValidation” to false.

Setting this will remove that button from checking that are the validation requirements fulfilled on the page and thus it will cause a postback.

Permalink Leave a Comment