You can load the Dropdownlist from your Companies table on Page_Load
, and fill in the property IsPostBack
true.
To load the Contributors Dropdownlist, Voce can use the event SelectedIndexChanged
of the Dropdownlist Company and make the query stating which company was selected.
See the example below:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="EmpresaColaboradores.aspx.cs" Inherits="stackoverflow.EmpresaColaboradores" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<table>
<tr>
<td class="Label">
<asp:Label ID="lblIdEmpresa" runat="server" Text="Empresas"></asp:Label>
</td>
<td class="Field">
<asp:DropDownList ID="ddlIdEmpresa" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlIdEmpresa_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="Label">
<asp:Label ID="lblIdColaboradore" runat="server" Text="Colaboradores"></asp:Label>
</td>
<td class="Field">
<asp:DropDownList ID="ddlIdColaboradore" runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
</asp:Content>
using System;
using System.Linq;
using System.Web.UI.WebControls;
namespace stackoverflow
{
public partial class EmpresaColaboradores : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
using (var ctx = new stackoverflowEntities())
{
var qrCategoria = ctx.Categoria.ToList();
ddlIdEmpresa.DataTextField = "Nome";
ddlIdEmpresa.DataValueField = "CategoriaId";
ddlIdEmpresa.DataSource = qrCategoria;
ddlIdEmpresa.DataBind();
}
ddlIdEmpresa_SelectedIndexChanged(null, null);
}
protected void ddlIdEmpresa_SelectedIndexChanged(object sender, EventArgs e)
{
var IdEmpresa = Convert.ToInt32(ddlIdEmpresa.SelectedValue);
ddlIdColaboradore.Items.Clear();
ddlIdColaboradore.Enabled = IdEmpresa > 0;
if (IdEmpresa > 0)
{
using (var ctx = new stackoverflowEntities())
{
var qrSubCategoria = ctx.SubCategoria
.Where(sc => sc.CategoriaId == IdEmpresa)
.ToList();
ddlIdColaboradore.DataTextField = "Nome";
ddlIdColaboradore.DataValueField = "SubCategoriaId";
ddlIdColaboradore.DataSource = qrSubCategoria;
ddlIdColaboradore.DataBind();
}
}
}
}
}
See the results:
Using a clause
where
?– Jéf Bueno
The two Dropdownlists are on the same page and you want the data of the first to modify the results of the second without the page being reloaded, that’s it?
– Fleuquer Lima
@Fleuquerlima exactly
– Kawaii
You could use ajax for that. Take a look at this answer from Cigano Morrison: http://answall.com/questions/76255/como-popular-um-dropdownlist-a-parti-de-outro-dropdownlist and this http:///www.daviferreira.com/posts/populando-selects-de-cities-estados-ajax-php-e-jquery or this http://www.yiiframework.com/forum/index.php/topic/15560-how-to-load-dropdown-dianico-no-update/
– pnet