Question : How to handle button click without reloading page first

Dear experts,

Is it possible to handle a button click without reloading the page before that? Because in handling the button press, I need to get the current colors of my GridView cells, and the colors are set on client, thus on reload the page they are lost before the click is handled.
Thanks.

Answer : How to handle button click without reloading page first

As far as the gridview1 in the JavaScript. I just forgot to take it out. It is not required in this application.

By way of a double apology allow me to give you this newer code which gets the coordinates from the selected cells, transmits them to the server, then resets the gridview with the appropriate selections.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
***ASPX PAGE***
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
<script type="text/javascript">

//Global Variable string containing information about cells in column 1 that are right-clicked
var GlobalVariable = '';

function codeForPoints(sender)
{
	//On right-click change cell color to blue
	sender.style.backgroundColor='Blue';

	//at same time gather coordinates and put into global variable
	var gridview1 = document.getElementsByTagName('table')[0];
	var y = sender.cellIndex;
	var x = sender.parentNode.rowIndex-1;
	GlobalVariable += x + ':' + y + ';';
}

function sendGridInfo(sender)
{
	//send global variable to server
	__doPostBack(sender.id,GlobalVariable);
}

</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server" />
    <div>
		<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
		</asp:GridView>
		<asp:Button ID="btnSubmit" runat="server" Text="Submit"
			OnClientClick="sendGridInfo(this);" onclick="btnSubmit_Click" />
		<div>String Sent To Server: <asp:Label ID="Lbl1" runat="server" /></div>
    </div>
    </form>
</body>
</html>

***CODE BEHIND***
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page 
{
    //public property to store the color sent in by __doPostBack
    public string coordinates
    { get; set; }


    protected void Page_Load(object sender, EventArgs e)
    {
        //Gathering data sent from custom JS __doPostBack
        //Could have done this any number of ways, its my sample so I chose this one :)
        coordinates = Request.Params.Get("__EVENTARGUMENT");

        //builds the gridview, of no consequence to this sample
        if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection(string.Format(@"Data Source=WT101\SQLEXPRESS;Integrated Security=True"));
            SqlCommand cmd = new SqlCommand("SELECT * FROM DevTable",conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataBind();
            dr.Close();
            conn.Close();
        }
    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes.Add("oncontextmenu", "codeForPoints(this); return false;");
        }
    }
    

    //takes the custom gridview information and uses it to populate a label
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //make list to hold coordinates
        List<KeyValuePair<string, string>> tableCoordinates = new List<KeyValuePair<string, string>>();
        Int32 counter = 0;

        //populate the list with delimited string sent in from _doPostBack()
        if(!string.IsNullOrEmpty(coordinates))
        {
            string[] split1 = coordinates.Split(new char[]{';'},StringSplitOptions.RemoveEmptyEntries);

            foreach (string xy in split1)
            {
                string[] split2 = split1[counter].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                tableCoordinates.Add(new KeyValuePair<string, string>(split2[0], split2[1]));
                counter += 1;
            }
        }

        //reset the proper background-color for the cells previously chosen in gridview
        counter = 0;
        foreach (KeyValuePair<string,string> kvp in tableCoordinates)
        {
            GridView1.Rows[Convert.ToInt32(kvp.Key)].Cells[Convert.ToInt32(kvp.Value)].BackColor = System.Drawing.Color.Blue;
            counter += 1;
        }
    }
}
Random Solutions  
 
programming4us programming4us