关于c#中的replace变量又出现了问题

lee13688 2009-04-10 11:09:50
在下面的这段代码中,变量赋值后,传不到replace中去,我晕倒。

<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" DataSourceID="nanchangyinhang" ShowFooter="False"
ShowHeader="False" CellPadding="0">
<ItemTemplate>
<% string youkey = "asdf";%>
<a href="Gg_show.aspx?id=<%# Eval("id") %>" target="_blank"><asp:Label ID="titleLabel" runat="server" Text='<%# Eval("title") %>'></asp:Label></a><br />
<asp:Label ID="Label1" runat="server" Text='<% Eval("title").ToString().Replace(youkey,"<font color=red>"+youkey+"</font>")%>'> </asp:Label><br />
</ItemTemplate>
</asp:DataList><asp:SqlDataSource ID="nanchangyinhang" runat="server" ConnectionString="<%$ ConnectionStrings:NanChangYinHangConnectionString %>"
SelectCommand="SELECT top 5 [title],[id] FROM [config] ORDER BY [id] DESC"></asp:SqlDataSource>

</div>
</form>


我用了2个label来分别显示,测试后,不报错,有个提示"警告 4 变量“youkey”已赋值,但其值从未使用过"

而且,Label1不显示出来了。
...全文
207 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lee13688 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Sandy945 的回复:]
HTML code<asp:LabelID="Label1"runat="server"Text='<%Eval("title").ToString().Replace(youkey,"<font color=red>"+youkey+"</font>")%>'></asp:Label><br/>

HTML code<asp:LabelID="Label1"runat="server"Text='<%#Eval("title").ToString().Replace(youkey,"<font color=red>"+youkey+"</font>")%>'></asp:Label><br/>

<%# Eval("title").ToString().Replace(youkey," <font color=red>"+youkey+" </font>")%>
[/Quote]

加了#后,直接报错,上下文中不存在youkey
阿非 2009-04-10
  • 打赏
  • 举报
回复
<asp:Label ID="Label1" runat="server" Text='<% Eval("title").ToString().Replace(youkey,"<font color=red>"+youkey+"</font>")%>'> </asp:Label><br />


<asp:Label ID="Label1" runat="server" Text='<%# Eval("title").ToString().Replace(youkey,"<font color=red>"+youkey+"</font>")%>'> </asp:Label><br />


<%# Eval("title").ToString().Replace(youkey,"<font color=red>"+youkey+"</font>")%>
lee13688 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ViewStates 的回复:]
把你定义YOUKEY的那句话放到DATALIST外边
[/Quote]

放外部了,还是没有用;

也试了放cs中,也没有用。。。。。。。。。。。
ViewStates 2009-04-10
  • 打赏
  • 举报
回复
把你定义YOUKEY的那句话放到DATALIST外边
lee13688 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 Sandy945 的回复:]
HTML code<%@ Page Language="C#"AutoEventWireup="true"CodeFile="replace.aspx.cs"Inherits="temp_replace"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>无标题页</title></head><body><formid="form1"runat="server"><asp:TextBoxID="txt"runat="server"></asp:Tex…
[/Quote]

非常感谢!
阿非 2009-04-10
  • 打赏
  • 举报
回复

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="replace.aspx.cs" Inherits="temp_replace" %>

<!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>
</head>
<body>
<form id="form1" runat="server">

<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:Button ID="btn" Text="设置关键字" runat="server" OnClick="btn_Click" /><br />
<asp:Repeater ID="rp" runat="server">
<AlternatingItemTemplate>
<%#getStr(Eval("Name")!=null?Eval("Name").ToString():"") %><br />
</AlternatingItemTemplate>
<ItemTemplate>
<%#Eval("Name") != null?Eval("Name").ToString().Replace(value,"<font color='red'>" + value + "</font>"):""%><br />
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>




using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class temp_replace : System.Web.UI.Page
{
protected string value;

protected string getStr(string oldStr)
{
return oldStr.Replace(value, "<font color='red'>" + value + "</font>");
}

private DataTable getDateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Name", typeof(String)));
DataRow dr;
for (int i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = "人员" + i.ToString();
dt.Rows.Add(dr);
}
return dt;
}


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
value = "1";
rp.DataSource = getDateTable();
rp.DataBind();
}
}
protected void btn_Click(object sender, EventArgs e)
{
value = txt.Text.Trim();
rp.DataSource = getDateTable();
rp.DataBind();
}
}

阿非 2009-04-10
  • 打赏
  • 举报
回复
稍等一下,机器有点卡 ~
lee13688 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 oth900 的回复:]

你可以试着把 <a>标签的内容改成
<a onclick="windows.open('Gg_show.aspx?id= <%# Eval("id") %>')" href="#"> <%# Eval("title") %> </a>

经实验这样是可以的

[/Quote]
我改这个干什么?
oth900 2009-04-10
  • 打赏
  • 举报
回复

你可以试着把<a>标签的内容改成
<a onclick="windows.open('Gg_show.aspx?id=<%# Eval("id") %>')" href="#"> <%# Eval("title") %> </a>

经实验这样是可以的
lee13688 2009-04-10
  • 打赏
  • 举报
回复
其实我想实现查询结果中的关键字用红色标出来,目前就只想到了用replace来操作。

楼上大哥,有没有查询结果关键字变色的代码?有的话发一个给小弟,谢谢!
lee13688 2009-04-10
  • 打赏
  • 举报
回复
在cs中,private youkey1(string youkey)
{
string youkey="asdf";
}
阿非 2009-04-10
  • 打赏
  • 举报
回复
? 你现在是怎么写的
lee13688 2009-04-10
  • 打赏
  • 举报
回复
还是不行啊,郁闷。。。。。。。。。。
阿非 2009-04-10
  • 打赏
  • 举报
回复
把<% string youkey = "asdf";%>去掉


youkey 在cs页里定义,访问级别至少是protected

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧