早听说magicajax是如何如何的简单,这几天总算见识到了,和以前写程序没多大区别,只要将控件包含在ajaxpanel里就OK了
源码下载: Files\huazi4995\magicajax-0.2.2-source.rar
在公司写了一个关于这个框架的应用页面,也将其源吗码编译成了支持中文的,好爽 .如下图
<%
@ Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax"
%>
<% @ Page language="c#" Codebehind="ZiDian.aspx.cs" AutoEventWireup="false" Inherits="LSA.ActiveForm.ZiDian" ResponseEncoding="utf-8" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > 代码类别选择 </ 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" >
< LINK href ="../Style/WinXP/style.css" type ="text/css" rel ="stylesheet" >
< base target ="_self" >
< style type ="text/css" > .btn_2k3 {}{ BORDER-RIGHT: #002d96 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #002d96 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 12px; FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#9DBCEA); BORDER-LEFT: #002d96 1px solid; CURSOR: hand; COLOR: black; PADDING-TOP: 2px; BORDER-BOTTOM: #002d96 1px solid }
.btn {}{ BORDER-RIGHT: #7b9ebd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #7b9ebd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 12px; FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#cecfde); BORDER-LEFT: #7b9ebd 1px solid; CURSOR: hand; COLOR: black; PADDING-TOP: 2px; BORDER-BOTTOM: #7b9ebd 1px solid }
</ style >
< script language ="javascript" >
function getvalue()
{
var a=document.getElementById("txtDm").value;
alert(a);
}
function returnData(dm,mc)
{
var strReturn="";
strReturn=strReturn + dm + "," + mc;
window.returnValue = strReturn;
window.close();
//var objList=document.getElementById("lst");
//objList.options[0].value = strValue;
//objList.options[0].text = strText;
}
function btncanel()
{
window.close();
}
//删除
function deleteOption()
{
var addOption=document.createElement("option");
var index1;
//alert(addOption);
if(document.Form1.lst.length==0)
return(false);
index1=document.Form1.lst.selectedIndex;
if(index1<0)
{
alert("请先选择!!!");
return(false);
}
addOption.text=document.Form1.lst.options(index1).text;
addOption.value=document.Form1.lst.value;
//document.Form1.lst_TeZheng.add(addOption);
document.Form1.lst.remove (index1);
}
function SetValue()
{
//var objList = document.getElementById("lst");
//if(!(objList.length <%=num%>))
//{
// alert("输入的待选项验证失败,应 <%=num%> 。");
// return;
//}
var strReturn="";
var objList = document.getElementById("lst");
for(var i=0;i<objList.length;i++)
{
//返回给文本框
//strReturn = strReturn + "," + objList.options[i].text;
//返回给多选框
strReturn = strReturn + "|" + objList.options[i].value + "," + objList.options[i].text;
}
if(strReturn !="")
strReturn = strReturn.substring(1,strReturn.length);
window.returnValue = strReturn;
window.close();
}
function KeyDown(){
//alert(event.keyCode);
if (event.keyCode==191){
//alert('123123');
event.keyCode=0; event.returnValue=false;
}
}
</ script >
</ HEAD >
< body MS_POSITIONING ="FlowLayout" >
< form id ="Form1" method ="post" runat ="server" >
< FONT face ="宋体" ></ FONT >
< br >
< br >
< center >
< ajax:ajaxpanel id ="AjaxPanel1" runat ="server" >
< TABLE id ="Table1" cellSpacing ="4" cellPadding ="4" width ="340" border ="1" >
< TR >
< TD >< FONT face ="宋体" > 关键字: </ FONT >
< asp:TextBox id ="txtDm" runat ="server" ToolTip ="输入代码或拼音首字母索引" Width ="70px" ></ asp:TextBox >
< asp:LinkButton id ="lbShowResult" runat ="server" > 搜 索 </ asp:LinkButton ></ TD >
< TD >
< asp:Button id ="btnPre" runat ="server" CssClass ="btn" Text ="前一页" ></ asp:Button >
< asp:Button id ="btnNext" runat ="server" CssClass ="btn" Text ="下一页" ></ asp:Button ></ TD >
</ TR >
< TR >
< TD colSpan ="2" >
< asp:DataGrid id ="dg" runat ="server" ToolTip ="双击可以选择" Width ="100%" AllowPaging ="True" AutoGenerateColumns ="False"
ShowHeader ="False" DataKeyField ="dm" >
< Columns >
< asp:TemplateColumn >
< HeaderStyle Width ="10px" ></ HeaderStyle >
< HeaderTemplate >
< INPUT id ="checkAll" type ="checkbox" CommandName ="Select2" >
</ HeaderTemplate >
< ItemTemplate >
< asp:CheckBox id ="chk" runat ="server" AutoPostBack ="True" ></ asp:CheckBox >
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn >
< HeaderStyle Width ="10px" ></ HeaderStyle >
< ItemTemplate >
< asp:RadioButton id ="rad" runat ="server" AutoPostBack ="True" ></ asp:RadioButton >
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:ButtonColumn Visible ="False" Text ="选择" CommandName ="Select" >
< HeaderStyle Width ="10px" ></ HeaderStyle >
</ asp:ButtonColumn >
< asp:BoundColumn DataField ="dm" >
< HeaderStyle Width ="20px" ></ HeaderStyle >
</ asp:BoundColumn >
< asp:BoundColumn DataField ="name" >
< HeaderStyle Width ="200px" ></ HeaderStyle >
</ asp:BoundColumn >
</ Columns >
< PagerStyle Visible ="False" Mode ="NumericPages" ></ PagerStyle >
</ asp:DataGrid ></ TD >
</ TR >
< TR >
< TD id ="tddrp" style ="HEIGHT: 67px" align ="center" colSpan ="2" runat ="server" >
< asp:ListBox id ="lst" runat ="server" ></ asp:ListBox >< INPUT id ="btndelete" style ="DISPLAY: none" onclick ="javascript:deleteOption();" type ="button"
value ="删 除" >
< asp:Button id ="btndel" runat ="server" CssClass ="btn" Text ="删 除" ></ asp:Button ></ TD >
</ TR >
< TR >
< TD id ="tdbtn" align ="center" colSpan ="2" runat ="server" >< INPUT class ="btn" onclick ="SetValue();" type ="button" value ="确 定" >
< INPUT class ="btn" id ="btncanel" onclick ="javascript:window.close();" type ="button" value ="取 消" >
</ TD >
</ TR >
</ TABLE >
</ ajax:ajaxpanel >
</ center >
</ form >
</ body >
</ HTML >
<% @ Page language="c#" Codebehind="ZiDian.aspx.cs" AutoEventWireup="false" Inherits="LSA.ActiveForm.ZiDian" ResponseEncoding="utf-8" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > 代码类别选择 </ 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" >
< LINK href ="../Style/WinXP/style.css" type ="text/css" rel ="stylesheet" >
< base target ="_self" >
< style type ="text/css" > .btn_2k3 {}{ BORDER-RIGHT: #002d96 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #002d96 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 12px; FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#9DBCEA); BORDER-LEFT: #002d96 1px solid; CURSOR: hand; COLOR: black; PADDING-TOP: 2px; BORDER-BOTTOM: #002d96 1px solid }
.btn {}{ BORDER-RIGHT: #7b9ebd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #7b9ebd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 12px; FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#cecfde); BORDER-LEFT: #7b9ebd 1px solid; CURSOR: hand; COLOR: black; PADDING-TOP: 2px; BORDER-BOTTOM: #7b9ebd 1px solid }
</ style >
< script language ="javascript" >
function getvalue()
{
var a=document.getElementById("txtDm").value;
alert(a);
}
function returnData(dm,mc)
{
var strReturn="";
strReturn=strReturn + dm + "," + mc;
window.returnValue = strReturn;
window.close();
//var objList=document.getElementById("lst");
//objList.options[0].value = strValue;
//objList.options[0].text = strText;
}
function btncanel()
{
window.close();
}
//删除
function deleteOption()
{
var addOption=document.createElement("option");
var index1;
//alert(addOption);
if(document.Form1.lst.length==0)
return(false);
index1=document.Form1.lst.selectedIndex;
if(index1<0)
{
alert("请先选择!!!");
return(false);
}
addOption.text=document.Form1.lst.options(index1).text;
addOption.value=document.Form1.lst.value;
//document.Form1.lst_TeZheng.add(addOption);
document.Form1.lst.remove (index1);
}
function SetValue()
{
//var objList = document.getElementById("lst");
//if(!(objList.length <%=num%>))
//{
// alert("输入的待选项验证失败,应 <%=num%> 。");
// return;
//}
var strReturn="";
var objList = document.getElementById("lst");
for(var i=0;i<objList.length;i++)
{
//返回给文本框
//strReturn = strReturn + "," + objList.options[i].text;
//返回给多选框
strReturn = strReturn + "|" + objList.options[i].value + "," + objList.options[i].text;
}
if(strReturn !="")
strReturn = strReturn.substring(1,strReturn.length);
window.returnValue = strReturn;
window.close();
}
function KeyDown(){
//alert(event.keyCode);
if (event.keyCode==191){
//alert('123123');
event.keyCode=0; event.returnValue=false;
}
}
</ script >
</ HEAD >
< body MS_POSITIONING ="FlowLayout" >
< form id ="Form1" method ="post" runat ="server" >
< FONT face ="宋体" ></ FONT >
< br >
< br >
< center >
< ajax:ajaxpanel id ="AjaxPanel1" runat ="server" >
< TABLE id ="Table1" cellSpacing ="4" cellPadding ="4" width ="340" border ="1" >
< TR >
< TD >< FONT face ="宋体" > 关键字: </ FONT >
< asp:TextBox id ="txtDm" runat ="server" ToolTip ="输入代码或拼音首字母索引" Width ="70px" ></ asp:TextBox >
< asp:LinkButton id ="lbShowResult" runat ="server" > 搜 索 </ asp:LinkButton ></ TD >
< TD >
< asp:Button id ="btnPre" runat ="server" CssClass ="btn" Text ="前一页" ></ asp:Button >
< asp:Button id ="btnNext" runat ="server" CssClass ="btn" Text ="下一页" ></ asp:Button ></ TD >
</ TR >
< TR >
< TD colSpan ="2" >
< asp:DataGrid id ="dg" runat ="server" ToolTip ="双击可以选择" Width ="100%" AllowPaging ="True" AutoGenerateColumns ="False"
ShowHeader ="False" DataKeyField ="dm" >
< Columns >
< asp:TemplateColumn >
< HeaderStyle Width ="10px" ></ HeaderStyle >
< HeaderTemplate >
< INPUT id ="checkAll" type ="checkbox" CommandName ="Select2" >
</ HeaderTemplate >
< ItemTemplate >
< asp:CheckBox id ="chk" runat ="server" AutoPostBack ="True" ></ asp:CheckBox >
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn >
< HeaderStyle Width ="10px" ></ HeaderStyle >
< ItemTemplate >
< asp:RadioButton id ="rad" runat ="server" AutoPostBack ="True" ></ asp:RadioButton >
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:ButtonColumn Visible ="False" Text ="选择" CommandName ="Select" >
< HeaderStyle Width ="10px" ></ HeaderStyle >
</ asp:ButtonColumn >
< asp:BoundColumn DataField ="dm" >
< HeaderStyle Width ="20px" ></ HeaderStyle >
</ asp:BoundColumn >
< asp:BoundColumn DataField ="name" >
< HeaderStyle Width ="200px" ></ HeaderStyle >
</ asp:BoundColumn >
</ Columns >
< PagerStyle Visible ="False" Mode ="NumericPages" ></ PagerStyle >
</ asp:DataGrid ></ TD >
</ TR >
< TR >
< TD id ="tddrp" style ="HEIGHT: 67px" align ="center" colSpan ="2" runat ="server" >
< asp:ListBox id ="lst" runat ="server" ></ asp:ListBox >< INPUT id ="btndelete" style ="DISPLAY: none" onclick ="javascript:deleteOption();" type ="button"
value ="删 除" >
< asp:Button id ="btndel" runat ="server" CssClass ="btn" Text ="删 除" ></ asp:Button ></ TD >
</ TR >
< TR >
< TD id ="tdbtn" align ="center" colSpan ="2" runat ="server" >< INPUT class ="btn" onclick ="SetValue();" type ="button" value ="确 定" >
< INPUT class ="btn" id ="btncanel" onclick ="javascript:window.close();" type ="button" value ="取 消" >
</ TD >
</ TR >
</ TABLE >
</ ajax:ajaxpanel >
</ center >
</ form >
</ body >
</ HTML >
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;
using System.Data.OracleClient;
namespace LSA.ActiveForm
{
/**//// <summary>
/// ZiDian 的摘要说明。
/// </summary>
public class ZiDian : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg;
protected MagicAjax.UI.Controls.AjaxPanel AjaxPanel1;
protected System.Web.UI.WebControls.LinkButton lbShowResult;
protected System.Web.UI.WebControls.ListBox lst;
protected string strTableName="";
protected System.Web.UI.WebControls.Button btndel;
protected System.Web.UI.WebControls.Button btnNext;
protected System.Web.UI.WebControls.Button btnPre;
protected string num = "1";
protected System.Web.UI.HtmlControls.HtmlTableCell tddrp;
protected System.Web.UI.WebControls.TextBox txtDm;
protected System.Web.UI.HtmlControls.HtmlTableCell tdbtn;
protected System.Web.UI.HtmlControls.HtmlInputText txtDm2;
加载#region 加载
private void Page_Load(object sender, System.EventArgs e)
{
//txtDm.Attributes.Add("onkeydown","if (event.keyCode==43){ alert(你输入的不是数字); } ");
//Ajax.Utility.RegisterTypeForAjax(typeof(ZiDian));
string strScript = Page.GetPostBackClientEvent(lbShowResult, "");
txtDm.Attributes.Add("onkeyup", strScript);
// 在此处放置用户代码以初始化页面
strTableName=Request.QueryString["TableName"].ToString();
if(Request.QueryString["NUM"]!=null)
this.num=Request.QueryString["NUM"].ToString();
//判断是否显示秒下拉列表与确定按钮
// if(Request.QueryString["drp"]!=null)
// {
// tddrp.Attributes.Add("style","display:none");
// tdbtn.Attributes.Add("style","display:none");
// }
if(!Page.IsPostBack)
{
GetData(strTableName);
IsShow();
}
}
#endregion
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lbShowResult.Click += new System.EventHandler(this.lbShowResult_Click);
this.btnPre.Click += new System.EventHandler(this.btnPre_Click);
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
this.dg.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_ItemCreated);
this.dg.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_ItemCommand);
this.dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_ItemDataBound);
this.btndel.Click += new System.EventHandler(this.btndel_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
绑定数据#region 绑定数据
private void GetData(string TableName)
{
string strSql="";
if(TableName.ToUpper()=="CODE_SHENSHIXIAN")
{
strSql=string.Format("select dm ,name from {0} where dm like '%{1}%' or UPPER(pym) like '%{1}%' or name like '%{1}%' order by px,dm asc ",TableName,txtDm.Text.Trim().ToUpper());
}
else if(TableName.ToUpper()=="BUMEN")
{
strSql=string.Format("select BIANHAO AS dm ,MINGCHENG AS name from {0} where BIANHAO like '%{1}%' or UPPER(BEIZHU) like '%{1}%' or MINGCHENG like '%{1}%' order by BIANHAO asc ",TableName,txtDm.Text.Trim().ToUpper());
}
else
{
strSql=string.Format("select dm ,name from {0} where dm like '%{1}%' or UPPER(pym) like '%{1}%' or name like '%{1}%' order by dm asc ",TableName,txtDm.Text.Trim().ToUpper());
}
DataSet ds=LSA.DataProvider.DatabaseHelper.Instance().ExecuteDataSet(strSql);
dg.DataSource=ds.Tables[0].DefaultView;
dg.DataBind();
}
#endregion
搜索#region 搜索
private void lbShowResult_Click(object sender, System.EventArgs e)
{
dg.CurrentPageIndex=0;
GetData(strTableName);
IsShow();
}
#endregion
给datagrid行增加双击事件#region 给datagrid行增加双击事件
private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
{
e.Item.Attributes.Add ("ondblclick", "javascript:returnData('" + e.Item.Cells[3].Text.Replace("\n","\\n") + "','" + e.Item.Cells[4].Text.Replace("\n","\\n") + "');");
string strdm=e.Item.Cells[2].Text;
string strmc=e.Item.Cells[3].Text;
e.Item.Attributes.Add
("onmouseover","this.className='tdbg-dark'");
e.Item.Attributes.Add
("onmouseout","this.className='tdbg'");
}
}
#endregion
#region
private void dg_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName.Equals("Select"))
{
string strkey=dg.DataKeys[e.Item.ItemIndex].ToString();
string strmc=e.Item.Cells[4].Text;
ListItem item=new ListItem(strmc,strkey);
lst.Items.Add(item);
}
if(e.CommandName.Equals("Select2"))
{
}
}
#endregion
多选钮事件#region 多选钮事件
private void cbo_CheckedChanged(object sender, System.EventArgs e)
{
for(int i=0;i<dg.Items.Count;i++)
{
if(((CheckBox)dg.Items[i].FindControl("chk")).Checked==true)
{
string strkey=dg.DataKeys[dg.Items[i].ItemIndex].ToString();
string strmc=dg.Items[i].Cells[4].Text;
//判断列表框中是否存在此值,若存在则不增加此项
string strLstDM=",;:";
for(int j=0;j<lst.Items.Count;j++)
{
strLstDM+=lst.Items[j].Value;
}
if(strLstDM.IndexOf(strkey,0)>0)
{
}
else
{
ListItem item=new ListItem(strmc,strkey);
lst.Items.Add(item);
}
// dg.Items[i].BackColor=Color.LawnGreen;
// dg.Items[i].Attributes.Add("style","className='tdbg-dark'");
}
else
{
string strkey=dg.DataKeys[dg.Items[i].ItemIndex].ToString();
string strmc=dg.Items[i].Cells[4].Text;
ListItem item=new ListItem(strmc,strkey);
lst.Items.Remove(item);
// dg.Items[i].BackColor=Color.Yellow;
// dg.Items[i].Attributes.Add("BackColor","className='tdbg'");
}
//((CheckBox)dg.Items[i].FindControl("chk")).Checked=false;
}
}
#endregion
单选钮事件#region 单选钮事件
private void rdb_CheckedChanged(object sender, System.EventArgs e)
{
string strkey="";
string strmc="";
for(int i=0;i<dg.Items.Count;i++)
{
if(((RadioButton)dg.Items[i].FindControl("rad")).Checked==true)
{
strkey=dg.DataKeys[dg.Items[i].ItemIndex].ToString();
strmc=dg.Items[i].Cells[4].Text;
lst.Items.Clear();
ListItem item=new ListItem(strmc,strkey);
lst.Items.Add(item);
((RadioButton)dg.Items[i].FindControl("rad")).Checked=false;
//((RadioButton)dg.Items[i].FindControl("rad")).Checked=true;
break;
}
}
}
#endregion
在datagrid创建item时注册事件#region 在datagrid创建item时注册事件
private void dg_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
{
//注册事件
CheckBox cbo=(CheckBox)e.Item.FindControl("chk");
cbo.CheckedChanged+=new EventHandler(cbo_CheckedChanged);
RadioButton rdb=(RadioButton)e.Item.FindControl("rad");
rdb.CheckedChanged+=new EventHandler(rdb_CheckedChanged);
}
}
#endregion
删除#region 删除
private void btndel_Click(object sender, System.EventArgs e)
{
for(int i=0;i<lst.Items.Count;i++)
{
if(lst.Items[i].Selected)
{
lst.Items.RemoveAt(i);
}
}
}
#endregion
下一页#region 下一页
private void btnNext_Click(object sender, System.EventArgs e)
{
int currNum=1;
try
{
int currIndex=dg.CurrentPageIndex;
currNum=dg.PageCount;
if(currNum!=currIndex+1)
{
dg.CurrentPageIndex+=1;
btnNext.Visible=true;
btnPre.Visible=true;
}
else
{
btnNext.Visible=false;
btnPre.Visible=true;
}
GetData(strTableName);
IsShow();
}
catch
{
dg.CurrentPageIndex=currNum-1;
}
}
#endregion
前一页#region 前一页
private void btnPre_Click(object sender, System.EventArgs e)
{
int currNum=1;
try
{
int currIndex=dg.CurrentPageIndex;
if(currNum!=currIndex+1)
{
dg.CurrentPageIndex-=1;
btnNext.Visible=true;
btnPre.Visible=true;
}
else
{
btnPre.Visible=false;
btnNext.Visible=true;
}
GetData(strTableName);
IsShow();
}
catch
{
dg.CurrentPageIndex+=1;
}
}
#endregion
判断单选框与多选框是否显示#region 判断单选框与多选框是否显示
private void IsShow()
{
//1表示单选,其它表示多选
if(num=="1")
{
for(int i=0;i<dg.Items.Count;i++)
{
dg.Items[i].Cells[0].Visible=false;
}
}
else
{
for(int i=0;i<dg.Items.Count;i++)
{
dg.Items[i].Cells[1].Visible=false;
}
}
}
#endregion
}
}
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;
using System.Data.OracleClient;
namespace LSA.ActiveForm
{
/**//// <summary>
/// ZiDian 的摘要说明。
/// </summary>
public class ZiDian : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg;
protected MagicAjax.UI.Controls.AjaxPanel AjaxPanel1;
protected System.Web.UI.WebControls.LinkButton lbShowResult;
protected System.Web.UI.WebControls.ListBox lst;
protected string strTableName="";
protected System.Web.UI.WebControls.Button btndel;
protected System.Web.UI.WebControls.Button btnNext;
protected System.Web.UI.WebControls.Button btnPre;
protected string num = "1";
protected System.Web.UI.HtmlControls.HtmlTableCell tddrp;
protected System.Web.UI.WebControls.TextBox txtDm;
protected System.Web.UI.HtmlControls.HtmlTableCell tdbtn;
protected System.Web.UI.HtmlControls.HtmlInputText txtDm2;
加载#region 加载
private void Page_Load(object sender, System.EventArgs e)
{
//txtDm.Attributes.Add("onkeydown","if (event.keyCode==43){ alert(你输入的不是数字); } ");
//Ajax.Utility.RegisterTypeForAjax(typeof(ZiDian));
string strScript = Page.GetPostBackClientEvent(lbShowResult, "");
txtDm.Attributes.Add("onkeyup", strScript);
// 在此处放置用户代码以初始化页面
strTableName=Request.QueryString["TableName"].ToString();
if(Request.QueryString["NUM"]!=null)
this.num=Request.QueryString["NUM"].ToString();
//判断是否显示秒下拉列表与确定按钮
// if(Request.QueryString["drp"]!=null)
// {
// tddrp.Attributes.Add("style","display:none");
// tdbtn.Attributes.Add("style","display:none");
// }
if(!Page.IsPostBack)
{
GetData(strTableName);
IsShow();
}
}
#endregion
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lbShowResult.Click += new System.EventHandler(this.lbShowResult_Click);
this.btnPre.Click += new System.EventHandler(this.btnPre_Click);
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
this.dg.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_ItemCreated);
this.dg.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_ItemCommand);
this.dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_ItemDataBound);
this.btndel.Click += new System.EventHandler(this.btndel_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
绑定数据#region 绑定数据
private void GetData(string TableName)
{
string strSql="";
if(TableName.ToUpper()=="CODE_SHENSHIXIAN")
{
strSql=string.Format("select dm ,name from {0} where dm like '%{1}%' or UPPER(pym) like '%{1}%' or name like '%{1}%' order by px,dm asc ",TableName,txtDm.Text.Trim().ToUpper());
}
else if(TableName.ToUpper()=="BUMEN")
{
strSql=string.Format("select BIANHAO AS dm ,MINGCHENG AS name from {0} where BIANHAO like '%{1}%' or UPPER(BEIZHU) like '%{1}%' or MINGCHENG like '%{1}%' order by BIANHAO asc ",TableName,txtDm.Text.Trim().ToUpper());
}
else
{
strSql=string.Format("select dm ,name from {0} where dm like '%{1}%' or UPPER(pym) like '%{1}%' or name like '%{1}%' order by dm asc ",TableName,txtDm.Text.Trim().ToUpper());
}
DataSet ds=LSA.DataProvider.DatabaseHelper.Instance().ExecuteDataSet(strSql);
dg.DataSource=ds.Tables[0].DefaultView;
dg.DataBind();
}
#endregion
搜索#region 搜索
private void lbShowResult_Click(object sender, System.EventArgs e)
{
dg.CurrentPageIndex=0;
GetData(strTableName);
IsShow();
}
#endregion
给datagrid行增加双击事件#region 给datagrid行增加双击事件
private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
{
e.Item.Attributes.Add ("ondblclick", "javascript:returnData('" + e.Item.Cells[3].Text.Replace("\n","\\n") + "','" + e.Item.Cells[4].Text.Replace("\n","\\n") + "');");
string strdm=e.Item.Cells[2].Text;
string strmc=e.Item.Cells[3].Text;
e.Item.Attributes.Add
("onmouseover","this.className='tdbg-dark'");
e.Item.Attributes.Add
("onmouseout","this.className='tdbg'");
}
}
#endregion
#region
private void dg_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName.Equals("Select"))
{
string strkey=dg.DataKeys[e.Item.ItemIndex].ToString();
string strmc=e.Item.Cells[4].Text;
ListItem item=new ListItem(strmc,strkey);
lst.Items.Add(item);
}
if(e.CommandName.Equals("Select2"))
{
}
}
#endregion
多选钮事件#region 多选钮事件
private void cbo_CheckedChanged(object sender, System.EventArgs e)
{
for(int i=0;i<dg.Items.Count;i++)
{
if(((CheckBox)dg.Items[i].FindControl("chk")).Checked==true)
{
string strkey=dg.DataKeys[dg.Items[i].ItemIndex].ToString();
string strmc=dg.Items[i].Cells[4].Text;
//判断列表框中是否存在此值,若存在则不增加此项
string strLstDM=",;:";
for(int j=0;j<lst.Items.Count;j++)
{
strLstDM+=lst.Items[j].Value;
}
if(strLstDM.IndexOf(strkey,0)>0)
{
}
else
{
ListItem item=new ListItem(strmc,strkey);
lst.Items.Add(item);
}
// dg.Items[i].BackColor=Color.LawnGreen;
// dg.Items[i].Attributes.Add("style","className='tdbg-dark'");
}
else
{
string strkey=dg.DataKeys[dg.Items[i].ItemIndex].ToString();
string strmc=dg.Items[i].Cells[4].Text;
ListItem item=new ListItem(strmc,strkey);
lst.Items.Remove(item);
// dg.Items[i].BackColor=Color.Yellow;
// dg.Items[i].Attributes.Add("BackColor","className='tdbg'");
}
//((CheckBox)dg.Items[i].FindControl("chk")).Checked=false;
}
}
#endregion
单选钮事件#region 单选钮事件
private void rdb_CheckedChanged(object sender, System.EventArgs e)
{
string strkey="";
string strmc="";
for(int i=0;i<dg.Items.Count;i++)
{
if(((RadioButton)dg.Items[i].FindControl("rad")).Checked==true)
{
strkey=dg.DataKeys[dg.Items[i].ItemIndex].ToString();
strmc=dg.Items[i].Cells[4].Text;
lst.Items.Clear();
ListItem item=new ListItem(strmc,strkey);
lst.Items.Add(item);
((RadioButton)dg.Items[i].FindControl("rad")).Checked=false;
//((RadioButton)dg.Items[i].FindControl("rad")).Checked=true;
break;
}
}
}
#endregion
在datagrid创建item时注册事件#region 在datagrid创建item时注册事件
private void dg_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
{
//注册事件
CheckBox cbo=(CheckBox)e.Item.FindControl("chk");
cbo.CheckedChanged+=new EventHandler(cbo_CheckedChanged);
RadioButton rdb=(RadioButton)e.Item.FindControl("rad");
rdb.CheckedChanged+=new EventHandler(rdb_CheckedChanged);
}
}
#endregion
删除#region 删除
private void btndel_Click(object sender, System.EventArgs e)
{
for(int i=0;i<lst.Items.Count;i++)
{
if(lst.Items[i].Selected)
{
lst.Items.RemoveAt(i);
}
}
}
#endregion
下一页#region 下一页
private void btnNext_Click(object sender, System.EventArgs e)
{
int currNum=1;
try
{
int currIndex=dg.CurrentPageIndex;
currNum=dg.PageCount;
if(currNum!=currIndex+1)
{
dg.CurrentPageIndex+=1;
btnNext.Visible=true;
btnPre.Visible=true;
}
else
{
btnNext.Visible=false;
btnPre.Visible=true;
}
GetData(strTableName);
IsShow();
}
catch
{
dg.CurrentPageIndex=currNum-1;
}
}
#endregion
前一页#region 前一页
private void btnPre_Click(object sender, System.EventArgs e)
{
int currNum=1;
try
{
int currIndex=dg.CurrentPageIndex;
if(currNum!=currIndex+1)
{
dg.CurrentPageIndex-=1;
btnNext.Visible=true;
btnPre.Visible=true;
}
else
{
btnPre.Visible=false;
btnNext.Visible=true;
}
GetData(strTableName);
IsShow();
}
catch
{
dg.CurrentPageIndex+=1;
}
}
#endregion
判断单选框与多选框是否显示#region 判断单选框与多选框是否显示
private void IsShow()
{
//1表示单选,其它表示多选
if(num=="1")
{
for(int i=0;i<dg.Items.Count;i++)
{
dg.Items[i].Cells[0].Visible=false;
}
}
else
{
for(int i=0;i<dg.Items.Count;i++)
{
dg.Items[i].Cells[1].Visible=false;
}
}
}
#endregion
}
}
在web.config下配置如下:
<
httpModules
>
< add name ="MagicAjax" type ="MagicAjax.MagicAjaxModule, MagicAjax" />
</ httpModules >
< add name ="MagicAjax" type ="MagicAjax.MagicAjaxModule, MagicAjax" />
</ httpModules >