62,046
社区成员
发帖
与我相关
我的任务
分享
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<div>
<asp:Button ID="Button1" runat="server" BackColor="red" Width="40"
BorderStyle="None" OnClick="UserOnClick" style="height: 17px" />
</div>
<script>
var timename;
function text() {
timename = setInterval("change()", 1000);
}
function change() {
var btnId = "<%=GetBtnId()%>";
var btnColor = document.getElementById(btnId).style.backgroundColor;
if (btnColor == 'blue') {
document.getElementById(btnId).style.backgroundColor = 'red';
}
else {
document.getElementById(btnId).style.backgroundColor = 'blue';
}
}
function textClose() {
clearInterval(timename);
}
</script>
protected void Page_Load(object sender, EventArgs e)
{
}
private Color _flickerColor1;
public Color FlickerColor1
{
get { return _flickerColor1; }
set { _flickerColor1 = value; }
}
private Color _flickerColor2;
public Color FlickerColor2
{
get { return _flickerColor2; }
set { _flickerColor2 = value; }
}
public Color BackGroud
{
get { return this.Button1.BackColor; }
set { this.Button1.BackColor = value; }
}
private bool isBroken;
public bool IsBroken
{
get { return isBroken; }
set {
isBroken = value;
if (isBroken)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>text()</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>textClose()</script>");
}
}
}
/// <summary>
/// 返回控件
/// </summary>
/// <returns></returns>
public string GetBtnId()
{
return this.Button1.ClientID.ToString();
}
void timer_Tick(object sender, EventArgs e)
{
if (isBroken)
{
if (Button1.BackColor == Color.Red)
{
Button1.BackColor = Color.Black;
}
else
{
Button1.BackColor = Color.Red;
}
}
}
public event EventHandler Login; //添加事件句柄
protected void UserOnClick(object sender, EventArgs e)
{
if (Login != null)
Login(this, new EventArgs()); //激活Login事件
}
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<!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">
<div style="margin:4,4,4,4;">
<asp:Button ID="Button1" runat="server" Text="是否停止闪烁" onclick="Button1_Click" />
<uc1:WebUserControl1 ID="WebUserControl13" runat="server" BackGroud="Black" />
<uc1:WebUserControl1 ID="WebUserControl12" runat="server" BackGroud="Black" />
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" BackGroud="Black" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
}
protected void OnLoginPress(Object Render, EventArgs e)
{
this.RegisterClientScriptBlock("", "<script>alert(1);</script>");
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!WebUserControl11.IsBroken)
{
WebUserControl11.IsBroken = true;
}
else
{
WebUserControl11.IsBroken = true;
}
if (!WebUserControl12.IsBroken)
{
WebUserControl12.IsBroken = true;
}
else
{
WebUserControl12.IsBroken = true;
}
}
window.setInterval(function()
{
intervalRun(param);
}, 888);
你可以把intervalRun(param) 替换成具体的js代码 public bool IsBroken
{
get { return isBroken; }
set {
isBroken = value;
if (isBroken)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('"+this.ClientID.ToString()+"');Mycontrl.prototype.Refresh('" + this.ClientID.ToString() + "','" + this.FlickerColor1.ToString() + "','" + this.FlickerColor2.ToString() + "')</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>textClose()</script>");
}
}
}
当有两个属性调用的时候,确实跑了两次这段代码,但是页面最终看源的HTML,发现只有第一次的js写到界面了。后面的未写。
<script>
var Mycontrl = function () {
this.clientID = null;
this.btn = null;
var ArrayList = null;
this.beginChange = function (clientId, color1, color2) {
clientID = clientId;
Mycontrl.prototype.ArrayList[clientId] = this;
//var func = new Function("Mycontrl.prototype.ArrayList[" + clientId + "].ChangeColor('" + clientId + "','" + color1 + "','" + color2 + "')");
var func = new Function("Mycontrl.prototype.ArrayList['" + clientId + "'].ChangeColor()");
alert(clientId);
setInterval(func, 1000);
}
this.ChangeColor = function () {
var btnColor = document.getElementById(clientID + "_Button1").style.backgroundColor;
if (btnColor == "red") {
document.getElementById(clientID + "_Button1").style.backgroundColor = 'blue';
}
else {
document.getElementById(clientID + "_Button1").style.backgroundColor = 'red';
}
}
// this.ChangeColor = function (clientid, color1, color2) {
// var btnColor = document.getElementById(clientid).style.backgroundColor;
// if (btnColor == color1) {
// document.getElementById(clientid).style.backgroundColor = color2;
// }
// else {
// document.getElementById(clientid).style.backgroundColor = color1;
// }
// }
}
Mycontrl.prototype.ArrayList = null;
Mycontrl.prototype.Refresh = function (clientid, color1, color2) {
if (!Mycontrl.prototype.ArrayList) {
Mycontrl.prototype.ArrayList = new Array();
}
var obj = Mycontrl.prototype.ArrayList[clientid];
if (typeof (obj) == "undefined" || obj == null) {
obj = new Mycontrl();
}
obj.beginChange(clientid, color1, color2);
}
</script>
你说的匿名方法,我也想过,这是我把js改过之后的代码,结果调用发现一个问题,初衷是想通过clientID来调用不一样的方法,但是单步跟踪的时候发现代码确实应该是调用两次,但是最终呈现的html只调用了一次js的方法,另一个调用未写到界面。我的调用是在用户控件的属性器的Set属性里调用的。