Mal
Mal

Reputation: 525

Button_Click without PostBack

In my below code when is button clicked the textbox value is being added to the database and the same is displayed in the DataBinder are reloading the page. Well, just like chat script I need whole process to be done without postback. So, how to pass the value from textbox to DataBinder without reloading the page. i.e. How to perform Button Click such that it do not postback.

protected void Page_Load(object sender, EventArgs e)
    {
            string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=repeater;" + "UID=root;" + "PASSWORD=****;" + "OPTION=3";
            OdbcConnection MyConnection = new OdbcConnection(MyConString);
            try
            {
                MyConnection.Open();
                OdbcCommand cmd = new OdbcCommand("Select message from table1", MyConnection);
                OdbcDataReader dr = cmd.ExecuteReader();
                ArrayList values = new ArrayList();
                while (dr.Read())
                {
                    if (!IsPostBack)
                    {
                    string ep = dr[0].ToString();
                    values.Add(new PositionData(ep));
                    Shout_Box.DataSource = values;
                    Shout_Box.DataBind();
                    }
                }
            }
            catch
            {
            }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {   
            string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=repeater;" + "UID=root;" + "PASSWORD=*****;" + "OPTION=3";
            OdbcConnection MyConnection = new OdbcConnection(MyConString);
            OdbcCommand cmd = new OdbcCommand("INSERT INTO table1(message)VALUES(?)", MyConnection);
            cmd.Parameters.Add("@email", OdbcType.VarChar, 255).Value = TextBox1.Text;
            MyConnection.Open();
            cmd.ExecuteNonQuery();
            MyConnection.Close();
    }

Upvotes: 1

Views: 978

Answers (2)

icaptan
icaptan

Reputation: 1535

Shortly: JavaScript, JQuery, AJAX

you have to use one of them, to succeed that; because any button on asp.net page if it runs on server, will already send postBack.

UpdatePanel is an AJAX component, you can use it, so that your button does not send postBack (or you are not effected)

Upvotes: 1

NakedBrunch
NakedBrunch

Reputation: 49413

A quick and dirty way would be to use an Update Panel. It would appear to your end-users to occur without a postback and you would only need to make minor changes to your page.

More information: Introduction to the UpdatePanel control (MSDN)

Upvotes: 2

Related Questions