Archive

Archive for November, 2010

Three Ways to Bind DropdownList Using Linq

November 4, 2010 Leave a comment

Hi all,

Try this example to show you Three Ways to Bind DropdownList Using Linq

Step1 :- Open VS2008 and create a new website

Step2 :- add new Linq to SQL DataContext  and name it “Northwind.dbml” and add Categories table in it

Step3 :- Add new Web page and fromDropdownList control inside page

<div>
    Select Category :-
        <asp:DropDownList ID="ddl_Category" runat="server" AppendDataBoundItems="True">
            <asp:ListItem Value="-1">-- Please Select --</asp:ListItem>
        </asp:DropDownList>
    </div>

Step4 :- In Code behind and the following code which will be used to Bind DropdownList

— First Way :-


private void BindCategories()
    {
        try
        {
            NorthwindDataContext dbml = new NorthwindDataContext();
            var str = "select * from Categories ";
            var category = dbml.ExecuteQuery<Category>(str);
            ddl_Category.DataTextField = "CategoryName";
            ddl_Category.DataValueField = "CategoryID";
            ddl_Category.DataSource = category.ToList();
            ddl_Category.DataBind();
        }
        catch
        {

        }
    }

— Second Way :-


private void BindCategories()
    {
        try
        {
            NorthwindDataContext dbml = new NorthwindDataContext();
            var category = from cat in dbml.Categories
                           select cat;
            ddl_Category.DataTextField = "CategoryName";
            ddl_Category.DataValueField = "CategoryID";
            ddl_Category.DataSource = category.ToList();
            ddl_Category.DataBind();
        }
        catch
        {

        }
    }

— Third Way :-


private void BindCategories()
    {
        try
        {
            NorthwindDataContext dbml = new NorthwindDataContext();
            var category = dbml.Categories;
            ddl_Category.DataTextField = "CategoryName";
            ddl_Category.DataValueField = "CategoryID";
            ddl_Category.DataSource = category.ToList();
            ddl_Category.DataBind();
        }
        catch
        {

        }
    }

and In Page Load add this code

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCategories();
        }
    }

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Linq

Change FontSize with Gridview According to Condition

November 3, 2010 Leave a comment

Hi all,

Try this example to Change FontSize with Gridview According to Condition

add this CSS Style in your page

    <style type="text/css">   
    .Small   
    {       
    font-size: small;   
    }   
    .Large   
    {       
    font-size: large;   
    }
    </style>

In Gridview ItemTemplate

<asp:TemplateField HeaderText="Description" SortExpression="Description">
           <ItemTemplate>
               <asp:Label ID="Label1" runat="server" Text='<%# Bind("Description") %>' CssClass='<%# (Convert.ToString(Eval("Description")).Length > 30)? "Small": "Large"%>'></asp:Label>
           </ItemTemplate>
</asp:TemplateField>

and there is another way to do the same

<asp:TemplateField HeaderText="Description" SortExpression="Description">
          <ItemTemplate>
             <asp:Label ID="Label1" runat="server" Text='<%# Bind("Description") %>' Font-Size='<%# (Eval("Description").ToString().Length > 30) ? FontUnit.XSmall : FontUnit.Small %>'></asp:Label>
          </ItemTemplate>
</asp:TemplateField>

Hope this helps

Good Luck

Categories: ASP.Net