cnbandicoot
cnbandicoot

Reputation: 370

Don't reload DropDownList when OnClick ASP Button

I'm trying save the value of a DropDownList when I press a Button, but when I press this, it appears that refresh page and ever get the index 0 of the DropDownList. Here's the code from method btnAccept_Click from .cs:

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>");
    }
}

The value 'comboModifica.selectedIndex' returns all the time the index 0

Here the code of my web form 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>

How Can I get the DropDownList's value?

Upvotes: 0

Views: 998

Answers (1)

sujith karivelil
sujith karivelil

Reputation: 29026

Your problem is that, the combobox get initialized in each postback. So you need to identify whether it is a postback or not. The init the combobox only when it is not a postback. Let BindMyCbo() is the method that's binds all values to the Required comboBox, Here it is comboModifica. Then you need to call the method only when The page load is not a PostBack. so the method call(within your page_load event) will be like the following:

 if (!IsPostBack)
    {
        BindMyCbo();
        //Rest of code
    }

So the combobox get initialized in page load and remains same after postback

Upvotes: 2

Related Questions