2015-11-21 34 views
0

我有一個文本輸入字段和3個組合框的表單和一個提交按鈕,它旨在將表單的數據保存到數據庫的表中。所以當我點擊提交按鈕,經過一些調試後,我發現在點擊它時,組合框沒有選定的值,從而導致錯誤和異常,並且顯然不會將數據保存到數據庫中。但不僅如此,除此之外,單擊提交按鈕後,組合框會丟失所有值,填充它們的完整列表將消失,就好像它們未被初始化。所有這些的可悲部分(因爲我知道我可以更多地檢查我的代碼)是當我完全相同時,在我的ASP.NET上使用完全相同的代碼(相同的dll和所有內容) Visual Studio服務器(在本地機器上)一切都很完美,因此我正在寫入同一個遠程數據庫,並且在單擊提交按鈕後,數據保存到數據庫中,並且窗體保持正常狀態,與點擊之前的所有組合框一樣正常。C#DropDownList行爲奇怪只在服務器上,而不是本地

爲什麼只發生在服務器上?爲什麼它可以在數據庫中寫入和讀取,而本地沒有任何問題?

我認爲它可能與我的Page_Load方法中的PostBack相關(我初始化它的組合框,但只有它不是PostBack時)......所以這可能是一個與IIS服務器如何相關的問題與PostBacks一起工作可能與本地ASP.NET服務器不同?

編輯 我加入的形式

<form id="form1" runat="server">  

<div class="popup" style="border: 1px solid #000; width: 600px;"> 
    <table style="height: 148px; width: 584px"> 
     <tr> 
      <td class="style1"> 
       <table> 
        <tr> 
         <td> 
          <abbr title="El N° de Folio le será otorgado luego de haber guardado la necesidad."><asp:Label ID="Labe" runat="server" Text="Folio *" class="innerControl"></asp:Label></abbr> 
         </td> 
         <td colspan="2"> 
          <abbr title="El N° de Folio le será otorgado luego de haber guardado la necesidad."><asp:TextBox ID="folTB" runat="server" Width="70px" class="innerControl" Enabled="false"></asp:TextBox></abbr> 
         </td> 
         <td> 
          <asp:Label ID="Label4" runat="server" Text="Fecha" class="innerControl"></asp:Label> 
         </td> 
         <td colspan="3"> 
          <asp:TextBox ID="fecTB" runat="server" Width="100px" class="innerControl" Enabled="false"></asp:TextBox> 
          <asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server" 
           Enabled="True" TargetControlID="fecTB" DefaultView="Years" PopupButtonID="calBTN"></asp:CalendarExtender> 
          <asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" 
           FilterType="Numbers, Custom" ValidChars="-" TargetControlID="fecTB"> 
           </asp:FilteredTextBoxExtender> 
          <asp:ScriptManager ID="ScriptManager1" runat="server" 
           EnableScriptGlobalization="True"> 
          </asp:ScriptManager> 
         </td> 
        </tr> 
        <tr> 
         <td style="padding-right: 20px;"> 
          <asp:Label ID="Label7" runat="server" Text="Beneficiarios" class="innerControl"></asp:Label> 
         </td> 
         <td style="padding-right: 20px;" colspan="2"> 
          <asp:TextBox ID="benTB" runat="server" class="innerControl" Width="70px" Text="0" Enabled="false"></asp:TextBox> 
          <asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender3" runat="server" 
           FilterType="Numbers" TargetControlID="benTB"> 
          </asp:FilteredTextBoxExtender> 
         </td> 
         <td style="padding-right: 20px;"> 
          <asp:Label ID="Label2" runat="server" Text="Intendencia" class="innerControl"></asp:Label> 
         </td> 
         <td colspan="3"> 
          <asp:DropDownList ID="intDDL" runat="server" class="innerControl" Width="270px"> 
          </asp:DropDownList> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <asp:Label ID="Label9" runat="server" Text="Mes/Año" class="innerControl"></asp:Label> 
         </td> 
         <td> 
          <asp:DropDownList ID="mesDDL" runat="server" class="innerControl"> 
          </asp:DropDownList> 
         </td> 
         <td> 
         /
         </td> 
         <td> 
          <asp:DropDownList ID="anoDDL" runat="server" class="innerControl"> 
          </asp:DropDownList> 
         </td> 
         <td style="width: 50px;"> 
          <asp:Label ID="Label1" runat="server" Text="_________" class="innerControl" ForeColor="White"></asp:Label> 
         </td> 
         <td> 
          <asp:Label ID="Label10" runat="server" Text="Meses" class="innerControl"></asp:Label> 
         </td> 
         <td> 
          <asp:TextBox ID="mesesTB" runat="server" Width="77px" class="innerControl" 
           Height="23px"></asp:TextBox> 
          <asp:FilteredTextBoxExtender ID="TBExtender" runat="server" 
           FilterType="Numbers" TargetControlID="mesesTB"> 
          </asp:FilteredTextBoxExtender> 
         </td> 
        </tr>       
       </table>      
      </td> 
     </tr> 
    </table> 
</div> 

<br /> 
<asp:Label runat="server" Text="Características del Requerimiento:" class="popup"></asp:Label> 

<br /> 
<div class="popup" style="border: 1px solid #000; width: 600px;"> 
    <table style="height: 100px; width: 584px"> 
     <tr> 
      <td class="style1"> 
       <table> 
        <tr> 
         <td> 
          <asp:Label ID="Label3" runat="server" Text="Población:" class="innerControl"></asp:Label> 
         </td> 
         <td colspan="2"> 
          <asp:TextBox ID="pobTB" runat="server" Width="70px" class="innerControl"></asp:TextBox> 
         </td>        
         <td> 
          <asp:Label ID="Label5" runat="server" Text="Fundamentación:" class="innerControl"></asp:Label> 
         </td> 
         <td rowspan="3"> 
          <asp:TextBox ID="fundTB" runat="server" Width="221px" class="innerControl" 
           TextMode="MultiLine" Rows="7" Columns="35"></asp:TextBox> 
         </td> 
        </tr> 
        <tr> 
         <td style="padding-right: 20px;"> 
          <asp:Label ID="Label6" runat="server" Text="Tasa Desempleo:" class="innerControl"></asp:Label> 
         </td> 
         <td style="padding-right: 20px;"> 
          <asp:TextBox ID="empTB" runat="server" class="innerControl" Width="70px" ></asp:TextBox> 
          <asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender2" runat="server" 
           FilterType="Numbers, Custom" ValidChars=".," TargetControlID="empTB"> 
           </asp:FilteredTextBoxExtender>         
         </td> 
         <td> 
          % 
         </td> 
        </tr> 
        <tr> 
         <td style="padding-right: 20px;"> 
          <asp:Label ID="Label8" runat="server" Text="Tasa Pobreza:" class="innerControl"></asp:Label> 
         </td> 
         <td style="padding-right: 20px;"> 
          <asp:TextBox ID="pobrTB" runat="server" class="innerControl" Width="70px" ></asp:TextBox> 
          <asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender4" runat="server" 
           FilterType="Numbers, Custom" ValidChars=".," TargetControlID="pobrTB"> 
           </asp:FilteredTextBoxExtender> 
         </td> 
         <td> 
          % 
         </td> 
        </tr> 
       </table> 
      </td> 
     </tr> 
    </table> 
</div> 
<div class="popup" style="width: 601px;"> 
    <table> 
     <tr> 
      <td align="center" colspan="6" class="style2"> 
       <asp:Button ID="guardarNecesBTN" Text="Guardar" style="width: 110px;" 
        runat="server" onclick="guardarNecesBTN_Click" Visible="true"/> 
       <asp:Button ID="cerrarModBTN" Text="Cerrar" style="width: 110px;" 
        runat="server" onclick="closePopUp" Visible="false"/> 
      </td> 
     </tr> 
     <tr> 
      <td class="style2"> 
       <asp:Label ID="Test" Text="" runat="server"></asp:Label> 
      </td> 
     </tr> 
    </table> 
</div> 
</form> 

的HTML代碼,這是隱藏代碼的.cs

public partial class nueva_necesidad : System.Web.UI.Page 
{ 
    int fol = 0; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      loadMeses(); 
      loadAnos(); 
      loadIntendencias(); 
      String fec = DateTime.Today.Day.ToString(); 
      fec = fec + "-" + DateTime.Today.Month.ToString(); 
      fec = fec + "-" + DateTime.Today.Year.ToString(); 
      fecTB.Text = (fec); 
     } 
    }   

    protected void loadIntendencias() 
    //Loads the intDDL with values taken from a DB table 
    { 
     intDDL.Items.Clear(); 
     ListItem newItem = null; 

     List<entIntendencia> intList = new List<entIntendencia>(); 

     intList = boNeces.loadIntendencias(Convert.ToInt32(Session["reg_usu"].ToString()), Convert.ToInt32(Session["niv_usu"].ToString())); 

     foreach (entIntendencia i in intList) 
     { 
      newItem = new ListItem(); 
      newItem.Text = i.nombre; 
      newItem.Value = i.id_int.ToString(); 
      intDDL.Items.Add(newItem); 
     } 
    } 

    protected void loadMeses() 
    //Loads the mesDDL, pre-selecting the (current month + 2) from the DDL 
    { 
     DateTime d = DateTime.Today; 
     int mesNum = d.Month; 
     ListItem newItem = null; 

     List<String> meses = new List<String>(); 
     meses.Add("Enero"); 
     meses.Add("Febrero"); 
     meses.Add("Marzo"); 
     meses.Add("Abril"); 
     meses.Add("Mayo"); 
     meses.Add("Junio"); 
     meses.Add("Julio"); 
     meses.Add("Agosto"); 
     meses.Add("Septiembre"); 
     meses.Add("Octubre"); 
     meses.Add("Noviembre"); 
     meses.Add("Diciembre"); 

     for (int i = 0; i <= 11; i++) 
     { 
      newItem = new ListItem(); 
      newItem.Text = meses[i]; 
      newItem.Value = (i + 1).ToString(); 
      mesDDL.Items.Add(newItem); 
     } 

     if (d.Month != 11 && d.Month != 12) 
      mesDDL.SelectedIndex = mesNum + 1; 
     else 
     { 
      if (d.Month == 11) 
       mesDDL.SelectedIndex = 0; 
      else 
       mesDDL.SelectedIndex = 1; 
     } 
    } 

    protected void loadAnos() 
    //Loads the anoDDL with a list of two: the current year and the next one. 
    { 
     anoDDL.Items.Clear(); 
     DateTime d = DateTime.Today; 
     int ano = d.Year; 
     ListItem newItem = null; 

     List<int> list = new List<int>(); 

     list.Add(ano); 
     list.Add(ano + 1); 

     foreach (int an in list) 
     { 
      newItem = new ListItem(); 
      newItem.Text = an.ToString(); 
      newItem.Value = an.ToString(); 
      anoDDL.Items.Add(newItem); 
     } 
    } 

    protected bool validaNecesidad(out int inten, out int meses, out int mes, out int ano, out int pob) 
    //Validates the completion of the form's input fields. 
    { 
     inten = 0; 
     meses = 0; 
     mes = 0; 
     ano = 0; 
     pob = 0; 

     Test.Text = intDDL.SelectedValue.ToString() + mesesTB.Text; //This line is intended to load the values of one of the comboboxes and one of the input textboxes onto a label called "Test" that's on the form itself, to check if they do have values or not, this is the first instruction that executes when the save button is clicked. 

     if (!Int32.TryParse(intDDL.SelectedValue.ToString(), out inten)) 
     { 
      Test.Text = Test.Text + "Error en DDL intendencia"; 
      return false; 
     } 
     if (inten <= 0) 
     { 
      Test.Text = Test.Text + "Error en DDL intendencia"; 
      return false; 
     } 

     if (!Int32.TryParse(mesesTB.Text, out meses)) 
     { 
      Test.Text = Test.Text + "Error en TB meses"; 
      return false; 
     } 
     if (meses <= 0) 
     { 
      Test.Text = Test.Text + "Error en TB meses"; 
      return false; 
     } 

     if (!Int32.TryParse(mesDDL.SelectedValue, out mes)) 
     { 
      Test.Text = Test.Text + "Error en DLL mes"; 
      return false; 
     } 
     if (mes <= 0) 
     { 
      Test.Text = Test.Text + "Error en DLL mes"; 
      return false; 
     } 

     if (!Int32.TryParse(anoDDL.SelectedValue, out ano)) 
     { 
      Test.Text = Test.Text + "Error en DLL ano"; 
      return false; 
     } 
     if (ano <= 0) 
     { 
      Test.Text = Test.Text + "Error en DLL ano"; 
      return false; 
     } 

     if (!Int32.TryParse(pobTB.Text, out pob)) 
     { 
      Test.Text = Test.Text + "Error en TB pob"; 
      return false; 
     } 
     if (pob <= 0) 
     { 
      Test.Text = Test.Text + "Error en TB pob"; 
      return false; 
     } 

     if (string.IsNullOrWhiteSpace(empTB.Text)) 
     { 
      Test.Text = Test.Text + "Error en TB emp"; 
      return false; 
     } 

     if (string.IsNullOrWhiteSpace(pobrTB.Text)) 
     { 
      Test.Text = Test.Text + "Error en TB pobr"; 
      return false; 
     } 

     if (string.IsNullOrWhiteSpace(fundTB.Text)) 
     { 
      Test.Text = Test.Text + "Error en TB fund"; 
      return false; 
     } 
     return true; 
    } 

    protected void guardarNecesBTN_Click(object sender, EventArgs e) 
    //Saves a new record on the NECESIDADES table. 
    { 
     int res = 0;    

     int inten = 0; 
     int meses = 0; 
     int mes = 0; 
     int ano = 0; 
     int pob = 0; 

     if (validaNecesidad(out inten, out meses, out mes, out ano, out pob)) 
     { 
      res = boNeces.saveNeces(inten, meses, mes, ano, pob, empTB.Text, pobrTB.Text, fundTB.Text, Session["log_usu"].ToString()); 
      if (res != 0) 
      { 
       Test.Text = Test.Text + "Folio creado correctamente con el N° " + res + "."; 
       Test.ForeColor = System.Drawing.Color.Green; 
       guardarNecesBTN.Visible = false; 
       cerrarModBTN.Visible = true; 
       intDDL.SelectedIndex = 0; 
       loadMeses(); loadAnos(); 
       empTB.Text = ""; pobTB.Text = ""; pobrTB.Text = ""; fundTB.Text = "";      
      } 
      else 
      { 
       Test.Text = "Ha ocurrido un problema, favor intentar más tarde."; 
       Test.ForeColor = System.Drawing.Color.Gray; 
       guardarNecesBTN.Visible = false; 
       cerrarModBTN.Visible = true; 
       intDDL.SelectedIndex = 0; 
       loadMeses(); loadAnos(); 
       empTB.Text = ""; pobTB.Text = ""; pobrTB.Text = ""; fundTB.Text = ""; 
      } 
     } 
     else 
     { 
      Test.Text = Test.Text + "Faltan campos por completar."; 
      Test.ForeColor = System.Drawing.Color.Red; 
     } 
    } 

    protected void closePopUp(object sender, EventArgs e) 
    //Function used to call the JavaScript function that closes the modal popup. 
    { 
     ScriptManager.RegisterStartupScript(this, GetType(), "closeMod", "closeMod();", true); 
    } 
} 

最後編輯: 我已經解決了問題。這是因爲在IIS管理器下的應用程序配置中將啓用視圖狀態屬性設置爲false。將其更改爲true,現在它與本地工作相同。

+0

請張貼一些代碼 - 向我們展示您的前端代碼和.cs文件代碼 –

回答

0

首先,添加清除mesDDL下拉loadMeses()函數。其次,你的sql查詢在哪裏?我沒有找到。

+0

您的答案應該是一個評論。 intDDL是從數據庫表中加載的唯一ddl,它在'intList = boNeces.loadIntendencias(Convert.ToInt32(Session [「reg_usu」]。ToString()),Convert.ToInt32(Session [「niv_usu 「] .ToString()));來自業務層的」loadIntendencias()「方法從具有查詢的數據層調用另一個方法。你是否建議我發佈該代碼? –

+0

@glenLambert是的,我認爲是 – Khazratbek

相關問題