C#DropDownList只在服务器上表现得很奇怪,而不是在本地
本文关键字:服务器 C#DropDownList | 更新日期: 2023-09-27 18:23:56
我有一个表单,其中包含一些文本输入字段和3个组合框,还有一个提交按钮,用于将表单的数据保存到数据库上的表中。因此,当我点击提交按钮时,经过一番调试,我发现在点击它的那一刻,组合框没有选择值,从而带来错误和异常,并且显然没有将数据保存到数据库中。但不仅如此,此外,在单击提交按钮后,组合框会丢失所有值,填充它们的完整列表也会消失,就好像它们没有初始化一样。
所有这一切的可悲之处(因为我知道我可以再检查一下我的代码)是,当我做完全相同的事情时,在我的ASP.NET Visual Studio服务器(在我的本地机器上)上使用完全相同的代码(相同的dll和所有内容),一切都会完美地工作,因此我会将数据写入相同的远程数据库,在单击提交按钮后,数据会保存到数据库中,并且表单对所有组合框保持正常,就像它们在单击之前一样。
为什么这种情况只发生在服务器上?为什么它可以在本地完全没有问题地从数据库中写入和读取?
我认为这可能与我的Page_Load方法上的PostBack有关(我在那里初始化了组合框,但前提是它不是PostBack)。。。那么,这可能是一个与IIS服务器如何使用PostBacks(可能与本地ASP.NET服务器不同)有关的问题吗?
编辑我正在添加表单的HTML代码
<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>
这就是.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管理器下应用程序配置的enable view state属性设置为false。把它改成了true,现在它和local一样工作。
首先,在loadMeses()函数中添加清除mesDDL下拉列表。其次,您的sql查询在哪里?我没发现。