#
这两天在搞一个repeater中放一个checkbox, 非常想实现在服务器端自动的搜索他,并验证,用foreach +FindControl 实现,解决了一个问题,又出现了新问题,一但递交给服务器,repeater的数据重新绑定了,checkbox复原了,还要给他一个变量组,记录他的状态,用一个隐藏asp:label记录id,但他的值也是要复空的,用text="",强行附值,才可以得到,不大愿意用js获获得数据,再用window.location.href("asp.aspx?id="+id+"&page"=page)传递数据,再跳转回来,绑定到page页,找了很多checkbox方面的资料,最后还是没成功,当然用datagrid是很好实现的。实验后才有了自己的想法,真的需要跳转吗?真的需要服务器控件,要服务器响应,不停的刷新吗?用ajax不是把所有的问题都解决了吗?更新后刷新一下就什么也解决了啊,当时怎么就没想到啊,我做的程序不多,没什么经验,知道的东西也是少之又少,但我以为还好的方法。如果有人知道更好的方法,请指教啊,我当是抛砖引玉了。
关键词:
javascript
window.history.go(0)
window.location.reload()
window.location.href=location.href
window.location.assign(location.href)
document.execCommand('Refresh')
window.navigate(location.href)
window.navigate("#pos");
window.location.replace(location.href)
window.open('自身的文件','_self')
location.reload(flag)
flag =true 不从缓存读取
=false 从缓存读取
动态改变一个iframe的url.完了就刷新这个iframe.该如何做?
iframename.location.href=***
小弟刚开始学习,大学时间全浪费了(当然不能怨任何人,是我太不用功了,),转眼就毕业了,呵呵,都毕业的人了,计算机还没入门,希望各位高手,走过,路过,如果有什么建议,意见,或者我的blog上的错误,留下点余香,对我来说是一生受用,下世也不忘怀。如果有人能留下联系方式,比如QQ,Msn,E_Mail。有问题能直接找到你,教导我,小弟感激不尽,小弟在此谢过了,在此说明,本人blog几乎全部来自网络,极少数是自己写的,我习惯不好,没有表明出处,在此表示歉意。小弟QQ:407521952
在网络开发中,经常遇到需要使用ASP.NET与JavaScript联合进行控制的情况。在本篇中,将使用DataGrid进行数据绑定,使用Javascript控制当选中其中的checkbox时,该行颜色改变。
首先,在页面中创建一个DataGrid控件,并设置其模板。
以下是引用片段: <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:CheckBox id="checkbox1" Runat ="server"></asp:CheckBox> <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem") %>'></asp:Label> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> |
第二,在页面中的<head></head>中编写JavaScript脚本函数,进行CheckBox的判断和颜色改变的控制。
以下是引用片段: <script> function checkme(obj,tr){ if(obj.checked) tr.style.backgroundColor='blue'; else tr.style.backgroundColor=''; } </script> |
第三,在Page_Load事件中为DataGrid绑定数据,并关联CheckBox的JavaScript脚本。
以下是引用片段: private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!IsPostBack) { databind(); } } private void databind() { ArrayList arr=new ArrayList(); arr.Add("新闻综合"); arr.Add("综艺"); arr.Add("电影"); arr.Add("教育"); arr.Add("戏剧"); arr.Add("军事"); arr.Add("体育"); DataGrid1.DataSource=arr; DataGrid1.DataBind(); int i; for(i=0;i<DataGrid1.Items.Count;i++){ CheckBox cb; cb=(CheckBox)DataGrid1.Items[i].FindControl("checkbox1"); DataGrid1.Items[i].Attributes.Add("id","tr" + i.ToString()); cb.Attributes.Add("onclick","checkme(this,tr" + i.ToString() + ");"); } } |
原理就是用JS遍列所有CheckBox把所有CheckBox设为未被选中,然后在把单击的CheckBox设为选中
以下是.ASPX文件
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DataGridCheck.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm1</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="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> <SCRIPT language="JavaScript"> function SetCheckBoxState() { var dom=document.all; var el=event.srcElement; if(el.tagName=="INPUT"&&el.type.toLowerCase()=="checkbox") { for(i=0;i<dom.length;i++) { if(dom[i].tagName=="INPUT"&&dom[i].type.toLowerCase()=="checkbox") { dom[i].checked=false; } } } el.checked=!el.checked; } </SCRIPT> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <FONT face="宋体"> <asp:datagrid id="dg" style="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 40px" runat="server" Width="440px" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:CheckBox id="chkExport" runat="server"></asp:CheckBox> </ItemTemplate> </asp:TemplateColumn> <asp:BoundColumn DataField="IntegerValue"></asp:BoundColumn> <asp:BoundColumn DataField="StringValue"></asp:BoundColumn> <asp:BoundColumn DataField="CurrencyValue"></asp:BoundColumn> </Columns> </asp:datagrid><asp:button id="Button1" style="Z-INDEX: 102; LEFT: 168px; POSITION: absolute; TOP: 8px" runat="server" Text="显示内容"></asp:button></FONT></form> </body> </HTML> 以下是CS文件
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 DataGridCheck { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.DataGrid dg; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if (!Page.IsPostBack) { dg.DataSource= CreateDataSource() ; dg.DataBind(); } } ICollection CreateDataSource() { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32))); dt.Columns.Add(new DataColumn("StringValue", typeof(string))); dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double))); for (int i = 0; i < 9; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = "Item " + i.ToString(); dr[2] = 1.23 * (i + 1); dt.Rows.Add(dr); } DataView dv = new DataView(dt); return dv; }
#region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_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) { System.Web.UI.WebControls.CheckBox chkExport; foreach (DataGridItem dgItem in dg.Items) { chkExport=(CheckBox)dgItem.FindControl("chkExport"); if(chkExport.Checked) { Response.Write("<script>alert('"+dgItem.Cells[2].Text+"和"+dgItem.Cells[3].Text+"')</script>"); } } }
private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemIndex<0) return; if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { System.Web.UI.WebControls.CheckBox chkExport; chkExport=(CheckBox)e.Item.FindControl("chkExport"); chkExport.Attributes.Add("OnClick","SetCheckBoxState()"); e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='48d1cc'"); e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor"); } }
} }
|