积极答复者
给GridView的某个模板TextBox添加背景色的代码,错在哪里了啊?

问题
-
我要给TextBox_ManageProjectR添加背景色,代码如下,最后一句错了!请问要如何修改啊?
protected void Button_Update_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView_Search.Rows.Count; i++)
{
GridViewRow row = GridView_Search.Rows[i];
((TextBox)row.Cells[0].FindControl("TextBox_ManageProjectR")).BackColor = "Red";
}
}C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已移动 ThankfulHeart 2012年12月14日 5:12 asp.net控件问题 (发件人:Visual C#)
答案
-
@问题1:按照您的提示我修改了代码并测试,我的ColorTable只写了两行的数据,于是就会出现如图1所示的错误,问题我写在图1的注释里了!(后面还有图3,是ColorTable的内容)
先判断你的myColor是否为空,然后继续。
@问题2:我把myColor = db.ColorTable.Where(c => c.ContractNumberIDC.ToString() == "1").FirstOrDefault();中改成了“1”,能运行,但是为什么Cell[4]中整个列都是红色了呢?
原因很简单——你写入了“RowDataBound”事件中,这样意味着每次重复绑定都会执行这个事件,而且你每行都满足条件,自然每一行第四列就设置成红色了,建议你看看逻辑是否出问题。
@这个背景色的效果是个红色方框框,而我是要给GridView中的模板TextBox添加背景底色,该怎么修改呢?
使用FindControl(e的Row方法)找出这个TextBox,设置其BackColor属性。
PS:linjiangxian11:你不必把每一个帖子都增加有用标记(左边绿色三角),只有对有帮助才点击。
- 已编辑 ThankfulHeart 2012年12月20日 2:08
- 已标记为答案 Song TianModerator 2012年12月20日 10:39
全部回复
-
给textbox加背景色不要服务器端这么麻烦吧,静态的用css,动态的用js。。。在客户端完成
- 已建议为答案 Willin Wang 2012年12月18日 1:03
- 取消建议作为答案 linjiangxian11 2012年12月18日 2:47
-
能否具体些呢?我也想用css或js,可是该如何入手呢?能否给出示例,下面是我的前台和后台代码,还有一个记录颜色的sql表。下面这个地址有最为详细的全部代码和示意图:
http://social.msdn.microsoft.com/Forums/zh-CN/295/thread/3ee68065-1040-4121-8b17-d298293fee21
-------------前台代码1--------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Color01.aspx.cs" Inherits="Try_Color01" %> <!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></title> <style type="text/css"> .style1 { width: 100%; } </style> </head> <body> <form id="form1" runat="server"> <div> <table cellpadding="0" cellspacing="0" class="style1"> <tr> <td><asp:Button ID="Button_Update" runat="server" Text="更新" onclick="Button_Update_Click" /> <asp:GridView ID="GridView_Search" runat="server" AutoGenerateColumns="False" DataKeyNames="ContractNumberID" DataSourceID="SqlDataSource_Search" onrowdatabound="GridView_Search_RowDataBound"> <Columns> <asp:BoundField DataField="ContractNumberID" HeaderText="ContractNumberID" InsertVisible="False" ReadOnly="True" SortExpression="ContractNumberID" /> <asp:TemplateField HeaderText="ContractNumber" SortExpression="ContractNumber"> <ItemTemplate> <asp:TextBox ID="TextBox_ContractNumber" runat="server" Text='<%# Bind("ContractNumber") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="LeaseDateFrom" SortExpression="LeaseDateFrom"> <ItemTemplate> <asp:TextBox ID="TextBox_LeaseDateFrom" runat="server" Text='<%# Bind("LeaseDateFrom") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Month" SortExpression="Month"> <ItemTemplate> <asp:TextBox ID="TextBox_Month" runat="server" Text='<%# Bind("Month") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="MonthRent" SortExpression="MonthRent"> <ItemTemplate> <asp:TextBox ID="TextBox_MonthRent" runat="server" Text='<%# Bind("MonthRent") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="MonthRentPaid" SortExpression="MonthRentPaid"> <ItemTemplate> <asp:TextBox ID="TextBox_MonthRentPaid" runat="server" Text='<%# Bind("MonthRentPaid") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </td> </tr> <tr> <td> <asp:SqlDataSource ID="SqlDataSource_Search" runat="server" ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" DeleteCommand="DELETE FROM [RentTableA] WHERE [ContractNumberID] = @ContractNumberID" InsertCommand="INSERT INTO [RentTableA] ([ContractNumber], [LeaseDateFrom], [Month], [MonthRent], [MonthRentPaid]) VALUES (@ContractNumber, @LeaseDateFrom, @Month, @MonthRent, @MonthRentPaid)" SelectCommand="SELECT [ContractNumberID], [ContractNumber], [LeaseDateFrom], [Month], [MonthRent], [MonthRentPaid] FROM [RentTableA]" UpdateCommand="UPDATE [RentTableA] SET [ContractNumber] = @ContractNumber, [LeaseDateFrom] = @LeaseDateFrom, [Month] = @Month, [MonthRent] = @MonthRent, [MonthRentPaid] = @MonthRentPaid WHERE [ContractNumberID] = @ContractNumberID"> <DeleteParameters> <asp:Parameter Name="ContractNumberID" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="ContractNumber" Type="String" /> <asp:Parameter Name="LeaseDateFrom" DbType="Date" /> <asp:Parameter Name="Month" DbType="Date" /> <asp:Parameter Name="MonthRent" Type="Int32" /> <asp:Parameter Name="MonthRentPaid" Type="Int32" /> <asp:Parameter Name="ContractNumberID" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="ContractNumber" Type="String" /> <asp:Parameter Name="LeaseDateFrom" DbType="Date" /> <asp:Parameter Name="Month" DbType="Date" /> <asp:Parameter Name="MonthRent" Type="Int32" /> <asp:Parameter Name="MonthRentPaid" Type="Int32" /> </InsertParameters> </asp:SqlDataSource> </td> </tr> </table> </div> </form> </body> </html>
---------------------------------------------后台代码2------------------------------------------------------
using System; using System.Collections; 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; public partial class Try_Color01 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button_Update_Click(object sender, EventArgs e) { for (int i = 0; i < GridView_Search.Rows.Count; i++) { GridViewRow row = GridView_Search.Rows[i]; SqlDataSource_Search.UpdateParameters[0].DefaultValue = ((TextBox)row.Cells[0].FindControl("TextBox_ContractNumber")).Text; SqlDataSource_Search.UpdateParameters[1].DefaultValue = ((TextBox)row.Cells[0].FindControl("TextBox_LeaseDateFrom")).Text; SqlDataSource_Search.UpdateParameters[2].DefaultValue = ((TextBox)row.Cells[0].FindControl("TextBox_Month")).Text; SqlDataSource_Search.UpdateParameters[3].DefaultValue = ((TextBox)row.Cells[0].FindControl("TextBox_MonthRent")).Text; SqlDataSource_Search.UpdateParameters[4].DefaultValue = ((TextBox)row.Cells[0].FindControl("TextBox_MonthRentPaid")).Text; SqlDataSource_Search.UpdateParameters[5].DefaultValue = GridView_Search.DataKeys[i].Values[0].ToString(); SqlDataSource_Search.Update(); } } protected void GridView_Search_RowDataBound(object sender, GridViewRowEventArgs e) { DataClassesDataContext db = new DataClassesDataContext(); if (e.Row.RowType != DataControlRowType.Header) { string id = e.Row.Cells[0].Text; Response.Write(id); string colorMonthRent; string colorMonthRentPaid; ColorTable myColor = db.ColorTable.Where(c => (c.ContractNumberIDC) == (Convert.ToInt32(id))).FirstOrDefault(); if (myColor.MonthRentC != null) colorMonthRent = myColor.MonthRentC; else//这句else去掉会出错,错误提示“colorMonthRent未赋值”,有时候ColorMonthRent是null值,这种情况如何处理 colorMonthRent = "white"; if (myColor.MonthRentPaidC != null) colorMonthRentPaid = myColor.MonthRentPaidC; else//这句else去掉会出错 colorMonthRentPaid = "white"; e.Row.Cells[4].Attributes.Add("Style", "background-Color:" + colorMonthRent + ""); e.Row.Cells[5].Attributes.Add("style", "background-Color:" + colorMonthRentPaid + ""); } } }
-----------------------------------------SQL-------------------------------------
---------------------------------ColorTable-------------------------------- USE [长铁物业公司资产数据库] GO /****** Object: Table [dbo].[ColorTable] Script Date: 10/22/2012 17:13:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[ColorTable]( [ContractNumberIDC] [int] NOT NULL, [MonthRentC] [nvarchar](50) NULL, [MonthRentPaidC] [nvarchar](50) NULL, CONSTRAINT [PK_ColorTable] PRIMARY KEY CLUSTERED ( [ContractNumberIDC] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO -----------------------------RentTableA --------------------------- USE [长铁物业公司资产数据库] GO /****** Object: Table [dbo].[RentTableA] Script Date: 10/22/2012 17:13:08 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[RentTableA]( [ContractNumberID] [int] IDENTITY(1,1) NOT NULL, [ContractNumber] [nvarchar](20) NULL, [LeaseDateFrom] [date] NULL, [Month] [date] NULL, [MonthRent] [int] NULL, [MonthRentPaid] [int] NULL, CONSTRAINT [PK_RentTableA] PRIMARY KEY CLUSTERED ( [ContractNumberID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已编辑 linjiangxian11 2012年12月18日 3:05
-
后台读颜色表输出json,给前台javascript变量。
可以给tr加上一个id,把表中对应的cid输出出来。然后判断textbox的ID是否包含对应的字段,给添加background 就好了。。。
用jQuery很容易实现,具体方法就不说了,很简单,但是很工作量不小
-
protected void GridView_Search_RowDataBound(object sender, GridViewRowEventArgs e) { DataClassesDataContext db = new DataClassesDataContext(); if (e.Row.RowType == DataControlRowType.DataRow) { string id = e.Row.Cells[0].Text; string colorMonthRent; string colorMonthRentPaid; ColorTable myColor = db.ColorTable.Where(c => (c.ContractNumberIDC) == (Convert.ToInt32(id))).FirstOrDefault(); if (myColor.MonthRentC != null) { colorMonthRent = myColor.MonthRentC; } else { //这里给默认值 colorMonthRent = "white"; } if (myColor.MonthRentPaidC != null) { colorMonthRentPaid = myColor.MonthRentPaidC; } else { colorMonthRentPaid = "white"; } e.Row.Cells[4].Attributes.Add("Style", "background-Color:" + colorMonthRent + ""); e.Row.Cells[5].Attributes.Add("style", "background-Color:" + colorMonthRentPaid + ""); } }
-
谢谢您的提示,以下是我继续尝试是遇到的问题,请帮忙看下!
问题1:按照您的提示我修改了代码并测试,我的ColorTable只写了两行的数据,于是就会出现如图1所示的错误,问题我写在图1的注释里了!(后面还有图3,是ColorTable的内容)
问题2:我把myColor = db.ColorTable.Where(c => c.ContractNumberIDC.ToString() == "1").FirstOrDefault();中改成了“1”,能运行,但是为什么Cell[4]中整个列都是红色了呢?我是要精确到某个单元格的背景色是红色(当ColorTable.ContractNumberIDC=RentTableA.ContractNumberIDC时,给相应的RentTableA.ContractNumberIDC的背景添加红色)。见图2
问题3:这个背景色的效果是个红色方框框,而我是要给GridView中的模板TextBox添加背景底色,该怎么修改呢?见图2-----------------------------代码1------------------------------
protected void GridView_Search_RowDataBound(object sender, GridViewRowEventArgs e) { DataClassesDataContext db = new DataClassesDataContext(); ColorTable myColor = new ColorTable(); if (e.Row.RowType == DataControlRowType.DataRow) { string id = e.Row.Cells[0].Text; string colorMonthRent; //应该还要加一个在ColorTable中是否有与RentTableA对应的数据的判断, //也就是ColorTable.ContractNumberIDC是否在RentTableA.ContractNumberIDC的数据中有相等的项,该怎么写呢? myColor = db.ColorTable.Where(c => c.ContractNumberIDC.ToString() == id).FirstOrDefault(); //myColor = db.ColorTable.Where(c => c.ContractNumberIDC.ToString() == "1").FirstOrDefault(); //Response.Write(myColor .MonthRentC .ToString ()); if (myColor.MonthRentC != null) { colorMonthRent = myColor.MonthRentC; e.Row.Cells[4].Attributes.Add("Style", "background-Color:" + colorMonthRent + ""); } else { colorMonthRent = "white"; } } }
---------------------------------------图1-------------------------------------------
----------------------------------------图2-----------------------------------
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
-
@问题1:按照您的提示我修改了代码并测试,我的ColorTable只写了两行的数据,于是就会出现如图1所示的错误,问题我写在图1的注释里了!(后面还有图3,是ColorTable的内容)
先判断你的myColor是否为空,然后继续。
@问题2:我把myColor = db.ColorTable.Where(c => c.ContractNumberIDC.ToString() == "1").FirstOrDefault();中改成了“1”,能运行,但是为什么Cell[4]中整个列都是红色了呢?
原因很简单——你写入了“RowDataBound”事件中,这样意味着每次重复绑定都会执行这个事件,而且你每行都满足条件,自然每一行第四列就设置成红色了,建议你看看逻辑是否出问题。
@这个背景色的效果是个红色方框框,而我是要给GridView中的模板TextBox添加背景底色,该怎么修改呢?
使用FindControl(e的Row方法)找出这个TextBox,设置其BackColor属性。
PS:linjiangxian11:你不必把每一个帖子都增加有用标记(左边绿色三角),只有对有帮助才点击。
- 已编辑 ThankfulHeart 2012年12月20日 2:08
- 已标记为答案 Song TianModerator 2012年12月20日 10:39