Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cant get color set in HTML Javascript 1

Status
Not open for further replies.

ISPrincess

Programmer
Feb 22, 2002
318
0
0
US
I have posted this in asp.net forum also, but since it involves Java, I wanted to put it here also.

In Server VB:
Code:
If dgMoves.Items(i).BackColor.Equals(System.Drawing.Color.LightSteelBlue) Then
                SetAlertMessage("Selected: " & i)
      End If

never equates to TRUE.


In server VB code, I keep getting color.empty returned event though I have set it in HTML

In my HTML I have Javascript . (I had to use the #b0c4de and not the color name because the javascript wasnt behaving)


Code:
SRow.style.backgroundColor = ( bg == '#b0c4de' ) ? '#ffffff' : '#b0c4de';

Also, dgMoves.Items(i).BackColor.ToString returns Color.Empty.

I think I amm missing something very obvious (as usual), please, can anyone see what is wrong here?

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
I have had and am having an afternoon of meetings so as soon as I can get back on this and have a moment I will definetly visit your site and other sponsors!

Since I beleive you have given me all the information I need (and then some!) I give you a star (wish I could give you 10).

Again, thank you so much for your wonderful assistance and education! Very very much appreciated!




PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
I am sorry - I still cannot "read" the contents of isSelected once the "submit" type button was clicked on the vb.net server code.

Help!

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
It's been awhile, you'll have to refresh my memory. All of the code I've posted and written for this thread/problem is client-side. That was the point, right? So you won't be able to work with the table elements server-side, in a button click event.

My code changes the color and contents of a cell CLIENT-SIDE. When you submit to server-side code, what do you want to do?

If, for example, you want to know WHICH row was selected, in order to do further processing, you'll have to add something to the ViewState for server code to look at.

Remind me what we're trying to accomplish.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting


If this post was helpful, click some ads! My personal site is also advertiser-supported. Hint, hint!
 
That is exactly what I need to do:


TGreer:
If, for example, you want to know WHICH row was selected, in order to do further processing, you'll have to add something to the ViewState for server code to look at.


The grid (which will be a multi-select grid) can get quite large and a post-back to server for every click/select is not feasible, so I want to be able to do something will all the selected rows - server-side once they click on a button.

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
In this explanation, "CS" means client-side/JavaScript, and "SS" means "server-side/C#/code-behind".

I revised my project at:


to show you how to:

1) CS: select a row

2) CS: change row's color

3) CS: change value of cell in row

4) CS: Store id of an element in a cell in that row in a form variable.

5) CS: Store text/HTML of an element in a cell in that row in a form variable.

6) SS: get the value of the form element used to store what was written in 4 & 5.

7) SS: Write a script to display those values in an alert

8) SS: Register that script to run when the resposne returns to user.

Note that this is just an example. You'll need to modify it to make the TextBox1 hidden (add "VISIBILITY: hidden;" to the style declaration). You'll have to modify the code to APPEND row elements/values to the TextBox1, rather than just set them every time a row is selected.

If you need any more help, just ask!



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Thanks! This does seem to be doing something similar to what I would like to do. Is it possible for you to post both the HTML and the Server Side code?

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
What the heck:

The code-behind:

Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ISPrincess
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid datagrid1;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.TextBox TextBox1;

		public DataTable myPrinters;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// create DataTable objects to bind to datagrid:
			myPrinters = new DataTable("myPrinters");
			DataColumn workCol = myPrinters.Columns.Add("DeviceNo", typeof(string));

			DataRow row = myPrinters.NewRow();
			row["DeviceNo"] = "HP Laser";
			myPrinters.Rows.Add(row);

			row = myPrinters.NewRow();
			row["DeviceNo"] = "Xerox 2045";
			myPrinters.Rows.Add(row);

			row = myPrinters.NewRow();
			row["DeviceNo"] = "Color Laser";
			myPrinters.Rows.Add(row);

			datagrid1.DataSource = myPrinters;
			datagrid1.DataBind();
		}

		private void datagrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
			if ((e.Item.ItemType != ListItemType.Header) & (e.Item.ItemType != ListItemType.Footer))
			{
				e.Item.Attributes.Add("onClick","HighlightRow(this);");
			}
																							   
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.datagrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.datagrid1_ItemDataBound);
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Button1_Click(object sender, System.EventArgs e)
		{
			// What row did the user select? What is it's current value?
			string curr_row = TextBox1.Text;
			Page.RegisterStartupScript("afterPostBack","<script>alert('" + curr_row + "')</script>");
		}

	}
}

The aspx/HTML/JavaScript:

Code:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ISPrincess.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>TestMultiSelect</title>
		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
		<meta content="C#" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta content="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"[/URL] name="vs_targetSchema">
		<script type="text/javascript">
	function HighlightRow(SRow)    
	{
		var bg = SRow.style.backgroundColor;
		SRow.cells[0].innerHTML = SRow.cells[0].innerHTML + "_sel";
		SRow.style.backgroundColor = ( bg == '#b0c4de' ) ? '#ffffff' : '#b0c4de';
		document.getElementById("TextBox1").value = SRow.cells[1].childNodes[0].id + ";" +
		SRow.cells[0].innerHTML;
	}		
		</script>
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<asp:datagrid id="datagrid1" runat="server" height="50%" AutoGenerateColumns="false" AllowSorting="True"
				EnableViewState="true">
				<Columns>
					<asp:BoundColumn HeaderText="Printer #" DataField="DeviceNo" />
					<asp:TemplateColumn HeaderText="IsSelected">
						<ItemTemplate>
							<asp:TextBox ID="isSelected" Runat="server" />
						</ItemTemplate>
					</asp:TemplateColumn>
				</Columns>
			</asp:datagrid><asp:textbox id="TextBox1" style="Z-INDEX: 102; LEFT: 312px; POSITION: absolute; TOP: 64px" runat="server"
				Width="288px" Height="72px" TextMode="MultiLine"></asp:textbox><asp:button id="Button1" style="Z-INDEX: 101; LEFT: 312px; POSITION: absolute; TOP: 16px" runat="server"
				Text="Button"></asp:button></form>
	</body>
</HTML>

Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
This is exactly what I have been trying to do All day today in some fashion or another.

What it all came down to was that I did not have runat=Server in my asp:textbox control!

This code works! And I am very glad you posted it, I know it will help others as well. I have seen a great deal of turmoil over this in other sites too with no solution.

Thanks TGreer - from myself and anyone else who finds this post and benifits!


PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Let me know if I should start a new thread.

If I can set the value of a textbox client-side and read it server-side, then why cant I imbed a textbox in the grid and set that to '1' client-side and the read through the rows of the grid for that value server-side?

I think you trying to get that across to me earlier, but again I do not know enouph about Javascript to code that.

I could not read the values on the server-side when I set the value by using .innerHTML = "1".

Can you show me what the Javascript would be to set a text box value inside the datagrid like the following incorrect syntax:

Code:
SRow.getElementById('isSelected').value = ( SRow.getElementById('isSelected').value == '1' ) ? '0' : '1';

Then can you show me how you would read it on the server?

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top