/**/ /// <summary> /// 主要实现进度条的功能,这段代码的调用就要实现进度的调度 /// 实现主要过程 /// default.aspx.cs是调用页面 /// 放入page_load事件中 /// UIHelper myUI = new UIHelper(); /// Response.Write(myUI.ShowProgBar(this.Page,"../JS/progressbar.htm")); /// Thread thread = new Thread(new ThreadStart(ThreadProc)); /// thread.Start(); /// LoadData();//load数据 /// thread.Join(); /// Response.Write("OK"); /// /// 其中ThreadProc方法为 /// public void ThreadProc() /// { /// string strScript = "<script>setPgb('pgbMain','{0}');</script>"; /// for (int i = 0; i <= 100; i++) /// { /// System.Threading.Thread.Sleep(10); /// Response.Write(string.Format(strScript, i)); /// Response.Flush(); /// } /// } /// 其中LoadData() /// public void LoadData() /// { /// for (int m = 0; m < 900; m++) /// { /// for (int i = 0; i < 900; i++) /// { /// /// } /// } /// } /// /// </summary> /// <param name="pageCurrent"></param> /// <param name="ShowProgbarScript"></param> /// <returns></returns> public static string ShowProgBar(System.Web.UI.Page pageCurrent, string ShowProgbarScript) { StreamReader sr = new StreamReader(pageCurrent.MapPath(ShowProgbarScript), System.Text.Encoding.Default); StringBuilder sb = new StringBuilder(); string line; try { while ((line = sr.ReadLine()) != null) { sb.AppendLine(line); } sr.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } //pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(), // System.Guid.NewGuid().ToString(), sb.ToString()); return(sb.ToString()); } #endregion #region fixedHeader public static string fixedHeader() { StringBuilder s = new StringBuilder(); s.Append(@"<table width='100%' border='1' cellspacing='0' style='MARGIN-TOP:-2px'>"); s.Append(@"<TR class='fixedHeaderTr' style='BACKGROUND:navy;COLOR:white'>"); s.Append(@"<TD nowrap>Header A</TD>"); s.Append(@"<TD nowrap>Header B</TD>"); s.Append(@"<TD nowrap>Header C</TD>"); s.Append(@"</TR>"); for (int m = 0; m < 100; m++) { s.Append(@"<TR>"); s.Append(@"<TD>A").Append(m).Append("</TD>"); s.Append(@"<TD>B").Append(m).Append("</TD>"); s.Append(@"<TD>C").Append(m).Append("</TD>"); s.Append(@"</TR>"); } s.Append(@"</table>"); return(s.ToString()); } #endregion #region refreshPage public static void refreshPage(System.Web.UI.Page pageCurrent) { StringBuilder sb = new StringBuilder(); sb.Append("<script language=\"javascript\">"); sb.Append("window.location.reload(true);"); sb.Append("</script>"); pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(), System.Guid.NewGuid().ToString(), sb.ToString()); } #endregion #region Page_revealTrans //进入页面<meta http-equiv="Page-Enter" content="revealTrans(duration=x, transition=y)"> //推出页面<meta http-equiv="Page-Exit" content="revealTrans(duration=x, transition=y)"> //这个是页面被载入和调出时的一些特效。duration表示特效的持续时间,以秒为单位。transition表示使用哪种特效,取值为1-23: // 0 矩形缩小 // 1 矩形扩大 // 2 圆形缩小 // 3 圆形扩大 // 4 下到上刷新 // 5 上到下刷新 // 6 左到右刷新 // 7 右到左刷新 // 8 竖百叶窗 // 9 横百叶窗 // 10 错位横百叶窗 // 11 错位竖百叶窗 // 12 点扩散 // 13 左右到中间刷新 // 14 中间到左右刷新 // 15 中间到上下 // 16 上下到中间 // 17 右下到左上 // 18 右上到左下 // 19 左上到右下 // 20 左下到右上 // 21 横条 // 22 竖条 // 23 以上22种随机选择一种 public static string Page_revealTrans(System.Web.UI.Page currentPage, string duration, string transition) { StringBuilder sb = new StringBuilder(); sb.Append("<meta http-equiv=\"Page-Enter\""); sb.Append("content=\""); sb.Append("revealTrans(duration=" + duration); sb.Append(",transition=" + transition); sb.Append(")\">"); //currentPage.ClientScript.RegisterStartupScript(currentPage.GetType(), // System.Guid.NewGuid().ToString(), sb.ToString()); return(sb.ToString()); } #endregion /// <summary> /// 显示一段自定义的输出代码 /// </summary> /// <param name="page">页面指针,一般为This</param> public static void RegisterStartupScript(System.Web.UI.Page page, string script) { var sb = new StringBuilder(); sb.Append("<script type=\"text/javascript\"> "); sb.Append(script.Trim()); sb.Append("</script>"); page.ClientScript.RegisterStartupScript(page.GetType(), page.GetType().Name, sb.ToString()); } public static void RegisterClientScriptBlock(System.Web.UI.Page page, string script) { var sb = new StringBuilder(); sb.Append("<script type=\"text/javascript\"> "); sb.Append(script.Trim()); sb.Append("</script>"); page.ClientScript.RegisterClientScriptBlock(page.GetType(), page.GetType().Name, sb.ToString()); } /**/ /// <summary> /// 调用客户端JavaScript函数 /// </summary> /// <param name="page">页面指针,一般为This</param> /// <param name="scriptName">函数名,带参数,如:FunA(1);</param> public static void CallClientScript(System.Web.UI.Page page, string scriptName) { String csname = "PopupScript"; Type cstype = page.GetType(); System.Web.UI.ClientScriptManager cs = page.ClientScript; if (!cs.IsStartupScriptRegistered(cstype, csname)) { String cstext = scriptName; cs.RegisterStartupScript(cstype, csname, cstext, true); } }
private static void IncludeJavaScript(ClientScriptManager manager, string resourceName, bool late) { var type = typeof(JavaScriptLibrary.JavaScriptHelper); if (!manager.IsStartupScriptRegistered(type, resourceName)) { if (late) { var url = manager.GetWebResourceUrl(type, resourceName); var scriptBlock = string.Format(TEMPLATE_SCRIPT, HttpUtility.HtmlEncode(url)); manager.RegisterStartupScript(type, resourceName, scriptBlock); } else { manager.RegisterClientScriptResource(type, resourceName); manager.RegisterStartupScript(type, resourceName, string.Empty); } } //manager.RegisterClientScriptResource(type, resourceName); }
/// <summary> /// 将此控件呈现给指定的输出参数。 /// </summary> /// <param name="output"> 要写出到的 HTML 编写器 </param> protected override void Render(HtmlTextWriter output) { if (this.BackColorFocus != "") { base.Attributes.Add("onfocus", "this.style.background='" + this.BackColorFocus + "'"); //if(!HasDropDownList) base.Attributes.Add("onblur", "this.style.background='#ffffff'"); //else // base.Attributes.Add("onblur", "this.style.background='#ffffff';if(!document.getElementById('" + this.ClientID + "List').focused){document.getElementById('" + this.ClientID + "List').style.display='none';}"); } //if (this.CssFocus != "") //{ // base.Attributes.Add("onmousedown", "this.className='" + this.CssFocus + "'"); // base.Attributes.Add("onmouseout", "this.className='" + this.CssClass + "'"); //} if (this.HasDropDownList) { base.Attributes.Add("title", "点击显示下拉框"); base.Attributes.Add("onclick", "ShowList('" + this.ClientID + "');"); } //输出 base.Render(output); output.Write(" "); if (InputExplain.Trim() != "") { this.lblExplain.ID = "lbl" + base.ID; this.lblExplain.RenderControl(output); } if (!this.AllowEmpty) { this.rfvDataInput.ID = "rfv" + base.ID; this.rfvDataInput.ControlToValidate = base.ID; this.rfvDataInput.RenderControl(output); } if (this.ValidType != DataType.Never && this.ValidType != DataType.String) { this.revDataInput.ID = "rev" + base.ID; this.revDataInput.ControlToValidate = base.ID; this.revDataInput.ValidationExpression = this.GetValidRegex(); this.revDataInput.ErrorMessage = error; this.revDataInput.RenderControl(output); } //可调整高度 if (this.IsRegulateHeight) { #region StringBuilder sbscript = new StringBuilder(); sbscript.Append("\n<!--调节输入框高度 begin-->\n"); sbscript.Append(" <script type=\"text/javascript\">\n"); sbscript.Append(" //Plus\n"); sbscript.Append(" function PlusHeight(id)\n"); sbscript.Append(" {\n"); sbscript.Append(" var txtInput = document.getElementById(id);\n"); sbscript.Append(" txtInput.rows = parseInt(txtInput.rows) + "+ this.RegulateRows + ";\n"); //sbscript.Append(" txtInput.style.height = txtInput.style.height+15;\n"); sbscript.Append(" }\n"); sbscript.Append(" //Minus\n"); sbscript.Append(" function MinusHeight(id)\n"); sbscript.Append(" {\n"); sbscript.Append(" var txtInput = document.getElementById(id);\n"); sbscript.Append(" if( parseInt(txtInput.rows) >= "+ (this.RegulateRows + 1) + " )\n"); sbscript.Append(" txtInput.rows = parseInt(txtInput.rows) - "+ this.RegulateRows + ";\n"); sbscript.Append(" }\n"); sbscript.Append(" </script>\n"); sbscript.Append("<!--调节输入框高度 end-->\n"); System.Web.UI.ClientScriptManager client = this.Page.ClientScript; Type cstype = Page.GetType(); if (!client.IsStartupScriptRegistered(cstype, "regulateheight")) { client.RegisterStartupScript(cstype, "regulateheight", sbscript.ToString()); } StringBuilder sbhtml = new StringBuilder(); sbhtml.Append("<div style=\"height:24px;\">"); sbhtml.Append(" " + this.ImageIconMinus + " " + this.ImageIconPlus); sbhtml.Append("</div>"); output.Write(sbhtml.ToString()); #endregion } //下拉框模式 if (this.HasDropDownList) { #region //已经改为点击输入框的时候激发客户端事件 //output.Write("<img id=\"" + this.ClientID + "Icon\" src=\"" + this.ImageIconShowDropDown + "\" border=\"0\" style=\"cursor:hand;\" onclick=\"ShowList('" + this.ClientID + "');\" align=\"absmiddle\" />\n"); int width = Convert.ToInt32(base.Width.Value); width = width == 0 ? 100 : width; output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "display:none;position:absolute;"); output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Id, this.ClientID + "List"); output.RenderBeginTag(HtmlTextWriterTag.Div); output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "margin-left:" + width + "px;"); output.RenderBeginTag(HtmlTextWriterTag.Span); drpList.ID = base.ID + "Items"; drpList.Style.Add("margin-left", "-" + width + "px"); drpList.Attributes.Add("onchange", "document.getElementById('" + this.ClientID + "').value=this.options[this.selectedIndex].text;document.getElementById('" + this.ClientID + "List').style.display='none';"); //drpList.Attributes.Add("onblur", "this.style.display='none'"); if (this.DropDownListDataSource != null && this.DropDownListDataSource.Rows.Count > 0) { drpList.DataTextField = this.DataTextField; drpList.DataValueField = this.DataValueField; drpList.DataSource = this.DropDownListDataSource.DefaultView; drpList.DataBind(); } if (HasDefaultItem) { drpList.Items.Insert(0, new ListItem(this.DefaultItemText, this.DefaultItemValue)); } foreach (ListItem item in drpList.Items) { if (item.Text == base.Text.Trim()) { item.Selected = true; break; } } this.drpList.RenderControl(output); output.RenderEndTag(); output.RenderEndTag(); StringBuilder sbscript = new StringBuilder(); #region js内容 sbscript.Append("\n<script type=\"text/javascript\">\n"); sbscript.Append(" <!--下拉框显示隐藏 begin-->\n"); sbscript.Append(" function ShowList(id)\n"); sbscript.Append(" {\n"); sbscript.Append(" var txtInput = document.getElementById(id);\n"); sbscript.Append(" var drpList = document.getElementById(id+'List');\n"); //sbscript.Append(" var drpIcon = document.getElementById(id+'Icon');\n"); sbscript.Append(" var drpItems = document.getElementById(id+'Items');\n"); //下拉框 sbscript.Append(" if(drpList.style.display == 'block')\n"); sbscript.Append(" {\n"); sbscript.Append(" txtInput.title = '点击显示下拉框';\n"); sbscript.Append(" drpList.style.display = 'none';\n"); //sbscript.Append(" drpIcon.src = '" + this.ImageIconShowDropDown + "';\n"); sbscript.Append(" }\n"); sbscript.Append(" else\n"); sbscript.Append(" {\n"); sbscript.Append(" txtInput.title = '点击关闭下拉框';\n"); sbscript.Append(" drpList.style.display = 'block';\n"); sbscript.Append(" drpList.style.left = GetOffsetLeft(txtInput)+'px';\n"); sbscript.Append(" drpList.style.top = (GetOffsetTop(txtInput)+1)+'px';\n"); sbscript.Append(" drpItems.style.width = txtInput.offsetWidth+'px';\n"); //sbscript.Append(" drpIcon.src = '" + this.ImageIconHideDropDown + "';\n"); sbscript.Append(" }\n"); sbscript.Append(" }\n"); sbscript.Append(" <!--下拉框显示隐藏 end-->\n"); sbscript.Append(" function GetOffsetTop(e) \n"); sbscript.Append(" {\n"); sbscript.Append(" var offsetTop = e.offsetTop;\n"); sbscript.Append(" var offsetParent = e.offsetParent;\n"); sbscript.Append(" while(offsetParent)\n"); sbscript.Append(" {\n"); sbscript.Append(" offsetTop += offsetParent.offsetTop;\n"); sbscript.Append(" offsetParent = offsetParent.offsetParent;\n"); sbscript.Append(" }\n"); sbscript.Append(" return offsetTop+e.offsetHeight;\n"); sbscript.Append(" }\n"); sbscript.Append(" function GetOffsetLeft(e) \n"); sbscript.Append(" {\n"); sbscript.Append(" var offsetLeft = e.offsetLeft;\n"); sbscript.Append(" var offsetParent = e.offsetParent;\n"); sbscript.Append(" while(offsetParent) \n"); sbscript.Append(" {\n"); sbscript.Append(" offsetLeft += offsetParent.offsetLeft;\n"); sbscript.Append(" offsetParent = offsetParent.offsetParent;\n"); sbscript.Append(" }\n"); sbscript.Append(" return offsetLeft;\n"); sbscript.Append(" }\n"); sbscript.Append("</script>\n"); # endregion System.Web.UI.ClientScriptManager client = this.Page.ClientScript; Type cstype = Page.GetType(); if (!client.IsStartupScriptRegistered(cstype, "showlist")) { client.RegisterStartupScript(cstype, "showlist", sbscript.ToString()); } #endregion }