Fórum Datalist #134691

12/04/2010

0

Olá,   Estou com uma dúvida que pode ser simples, mas esta me dando alguns problemas   Tenho um projeto de Imobiliaria e para isto  criei as tabelas de Empreendimentos, Torres e apartamentos.   Para mostrar em um WebForm esta, oque chamo de Vizualização,     Na página eu Criei um DataList Com informações das torres.    Após isto dentro deste Datalist acrescentei um Outro que me mostra o Número e o Status de cada apartamento     o Status é igual a Vendido, livre....       Coloquei dentro deste Segundo datalist uma combobox  com a definição do status,   meu cliente quer mudar este  Status no SelectIndexChanged e para isto preciso do ID do Apartamento, mas este não consigo, buscar.   Alguem pode me ajudar.....???      
Fernando Reis

Fernando Reis

Responder

Posts

12/04/2010

Fabio Mans

Olá Fernando segue a solução.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:DataList ID="DataList1" runat="server" DataKeyField="idtorre" OnItemDataBound="DataList1_ItemDataBound">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("IdTorre") %>'></asp:Label>
                    <br />
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("Torre") %>'></asp:Label>
                    <asp:DataList ID="DataList2" runat="server" DataKeyField="IdApto" OnItemDataBound="DataList2_ItemDataBound">
                        <ItemTemplate>
                            <asp:Label ID="IdLbl" runat="server" Text='<%# Eval("IdApto") %>'></asp:Label>                           
                            <br />
                          
                            <br />
                           
                            <asp:DropDownList ID="StatusDrop" runat="server" AutoPostBack="True" OnSelectedIndexChanged="StatusDrop_SelectedIndexChanged">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:DataList>
                </ItemTemplate>
            </asp:DataList></div>
    </form>
</body>
</html>


===============================================================




using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            ObterTorres();
    }

    private void ObterTorres()
    {
        Torres torres= new Torres();
        DataList1.DataSource = torres.ObterTorres();
        DataList1.DataBind();
    }


    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
        using (DataList tempDt = (DataList)e.Item.FindControl("DataList2"))
        {
            Apartamentos ap = new Apartamentos();
            tempDt.DataSource = ap.ObterApartamentos();
            tempDt.DataBind();
        }
      
    }
    protected void StatusDrop_SelectedIndexChanged(object sender, EventArgs e)
    {
       
        DropDownList ddl = sender as DropDownList;
        foreach (DataListItem dataListItem in DataList1.Items)
        {
            DataList temp  = (DataList)dataListItem.FindControl("DataList2");
            foreach (DataListItem dataListItem2 in temp.Items)
            {
                Control ctrl = dataListItem2.FindControl("StatusDrop") as DropDownList;
                if (ctrl != null)
                {
                    DropDownList ddl1 = (DropDownList)ctrl;
                 
                    if (ddl != null)
                        if (ddl.ClientID == ddl1.ClientID)
                        {
                            Label lbl = dataListItem2.FindControl("IdLbl") as Label;
                            if (lbl != null) Response.Write(lbl.Text);
                            break;
                        }
                }
            }


====================================================================


using System.Collections.Generic;

/// <summary>
/// Summary description for Consultoria
/// </summary>
public class Consultoria
{
   
}


public struct Torres
{
    private int _idTorre;

    public int IdTorre
    {
        get { return _idTorre; }
        set { _idTorre = value; }
    }

    public string Torre
    {
        get { return _torre; }
        set { _torre = value; }
    }

    private string _torre;


    public List<Torres> ObterTorres()
    {
        List<Torres> torres = new List<Torres>();
        Torres torre = new Torres();
        torre.IdTorre = 1;
        torre.Torre = "Barão";
        torres.Add(torre);

        Torres torre1 = new Torres();
        torre1.IdTorre = 2;
        torre1.Torre = "Baronesa";
        torres.Add(torre1);

        return torres;
    }
}

public struct Apartamentos
{
    public int IdApto
    {
        get { return _idApto; }
        set { _idApto = value; }
    }

    public int IdTorre
    {
        get { return _idTorre; }
        set { _idTorre = value; }
    }
   

    private int _idApto;
    private int _idTorre;
   

    public List<Apartamentos> ObterApartamentos()
    {
        List<Apartamentos> apartamentos = new List<Apartamentos>();
        Apartamentos ap = new Apartamentos();
        ap._idApto = 11;
        ap.IdTorre = 1;
        apartamentos.Add(ap);

        Apartamentos ap1 = new Apartamentos();
        ap1._idApto = 12;
        ap1.IdTorre = 1;
        apartamentos.Add(ap1);

        Apartamentos ap2 = new Apartamentos();
        ap2._idApto = 13;
        ap2.IdTorre = 1;
        apartamentos.Add(ap2);

        Apartamentos ap3 = new Apartamentos();
        ap3._idApto = 25;
        ap3.IdTorre = 2;
        apartamentos.Add(ap3);

        Apartamentos ap4 = new Apartamentos();
        ap4._idApto = 27;
        ap4.IdTorre = 2;
        apartamentos.Add(ap4);

        Apartamentos ap5 = new Apartamentos();
        ap5._idApto = 28;
        ap5.IdTorre = 2;
        apartamentos.Add(ap5);

        return apartamentos;
    }
}

public struct Status
{
    public int IdStatus
    {
        get { return _idStatus; }
        set { _idStatus = value; }
    }

    public string NomeStatus
    {
        get { return _nomeStatus; }
        set { _nomeStatus = value; }
    }

    private int _idStatus;
    private string _nomeStatus;

    public List<Status> ObterStatus()
    {
        List<Status> status = new List<Status>();
        Status st = new Status();
        st.IdStatus = 1;
        st.NomeStatus = "Vendido";
        status.Add(st);

        Status st2 = new Status();
        st2.IdStatus = 2;
        st2.NomeStatus = "Alugado";
        status.Add(st2);

        Status st3 = new Status();
        st3.IdStatus = 1;
        st3.NomeStatus = "Disponível";
        status.Add(st3);

        return status;
      
    }
}




          
          

           

          

         
        }
    }

    protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;

        using (DropDownList tempDrp = (DropDownList)e.Item.FindControl("StatusDrop"))
        {
            Status st = new Status();
            tempDrp.DataSource = st.ObterStatus();
            tempDrp.DataValueField = "IdStatus";
            tempDrp.DataTextField = "NomeStatus";
            tempDrp.DataBind();
        }
    }
}
Responder

Gostei + 0

12/04/2010

Fernando Reis

Estou testando,


Respondo em breve,

Grato
Responder

Gostei + 0

12/04/2010

Fernando Reis

Fabio,


Cheguei até esta parte

Label lbl = dataListItem2.FindControl("IdLbl") as Label;
                            if (lbl != null) Response.Write(lbl.Text);
                            break;



Consegui sim pegar o ID do apartamento,

agora,

Preciso saber qual o Valor do DropDowlist selecionado no Indexchanged para pedir o método de Update e dar um Databind nos dois Datalist


Como posso fazer?

Responder

Gostei + 0

12/04/2010

Fernando Reis

Fabio

Cheguei la,


bacana
mas me deu um erro no  Refresh da página


pode me ajudar


veja o código:


int intStatus = Convert.ToInt32(ddl.SelectedValue);
                            Label lbl = dataListItem2.FindControl("lblIDApartamento") as Label;
                            //if (lbl != null) Response.Write(lbl.Text);
                            //break;

                            clsQuarto clsQuarto = new clsQuarto();
                            clsQuarto.UpdateQuarto(Convert.ToInt32(lbl.Text),intStatus);
                            dtlTorres.DataBind();
Responder

Gostei + 0

12/04/2010

Fernando Reis

Erro:

Coleção foi modificada; talvez a operação de enumeração não seja executada. body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } pre {font-family:"Lucida Console";font-size: .9em} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } Coleção foi modificada; talvez a operação de enumeração não seja executada. Descrição: Ocorreu uma exceção não tratada durante a execução da atual solicitação da Web. Examine o rastreamento de pilha para obter mais informações sobre o erro e onde foi originado no código.

Detalhes da Exceção: System.InvalidOperationException: Coleção foi modificada; talvez a operação de enumeração não seja executada.

Erro de Origem:

Linha 74: { Linha 75: DropDownList ddl = sender as DropDownList; Linha 76: foreach (DataListItem dataListItem in dtlTorres.Items) Linha 77: { Linha 78: DataList temp = (DataList)dataListItem.FindControl("dtlApartamentos");
Arquivo de Origem: c:\Users\Fernando\Desktop\brcorp\investidores\EspelhoVendas.aspx.cs    Linha: 76

Rastreamento de Pilha:

Responder

Gostei + 0

12/04/2010

Fernando Reis

Fabio,   algum dica?

Responder

Gostei + 0

12/04/2010

Fabio Mans

Não entendi, você não conseguiu pegar o ID?
Responder

Gostei + 0

12/04/2010

Fernando Reis

opa


Consegui sim,

Peguei o ID,  e pedi para fazer o UPDATE com uma classe
mas retornou o erro abaixo







Responder

Gostei + 0

12/04/2010

Fabio Mans

Você não fez igual ao exemplo abaixo?

protected void StatusDrop_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        foreach (DataListItem dataListItem in DataList1.Items)
        {
            DataList temp  = (DataList)dataListItem.FindControl("DataList2");
            foreach (DataListItem dataListItem2 in temp.Items)
            {
                Control ctrl = dataListItem2.FindControl("StatusDrop") as DropDownList;
                if (ctrl != null)
                {
                    DropDownList ddl1 = (DropDownList)ctrl;
                 
                    if (ddl != null)
                        if (ddl.ClientID == ddl1.ClientID)
                        {
                            Label lbl = dataListItem2.FindControl("IdLbl") as Label;
                            if (lbl != null)
                            {
                                UtualizarStatus(ddl.SelectedValue, lbl.Text);
                            }
                               
                              
                        }
                }
            }
        }
    }

    private void UtualizarStatus(string p, string p_2)
    {
        //Metodo que atualiza
        ObterTorres();
    }
Responder

Gostei + 0

12/04/2010

Fernando Reis

Vou tentar meu amigo
Responder

Gostei + 0

12/04/2010

Fernando Reis

veja a chamada


protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        foreach (DataListItem dataListItem in dtlTorres.Items)
        {
            DataList temp = (DataList)dataListItem.FindControl("dtlApartamentos");
            foreach (DataListItem dataListItem2 in temp.Items)
            {
                Control ctrl = dataListItem2.FindControl("ddlStatus") as DropDownList;
                if (ctrl != null)
                {
                    DropDownList ddl1 = (DropDownList)ctrl;
                    if (ddl != null)
                        if (ddl.ClientID == ddl1.ClientID)
                        {
                            int intStatus = Convert.ToInt32(ddl.SelectedValue);
                            Label lbl = dataListItem2.FindControl("lblIDApartamento") as Label;
                            //if (lbl != null) Response.Write(lbl.Text);
                            //break;
                            clsQuarto clsQuarto = new clsQuarto();
                            clsQuarto.UpdateQuarto(Convert.ToInt32(lbl.Text),intStatus);
                            dtlTorres.DataBind();

                        }
                }
            }
        }
    }
Responder

Gostei + 0

12/04/2010

Fabio Mans

dtlTorres.DataBind(); ??

Chame o método que carrega o Datalist.


Responder

Gostei + 0

12/04/2010

Fernando Reis

Coloquei um DataSource ligado
é este o problema?

Responder

Gostei + 0

12/04/2010

Fabio Mans

O que é um DataSource Ligado? ObjectDataSource?

Eu não uso, faço via código.
Responder

Gostei + 0

12/04/2010

Fernando Reis

Sim, utilizo um datasource para o primeiro Datalist
este é o problema???


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar