***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;
}
}
}
|