Usuário com melhor resposta
Json dados da view para o .cs

Pergunta
-
Estou com um grande problema eu estou tentando fazer com que o meu segundo DropDownList seja populado, ex: eu seleciono o prédio no DropDownList1 dai ele tem que me mostrar a disciplina que tem na quele prédio, não sei como chamar o método no CS já fiz de várias maneiras e nada funciona, deve ser fácil mas n to conseguindo, ai esta o meu código:
VIEW:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TurmaInc.aspx.cs" Inherits="Verification.TurmaInc" %> <!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 id="Head1" runat="server"> <title>Cadastrar Turma</title> <script src="Script/jquery1.7.2.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#DropDownList1').change(getModels); $('#DropDownList2').attr('disabled', true); }); function getModels() { $.ajax({ url: 'TurmaInc.aspx/Turmas', //URL de destino contentType: 'application/x-www-form-urlencoded; charset=UTF-8', data: "{id: '" + $('#DropDownList1').val() + "'}", type: 'get', dataType: "json", //Tipo de Retorno success: function (json) { alert(json['id']); } }); } </script> </head> <body> <form action="TurmaInc.aspx" method="post" id="frm_cadastra_turma" name="frm_cadastra_turma" runat="server"> <table> <tr> <td> Prédio: <br/> <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList> </td> </tr> <tr> <td> Disciplina: <br/> <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList> </td> </tr> </table> </form> </body> </html>
NO CS:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data.SqlClient; using System.Data; namespace Verification { public partial class TurmaInc : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //quando carrega a página entra aqui if (!Page.IsPostBack) { this.SelecionaPredio(); } } void SelecionaPredio() { using (DataClasses1DataContext context = new DataClasses1DataContext(ConfigurationManager.ConnectionStrings["dboConnectionString"].ConnectionString)) { var resultado = from pr in context.Predios orderby pr.predio1 ascending select new { pr.id, pr.predio1 }; DropDownList1.DataSource = resultado.ToList(); DropDownList1.DataTextField = "predio1"; DropDownList1.DataValueField = "id"; DropDownList1.DataBind(); } }
void Turmas(string id)
{
using (DataClasses1DataContext context = new DataClasses1DataContext(ConfigurationManager.ConnectionStrings["dboConnectionString"].ConnectionString)) { var resultado = from pr in context.Predios orderby pr.predio1 ascending
where pr.id = id select new { pr.id, pr.predio1 }; resultado.ToList(); }
}
} }
- Editado guitutilo sexta-feira, 25 de maio de 2012 01:53
Respostas
-
veja
http://forums.asp.net/t/1616411.aspx?jquery+ajax+-++bind+json+to+dropdownlist
Não esqueça de usar o componente </> na barra para posta seu código. Microsoft MCPD,MCTS,MCC
- Marcado como Resposta Harley Araujo segunda-feira, 28 de maio de 2012 13:55