Skip to content

Instantly share code, notes, and snippets.

@stevebosworth
Last active December 16, 2015 05:19
Show Gist options
  • Save stevebosworth/5383859 to your computer and use it in GitHub Desktop.
Save stevebosworth/5383859 to your computer and use it in GitHub Desktop.
A custom Databound Dropdownlist using LINQ
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="databoundHybridControl.aspx.cs" Inherits="databoundHybridControl" %>
<%@ Register TagPrefix="DBadvHC" Namespace="databoundHybrid" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<DBadvHC:hybridControlDB ID="hcDB_main" runat="server" />
</div>
</form>
</body>
</html>
using System;
using System.Web.UI;//imported
using System.Web.UI.WebControls;//imported
using System.Linq; //imported
/// <summary>
/// Summary description for databoundHybridControl
/// </summary>
namespace databoundHybrid
{
public class hybridControlDB : CompositeControl
{
productClass objProd = new productClass();
private DropDownList _ddlProducts;
protected override void CreateChildControls()
{
//DropDownList
_ddlProducts = new DropDownList();
_ddlProducts.ID = "ddl_products";
_ddlProducts.DataSource = objProd.getProducts();
_ddlProducts.DataTextField = "name";
_ddlProducts.DataBind();
this.Controls.Add(_ddlProducts);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for productClass
/// </summary>
public class productClass
{
LinqClassDataContext objProdDC = new LinqClassDataContext();
public IQueryable<product> getProducts()
{
var allProducts = objProdDC.products.Select(x => x);
return allProducts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment