entity ile dropdown doldurmak ve seçildiğinde gride datasource çağırmak.................
-northwind db categories ve products kullandım. kodlar resimlerin alt kısmındadır
---------- html markup - update panel
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1" DisplayAfter="2">
<ProgressTemplate>
<img src="Content/images/progress.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
<asp:Label ID="Label1" runat="server" Text="Kategoriler"></asp:Label>
<asp:DropDownList ID="DropDownListCategories" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListCategories_SelectedIndexChanged"></asp:DropDownList>
<asp:GridView ID="GridViewCategories" runat="server" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
-----code behind
#region" Tanımlamalar "
NorthwindEntities model = new NorthwindEntities();
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Kategoriler();
}
}
private void Kategoriler()
{
var kats=(from kt in model.Categories orderby kt.CategoryName
select kt);
foreach (var item in kats)
{
ListItem li=new ListItem();
li.Text=item.CategoryName;
li.Value=item.CategoryID.ToString();
this.DropDownListCategories.Items.Add(li);
}
this.DropDownListCategories.SelectedIndex = -1;
}
private void Urunler(int catID)
{
var prods = (from pr in model.Products
where pr.CategoryID == catID
orderby pr.ProductName
select pr);
GridViewCategories.DataSource = prods.ToList();
GridViewCategories.DataBind();
}
protected void DropDownListCategories_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.DropDownListCategories.SelectedIndex < 0) { return; }
int catID =Convert.ToInt32(DropDownListCategories.SelectedValue);
Urunler(catID);
}
Yorumlar
Yorum Gönder