Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Autopostback e Listbox

  1. #1
    Utente di HTML.it L'avatar di dwb
    Registrato dal
    Mar 2001
    residenza
    My IDE! :)
    Messaggi
    2,908

    Autopostback e Listbox

    Nel mio form ho alcune Listbox e, a seconda del valore selezionato, devo abilitare o no una textbox. Fino a quando ho un solo Listbox tutto funziona a meraviglia, ma appena ho inserito il secondo, sembra che questo modifichi il comportamento anche del primo.

    Ecco il codice:

    codice:
     protected void chkTrade_SelectedIndexChanged(object sender, EventArgs e)
            {
               if(chkTrade.SelectedValue=="SI")
                {
                    txtTrade.Enabled = true;
                }
               else
                {
                    txtTrade.Enabled = false;
                }
            }
    
    
            protected void chkEtoro_SelectedIndexChanged(object sender, EventArgs e)
            {
                if(chkEtoro.SelectedValue =="SI")
                {
                    txtEtoro.Enabled = true;
                }
                else
                {
                    txtEtoro.Enabled = false;
                }
            }
    Come posso separare gli ambiti?
    ~Il nome di una variabile deve riflettere il suo scopo e non il suo tipo di dati, NET Framework.

  2. #2
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,887
    Stai attento a fare il bind DENTRO una If che sia solo al Not Postback

    codice:
    if (!IsPostBack)
    {
     Bind_chkTrade(); // sub che effettua il bind della LB1
     Bind_chkEtoro(); // sub che effettua il bind della LB2
    }

    altrimenti al cambiamento di uno dei due, la pagina rifa' il bind e porta allo stato iniziale entrambe le LBox.

    Posta tutto il codice, comunque. Il comportamento è anomalo.

  3. #3
    Utente di HTML.it L'avatar di dwb
    Registrato dal
    Mar 2001
    residenza
    My IDE! :)
    Messaggi
    2,908
    Details.aspx.cs
    codice:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.Sql;
    using System.Data.SqlClient;
    
    
    namespace AffiliationCMS
    {
        public partial class Details : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
    
                string LoginStatus = Request.Cookies["HasLogged"]["Status"].ToString();
                if(LoginStatus !="1")
                {
                    Response.Redirect("Login.aspx");
                }
            }
    
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                if (Page.IsValid)
                {
                   
                    
                    string Username = Request.Cookies["HasLogged"]["Username"].ToString();
                    string Code = Guid.NewGuid().ToString();
                    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                    builder.DataSource = "localhost\\SQLExpress";
                    builder.InitialCatalog = "MioDB";
                    builder.IntegratedSecurity = true;
                    SqlConnection conn = new SqlConnection(builder.ConnectionString);
                   
    
    
                    SqlCommand cmd = new SqlCommand("INSERT INTO UsersAffiliation(Data, Nome, Cognome, EmailSkrill, Referer, UserCode, chkTrade, chk24Option, chkEtoro, txtTrade, txtEtoro, txt24Option) values ('" + txtData.Text + "', '" + txtName.Text + "', '" + txtSurname.Text + "', '" + txtSkrillMail.Text + "', '" + Username + "', '" + Code + "', '" + chkTrade.SelectedValue.ToString() + "', '" + chk24Option.SelectedValue.ToString() + "', + '" + chkEtoro.SelectedValue.ToString() + "', '" + txtTrade.Text.ToString() + "', '" + txtEtoro.Text.ToString() + "', '" + txt24Option.Text.ToString() + "')", conn);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    
                }
            }
    
    
        
    
    
            protected void chkEtoro_CheckedChanged(object sender, EventArgs e)
            {
                txtEtoro.Enabled = true;
            }
    
    
            protected void chk24Option_CheckedChanged(object sender, EventArgs e)
            {
                txt24Option.Enabled = true;
            }
    
    
            protected void chkTrade_CheckedChanged1(object sender, EventArgs e)
            {
                txtTrade.Enabled = true;
            }
    
    
            protected void chkTrade_SelectedIndexChanged(object sender, EventArgs e)
            {
               if(chkTrade.SelectedValue=="SI")
                {
                    txtTrade.Enabled = true;
                }
               else
                {
                    txtTrade.Enabled = false;
                }
            }
    
    
            protected void chkEtoro_SelectedIndexChanged(object sender, EventArgs e)
            {
                if(chkEtoro.SelectedValue =="SI")
                {
                    txtEtoro.Enabled = true;
                }
                else
                {
                    txtEtoro.Enabled = false;
                }
            }
        }
    }
    Details.aspx

    codice:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Details.aspx.cs" Inherits="AffiliationCMS.Details" %>
    
    
    <!DOCTYPE html>
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <style type="text/css">
            .auto-style1 {
                width: 210px;
            }
            .auto-style2 {
                width: 164px;
            }
        </style>
        <h1>AREA RISERVATA</h1>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            Benvenuto, <b><%=Request.Cookies["HasLogged"]["Username"] %></b>
        
        </div>
        <table style="width:70%;">
            <tr>
                <td class="auto-style1">Data</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtData" runat="server" EnableViewState="False"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="validatorData" runat="server" ControlToValidate="txtData" ErrorMessage="Inserire la Data!"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">Nome</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtName" runat="server" EnableViewState="False"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="validatorName" runat="server" ControlToValidate="txtName" ErrorMessage="Inserire il Nome"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">Cognome</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtSurname" runat="server" EnableViewState="False"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="validatorSurname" runat="server" ControlToValidate="txtSurname" ErrorMessage="Inserire il Cognome!"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">Email Skrill</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtSkrillMail" runat="server" EnableViewState="False"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="validatorMail" runat="server" ControlToValidate="txtSkrillMail" ErrorMessage="Inserire la mail Skrill!"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
                    &nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td colspan="3"><center>BROKER</center></td>
            </tr>
            <tr>
                <td class="auto-style1">
                    <asp:ListBox ID="chkTrade" runat="server" Rows="1" OnSelectedIndexChanged="chkTrade_SelectedIndexChanged">
                        <asp:ListItem Value="SI">SI</asp:ListItem>
                        <asp:ListItem Value="NO">NO</asp:ListItem>
                    </asp:ListBox>
                    Trade</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtTrade" runat="server" Enabled="False" EnableViewState="False"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">
                    <asp:ListBox ID="chkEtoro" runat="server" Rows="1" OnSelectedIndexChanged="chkEtoro_SelectedIndexChanged">
                        <asp:ListItem Value="SI">SI</asp:ListItem>
                        <asp:ListItem Value="NO">NO</asp:ListItem>
                    </asp:ListBox>
                    Etoro</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtEtoro" runat="server" Enabled="False" EnableViewState="False"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">
                    <asp:ListBox ID="chk24Option" runat="server" Rows="1">
                        <asp:ListItem Value="SI">SI</asp:ListItem>
                        <asp:ListItem Value="NO">NO</asp:ListItem>
                    </asp:ListBox>
                    24Option</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txt24Option" runat="server" Enabled="False" EnableViewState="False"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Rimborsato</td>
                <td class="auto-style2">
                    <asp:ListBox ID="chkRefund" runat="server" Rows="1">
                        <asp:ListItem Value="1">SI</asp:ListItem>
                        <asp:ListItem Value="0">NO</asp:ListItem>
                    </asp:ListBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Rimborso</td>
                <td class="auto-style2">
                    <asp:TextBox ID="refundTxt" runat="server" EnableViewState="False"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>
    
    
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Inserisci" />
            <br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    
    
        </form>
        </body>
    </html>
    ~Il nome di una variabile deve riflettere il suo scopo e non il suo tipo di dati, NET Framework.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.