Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, Why when I chose an item from my dropDownList or when I click on a button clears my textBoxes ??
here is my ASP Code

ASP.NET
<%@ Page Title="Réparation" Language="C#" MasterPageFile="~/TP/MasterPageTP.master" AutoEventWireup="true" CodeFile="ModifRep.aspx.cs" Inherits="TP_ModifRep" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
     <div id="content">
    <br />
<br />
<h2>Matériels en réparations</h2>
<asp:Panel ID="Panel1" runat="server" align="center">
<table style="border-color: #000099; height: 918px; width: 688px" border="1">
 <tr>
        <td>
                  <asp:Label ID="Label18" runat="server" Text="Diagnostique du préstatire"></asp:Label>
              </td>
        <td>
                 <asp:TextBox ID="txtSuivi" runat="server" CssClass="TextBox" 
          Height="94px" Width="288px" TextMode="MultiLine"></asp:TextBox>
              </td>
        </tr>
          <tr>
        <td>
                  <asp:Label ID="Label20" runat="server" Text="Statut"></asp:Label>
              </td>
        <td>
                <asp:DropDownList ID="cmbStatut" runat="server" CssClass="Combobox" 
                Height="32px" Width="170px" AutoPostBack="True" AppendDataBoundItems="true"
                onselectedindexchanged="cmbStatut_SelectedIndexChanged" >
            </asp:DropDownList>
              <br />
            <asp:Label ID="lblErrorSta" runat="server" ForeColor="Red" 
                Text="Veuillez saisir le statut" Visible="False"></asp:Label>
              </td>
        </tr>
          <tr>
        <td>
                  <asp:Label ID="Label19" runat="server" Text="Date de récéption  du matériel"></asp:Label>
              </td>
        <td>
               <asp:TextBox ID="txtDateReception" runat="server" CssClass="TextBox" 
                Height="25px" Width="162px"></asp:TextBox>
            <asp:CalendarExtender ID="txtDateReception_CalendarExtender" runat="server" 
                Enabled="True" TargetControlID="txtDateReception">
            </asp:CalendarExtender>
              </td>
        </tr>
          <tr>
        <td>
                 <asp:Label ID="Label21" runat="server" Text="Nouveau service tag"></asp:Label>
              </td>
        <td>
            <asp:TextBox ID="txtNvSt" runat="server" CssClass="TextBox" Height="25px" 
                Width="162px"></asp:TextBox>
              <asp:Label ID="lblST" runat="server" Text="Label"></asp:Label>
              </td>
        </tr>
    </table>
 <asp:Button ID="btnValider" runat="server" CssClass="blueButton"
                Text="Valider" onclick="btnValider_Click" />
 </asp:Panel>


Here is my btnValider Code
C#
protected void btnValider_Click(object sender, EventArgs e)
    {
         if (cmbStatut.SelectedIndex==2)
                {if (string.IsNullOrEmpty(txtNvSt.Text))
                        {
                            con.charger("update Materiel set reparation= NULL where serviceTag='" + txtServiceTag.Text + "'", false);
                            Session["ST"] = txtNvSt.Text;
                            Response.Redirect("NouveauMAt.aspx");
                        }
                        else
                        {
                            con.charger("insert into Stocker values('1', '" + txtServiceTag.Text + "')", false);

                        }
                }
               else if (cmbStatut.SelectedIndex == 3)
               {
                   con.charger("insert into Stocker values('4', '" + txtServiceTag.Text + "')", false);
                   Response.Redirect("StockHS.aspx");
               }

                

    }



At first it tests the selected index of the dropDownList (cmbStatut), and then it tests if the txtNvSt is empty or not. If it's not empty it has to retrieve the value in the txtNv.Text and put it in a session, after that it redirects me to the specific page.
When I did a debug, I found that the txtNvSt.text="" as well as other textBoxes.

The same thing happends when I chose another item from my DropDownList, it fires an autopostBack and clears all textBoxs
Thank you for your help
Posted
Updated 3-Jul-14 0:36am
v2

1 solution

Quote:
If it's not empty it has to retrieve the value in the txtNv.Text and put it in a session, after that it redirects me to the specific page.
But the code is the reverse. Do like below...
C#
if(!string.IsNullOrEmpty(txtNvSt.Text)
 
Share this answer
 
Comments
Member 10889990 3-Jul-14 7:15am    
I was doing that code, but it didn't work, beacuse when I type something in the txtNvSt it goes to the else, and when I tool the "!" off, it worked as I wanted it to be. but this is not my probleme, the probleme is whenever an autopostBack is fired all my textBoxes get emty ! How can I resolve this ???
Can you show me the Page Load code?
Member 10889990 3-Jul-14 7:47am    
I solved the indexChanged autoPost back issue, but still have this major prbleme of this button !!!!
Here's my load code
protected void Page_Load(object sender, EventArgs e)
{
txtNvSt.Attributes["value"] = txtNvSt.Text;
txtSuivi.Attributes["value"] = txtSuivi.Text;
if (!IsPostBack)
{
ArrayList list = new ArrayList();
list.Insert(0, "Selectionnez...");
list.Insert(1, "En cours");
list.Insert(2, "Résolut");
list.Insert(3, "Envoyé au stock HS");
cmbStatut.DataSource = list;
cmbStatut.DataBind();

}
// Here I have other textBoxs that are filled automatically from my dataBase (I don't have problem with those fields)
con.seConnecter();
if (Session["serviceTag"] != null)
{

con.charger("select serviceTag , Entite, typeMateriel , serieMateriel , Emplacement, dateDeclaration, nInteraction , Commentaire, nTicketPresta, " +
"suivieMateriel, dateReception , nNouvST , statut from Reparation where serviceTag='" + Session["serviceTag"] +"' order by dateDeclaration desc", true);
txtServiceTag.Text = "" + con.dataTable.Rows[0].ItemArray[0].ToString();
txtDateIncident.Text = "" + con.dataTable.Rows[0].ItemArray[5].ToString();
txtNInteraction.Text = "" + con.dataTable.Rows[0].ItemArray[6].ToString();
txtCommentaire.Text = "" + con.dataTable.Rows[0].ItemArray[7].ToString();
txtTicketPres.Text = "" + con.dataTable.Rows[0].ItemArray[8].ToString();
txtSuivi.Text = "" + con.dataTable.Rows[0].ItemArray[9].ToString();
txtDateReception.Text = "" + con.dataTable.Rows[0].ItemArray[10].ToString();
txtNvSt.Text = "" + con.dataTable.Rows[0].ItemArray[11].ToString();
}
Do you see that, the below code is assigning text to the TextBox...

txtNvSt.Text = "" + con.dataTable.Rows[0].ItemArray[11].ToString();

So, when you Select anything from the DropDownList, it executes this as this is not inside !IsPostBack. I suspect that this is the line making the TextBox blank. Debug and see.
Member 10889990 3-Jul-14 8:07am    
YES YES YESSSS, IT WOOOORKED, My god THANK YOUUUUUUUUUU @Tadit Dash You are such a life saver. THNAK YOUUUUUUU

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900