OnClick ASP 按钮时不要重新加载 DropDownList
Don't reload DropDownList when OnClick ASP Button
我试图在按下按钮时保存 DropDownList 的值,但是当我按下此按钮时,似乎刷新页面并获得了 DropDownList 的索引 0。
这是来自 .cs:
方法 btnAccept_Click
的代码
if (comboOpciones.SelectedIndex == 1)
{
if (serv.modifyMovie(comboModifica.selectedIndex + 1, textboxTitulo.Text, Convert.ToInt32(textboxAño.Text), textBoxGenero.Text, textboxNacionalidad.Text, 0) == 1)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert(' Succesful Modify ');</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert(' Unsuccesful modify');</script>");
}
}
值'comboModifica.selectedIndex'returns一直索引0
这是我的网络表单的代码 ASP:
<table class="centrada">
<tr>
<td>
</td>
<td>
<p>¿Qué desea hacer?</p>
<asp:DropDownList ID="comboOpciones" runat="server" AutoPostBack="True" OnSelectedIndexChanged="comboOpciones_SelectedIndexChanged">
<asp:ListItem>Añadir</asp:ListItem>
<asp:ListItem>Modificar</asp:ListItem>
</asp:DropDownList>
</td>
<td>
</td>
<td>
<div runat="server" id="divModificar">
<p>Introduce ID de película a modificar:</p>
<asp:DropDownList ID="comboModifica" runat="server"></asp:DropDownList>
</div>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Nuevo título:"></asp:Label>
</td>
<td>
<asp:TextBox ID="textboxTitulo" runat="server" MaxLength="30"></asp:TextBox>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text="Año:"></asp:Label>
</td>
<td>
<asp:TextBox ID="textboxAño" runat="server" MaxLength="4"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Género:"></asp:Label>
</td>
<td>
<asp:TextBox ID="textBoxGenero" runat="server" MaxLength="15"></asp:TextBox>
</td>
<td>
<asp:Label ID="Label4" runat="server" Text="Nacionalidad:"></asp:Label>
</td>
<td>
<asp:TextBox ID="textboxNacionalidad" runat="server" MaxLength="25"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="Director:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="comboDirectores" runat="server"></asp:DropDownList>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="4">
<asp:Button ID="btnAccept" runat="server" Text="Enviar" AutoPostBack="false" OnClick="btnAccept_Click"/>
</td>
</tr>
</table>
如何获取 DropDownList 的值?
您的问题是,组合框在每次回发时都会被初始化。所以需要判断是不是postback。只有当它不是回发时才初始化组合框。让 BindMyCbo()
是将所有值绑定到 Required 组合框的方法,这里是 comboModifica
。然后只有当页面加载不是 PostBack 时才需要调用该方法。因此方法调用(在您的 page_load 事件中)将如下所示:
if (!IsPostBack)
{
BindMyCbo();
//Rest of code
}
因此组合框在页面加载时初始化并在回发后保持不变
我试图在按下按钮时保存 DropDownList 的值,但是当我按下此按钮时,似乎刷新页面并获得了 DropDownList 的索引 0。 这是来自 .cs:
方法btnAccept_Click
的代码
if (comboOpciones.SelectedIndex == 1)
{
if (serv.modifyMovie(comboModifica.selectedIndex + 1, textboxTitulo.Text, Convert.ToInt32(textboxAño.Text), textBoxGenero.Text, textboxNacionalidad.Text, 0) == 1)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert(' Succesful Modify ');</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert(' Unsuccesful modify');</script>");
}
}
值'comboModifica.selectedIndex'returns一直索引0
这是我的网络表单的代码 ASP:
<table class="centrada">
<tr>
<td>
</td>
<td>
<p>¿Qué desea hacer?</p>
<asp:DropDownList ID="comboOpciones" runat="server" AutoPostBack="True" OnSelectedIndexChanged="comboOpciones_SelectedIndexChanged">
<asp:ListItem>Añadir</asp:ListItem>
<asp:ListItem>Modificar</asp:ListItem>
</asp:DropDownList>
</td>
<td>
</td>
<td>
<div runat="server" id="divModificar">
<p>Introduce ID de película a modificar:</p>
<asp:DropDownList ID="comboModifica" runat="server"></asp:DropDownList>
</div>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Nuevo título:"></asp:Label>
</td>
<td>
<asp:TextBox ID="textboxTitulo" runat="server" MaxLength="30"></asp:TextBox>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text="Año:"></asp:Label>
</td>
<td>
<asp:TextBox ID="textboxAño" runat="server" MaxLength="4"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Género:"></asp:Label>
</td>
<td>
<asp:TextBox ID="textBoxGenero" runat="server" MaxLength="15"></asp:TextBox>
</td>
<td>
<asp:Label ID="Label4" runat="server" Text="Nacionalidad:"></asp:Label>
</td>
<td>
<asp:TextBox ID="textboxNacionalidad" runat="server" MaxLength="25"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="Director:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="comboDirectores" runat="server"></asp:DropDownList>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="4">
<asp:Button ID="btnAccept" runat="server" Text="Enviar" AutoPostBack="false" OnClick="btnAccept_Click"/>
</td>
</tr>
</table>
如何获取 DropDownList 的值?
您的问题是,组合框在每次回发时都会被初始化。所以需要判断是不是postback。只有当它不是回发时才初始化组合框。让 BindMyCbo()
是将所有值绑定到 Required 组合框的方法,这里是 comboModifica
。然后只有当页面加载不是 PostBack 时才需要调用该方法。因此方法调用(在您的 page_load 事件中)将如下所示:
if (!IsPostBack)
{
BindMyCbo();
//Rest of code
}
因此组合框在页面加载时初始化并在回发后保持不变