Button to call another Aspx page

Discussion in 'ASP .Net' started by egsdar, Oct 16, 2008.

  1. egsdar

    egsdar Guest

    Hello, i have implemented a button in some other pages that calls another
    one, the problem is that everytime I hit the button he goes for the postback
    code and I already disable that part. this is my code:

    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="SIP ::
    Nichos de Mercado" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %>

    <script runat="server">
    Sub Page_Load(ByVal obj As Object, ByVal e As EventArgs)
    If IsPostBack() Then
    Dim objConn As New
    OleDbConnection("Provider=SQLNCLI;Server=db2fx981\comware;Database=SIP;Trusted_Connection=yes;")
    objConn.Open()
    Dim sSQL, Resul As String
    sSQL = "Insert into Nicho (Descripcion) values ('" &
    Descripcion.Text & "')"
    Dim objCmd As New OleDbCommand(sSQL, objConn)

    Resul = objCmd.ExecuteNonQuery
    If Resul Then
    LblSaved.Visible = True
    End If
    End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
    System.EventArgs)
    Response.Redirect("Nichos_con.aspx")
    End Sub
    </script>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2"
    Runat="Server">
    <span style="font-size: 24pt">Nichos de Mercado<br />
    <br />
    </span><span style="font-size: 11pt; font-family: Tahoma">Por favor
    ingrese los datos
    solicitados en el formulario.<br />
    <br />
    <br />
    <br />
    <table border="0" cellpadding="0" cellspacing="0" style="left:
    220px; width: 306px;
    position: relative; top: 3px">
    <tr>
    <td style="width: 100px; height: 15px; text-align: center">
    <strong><span style="font-size: 10pt">Nicho de
    Mercado</span></strong></td>
    <td style="width: 100px; height: 15px">
    <asp:TextBox ID="Descripcion" runat="server"
    Style="position: relative"></asp:TextBox></td>
    </tr>
    <tr>
    <td colspan="2" style="text-align: center">
    <asp:Button ID="Button1" runat="server" Font-Bold="True"
    Font-Names="Tahoma" Style="position: relative"
    Text="Salvar" OnClick="Button1_Click" /></td>
    </tr>
    </table>
    <br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
    runat="server" ErrorMessage="El campo de Nicho de Mercado no debe estar en
    blanco"
    Font-Bold="True" Font-Names="Tahoma" Font-Size="X-Small"
    Style="left: 196px;
    position: relative; top: -7px"
    ControlToValidate="Descripcion"></asp:RequiredFieldValidator>
    <asp:Label ID="LblSaved" runat="server" Font-Bold="True"
    Font-Names="MS Reference Sans Serif"
    Font-Size="X-Small" Style="left: -73px; position: relative; top:
    20px" Text=":: Registro Salvado ::"
    Visible="False"></asp:Label><br />
    <br />
    <br />
    <br />
    <asp:Button ID="Button2" runat="server" CausesValidation="False"
    Font-Bold="True"
    OnClick="Button1_Click" Style="left: 273px; position: relative;
    top: 1px" Text="Consultar Nichos" /><br />
    </span>
    </asp:Content>


    The button is Button2, how can I solve this?
    egsdar, Oct 16, 2008
    #1
    1. Advertising

  2. You are obviously using VB.NET based on your code. However, I am noticing
    that you are assigning eventhandlers to your Buttons using the OnClick
    attribute rather than the Handles clause (which is the preferred way to do
    it in VB.NET). Although either one should work, and I will admit that I did
    not actually test your code, try the following:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Button1.Click, Button2.Click
    Response.Redirect("Nichos_con.aspx")
    End Sub

    If you use the above code you can remove the OnClick attribute from your
    Button tags (although it shouldn't hurt anything if you don't). See if it
    does anything different with the above code. Also, I am getting the
    impression that you have all your code in one file (is this correct?). It is
    considered better practice, and definitely more organized, to use the
    following three files (which is how Visual Studio 2005+ does it by default):

    *.aspx
    *.aspx.designer.vb (this file is automatically generated when you add a
    control to *.aspx)
    *.aspx.vb

    I would suggest taking this into account. Good Luck!
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "egsdar" <> wrote in message
    news:...
    > Hello, i have implemented a button in some other pages that calls another
    > one, the problem is that everytime I hit the button he goes for the
    > postback
    > code and I already disable that part. this is my code:
    >
    > <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="SIP ::
    > Nichos de Mercado" %>
    > <%@ Import Namespace="System.Data" %>
    > <%@ Import Namespace="System.Data.OleDb" %>
    >
    > <script runat="server">
    > Sub Page_Load(ByVal obj As Object, ByVal e As EventArgs)
    > If IsPostBack() Then
    > Dim objConn As New
    > OleDbConnection("Provider=SQLNCLI;Server=db2fx981\comware;Database=SIP;Trusted_Connection=yes;")
    > objConn.Open()
    > Dim sSQL, Resul As String
    > sSQL = "Insert into Nicho (Descripcion) values ('" &
    > Descripcion.Text & "')"
    > Dim objCmd As New OleDbCommand(sSQL, objConn)
    >
    > Resul = objCmd.ExecuteNonQuery
    > If Resul Then
    > LblSaved.Visible = True
    > End If
    > End If
    > End Sub
    >
    > Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs)
    > Response.Redirect("Nichos_con.aspx")
    > End Sub
    > </script>
    >
    > <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2"
    > Runat="Server">
    > <span style="font-size: 24pt">Nichos de Mercado<br />
    > <br />
    > </span><span style="font-size: 11pt; font-family: Tahoma">Por favor
    > ingrese los datos
    > solicitados en el formulario.<br />
    > <br />
    > <br />
    > <br />
    > <table border="0" cellpadding="0" cellspacing="0" style="left:
    > 220px; width: 306px;
    > position: relative; top: 3px">
    > <tr>
    > <td style="width: 100px; height: 15px; text-align: center">
    > <strong><span style="font-size: 10pt">Nicho de
    > Mercado</span></strong></td>
    > <td style="width: 100px; height: 15px">
    > <asp:TextBox ID="Descripcion" runat="server"
    > Style="position: relative"></asp:TextBox></td>
    > </tr>
    > <tr>
    > <td colspan="2" style="text-align: center">
    > <asp:Button ID="Button1" runat="server"
    > Font-Bold="True"
    > Font-Names="Tahoma" Style="position: relative"
    > Text="Salvar" OnClick="Button1_Click" /></td>
    > </tr>
    > </table>
    > <br />
    > <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
    > runat="server" ErrorMessage="El campo de Nicho de Mercado no debe estar en
    > blanco"
    > Font-Bold="True" Font-Names="Tahoma" Font-Size="X-Small"
    > Style="left: 196px;
    > position: relative; top: -7px"
    > ControlToValidate="Descripcion"></asp:RequiredFieldValidator>
    > <asp:Label ID="LblSaved" runat="server" Font-Bold="True"
    > Font-Names="MS Reference Sans Serif"
    > Font-Size="X-Small" Style="left: -73px; position: relative;
    > top:
    > 20px" Text=":: Registro Salvado ::"
    > Visible="False"></asp:Label><br />
    > <br />
    > <br />
    > <br />
    > <asp:Button ID="Button2" runat="server" CausesValidation="False"
    > Font-Bold="True"
    > OnClick="Button1_Click" Style="left: 273px; position: relative;
    > top: 1px" Text="Consultar Nichos" /><br />
    > </span>
    > </asp:Content>
    >
    >
    > The button is Button2, how can I solve this?
    >
    >
    Nathan Sokalski, Oct 16, 2008
    #2
    1. Advertising

  3. egsdar

    bruce barker Guest

    the onload event always runs before the click event. unlike a winform
    app, in asp.net the full page processing cycle happens on the first hit
    to the and every postback (any server callback).

    -- bruce (sqlwork.com)

    egsdar wrote:
    > Hello, i have implemented a button in some other pages that calls another
    > one, the problem is that everytime I hit the button he goes for the postback
    > code and I already disable that part. this is my code:
    >
    > <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="SIP ::
    > Nichos de Mercado" %>
    > <%@ Import Namespace="System.Data" %>
    > <%@ Import Namespace="System.Data.OleDb" %>
    >
    > <script runat="server">
    > Sub Page_Load(ByVal obj As Object, ByVal e As EventArgs)
    > If IsPostBack() Then
    > Dim objConn As New
    > OleDbConnection("Provider=SQLNCLI;Server=db2fx981\comware;Database=SIP;Trusted_Connection=yes;")
    > objConn.Open()
    > Dim sSQL, Resul As String
    > sSQL = "Insert into Nicho (Descripcion) values ('" &
    > Descripcion.Text & "')"
    > Dim objCmd As New OleDbCommand(sSQL, objConn)
    >
    > Resul = objCmd.ExecuteNonQuery
    > If Resul Then
    > LblSaved.Visible = True
    > End If
    > End If
    > End Sub
    >
    > Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs)
    > Response.Redirect("Nichos_con.aspx")
    > End Sub
    > </script>
    >
    > <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2"
    > Runat="Server">
    > <span style="font-size: 24pt">Nichos de Mercado<br />
    > <br />
    > </span><span style="font-size: 11pt; font-family: Tahoma">Por favor
    > ingrese los datos
    > solicitados en el formulario.<br />
    > <br />
    > <br />
    > <br />
    > <table border="0" cellpadding="0" cellspacing="0" style="left:
    > 220px; width: 306px;
    > position: relative; top: 3px">
    > <tr>
    > <td style="width: 100px; height: 15px; text-align: center">
    > <strong><span style="font-size: 10pt">Nicho de
    > Mercado</span></strong></td>
    > <td style="width: 100px; height: 15px">
    > <asp:TextBox ID="Descripcion" runat="server"
    > Style="position: relative"></asp:TextBox></td>
    > </tr>
    > <tr>
    > <td colspan="2" style="text-align: center">
    > <asp:Button ID="Button1" runat="server" Font-Bold="True"
    > Font-Names="Tahoma" Style="position: relative"
    > Text="Salvar" OnClick="Button1_Click" /></td>
    > </tr>
    > </table>
    > <br />
    > <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
    > runat="server" ErrorMessage="El campo de Nicho de Mercado no debe estar en
    > blanco"
    > Font-Bold="True" Font-Names="Tahoma" Font-Size="X-Small"
    > Style="left: 196px;
    > position: relative; top: -7px"
    > ControlToValidate="Descripcion"></asp:RequiredFieldValidator>
    > <asp:Label ID="LblSaved" runat="server" Font-Bold="True"
    > Font-Names="MS Reference Sans Serif"
    > Font-Size="X-Small" Style="left: -73px; position: relative; top:
    > 20px" Text=":: Registro Salvado ::"
    > Visible="False"></asp:Label><br />
    > <br />
    > <br />
    > <br />
    > <asp:Button ID="Button2" runat="server" CausesValidation="False"
    > Font-Bold="True"
    > OnClick="Button1_Click" Style="left: 273px; position: relative;
    > top: 1px" Text="Consultar Nichos" /><br />
    > </span>
    > </asp:Content>
    >
    >
    > The button is Button2, how can I solve this?
    >
    >
    bruce barker, Oct 16, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hrvoje Vrbanc
    Replies:
    1
    Views:
    1,085
    Hrvoje Vrbanc
    Aug 27, 2003
  2. Sriram Mallajyosula

    How do I post data from aspx page to another aspx page.

    Sriram Mallajyosula, Nov 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    977
    Steve C. Orr [MVP, MCSD]
    Nov 5, 2003
  3. =?Utf-8?B?Uk1M?=
    Replies:
    1
    Views:
    560
    =?Utf-8?B?QW5keSBMdWtzaWM=?=
    Sep 16, 2005
  4. Progman
    Replies:
    2
    Views:
    1,906
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 13, 2006
  5. MadHatter51
    Replies:
    1
    Views:
    864
    Hakan Lambracht
    May 17, 2006
Loading...

Share This Page