1.验证文本框输入是否为空
<script type="text/javascript">
function checknull()
{
if(document.getElementById('Text1').value=='')
{
alert('方案名称不能为空!');
return false;
}
else
return true;
}
</script>
Css...
<style type="text/css">
A:link {COLOR: #003399; TEXT-DECORATION: none}
A:visited {COLOR: #003399; TEXT-DECORATION: none}
A:active {COLOR: #003399; TEXT-DECORATION: underline}
A:hover {COLOR: #003399; TEXT-DECORATION: underline}
</style>
<style>
body,td,th{
font-size:12px;
}
</style>
2.多个控件的绑定
private void LoadExistParam()
{
string strsql = "SELECT OpParams FROM Ds_DeskConfig WHERE ID='" + Request.QueryString["ID"] + "'";
OleDbDataReader reader = Database.sqlreader(strsql);
string param = "";
while (reader.Read())
{
param = reader["OpParams"].ToString();
}
if (param == null || param.Length == 0)
return;
ArrayList ddls = GetDDLs();
for (int i = 0; i < ddls.Count; i++)
{
DropDownList ddl = (DropDownList)ddls[i];
string cfg = param.Substring(param.IndexOf(ddl.ID.Substring(3)+"="));
cfg = cfg.Substring(cfg.IndexOf("=")+1,cfg.IndexOf(",")-cfg.IndexOf("=")-1);
ddl.SelectedValue = cfg;
}
}
private ArrayList GetDDLs()
{
ArrayList ddls = new ArrayList();
ddls.Add(ddl1); ddls.Add(ddl2); ddls.Add(ddl3); ddls.Add(ddl4); ddls.Add(ddl5); ddls.Add(ddl6);
ddls.Add(ddl7); ddls.Add(ddl8); ddls.Add(ddl9); ddls.Add(ddl10); ddls.Add(ddl11); ddls.Add(ddl12);
ddls.Add(ddl13); ddls.Add(ddl14); ddls.Add(ddl15); ddls.Add(ddl16); ddls.Add(ddl17); ddls.Add(ddl18);
ddls.Add(ddl19); ddls.Add(ddl20); ddls.Add(ddl21); ddls.Add(ddl22); ddls.Add(ddl23); ddls.Add(ddl24);
ddls.Add(ddl25); ddls.Add(ddl26); ddls.Add(ddl27); ddls.Add(ddl28); ddls.Add(ddl29); ddls.Add(ddl30);
return ddls;
}
private void AddSelects()
{
ArrayList ddls = GetDDLs();
string strSQL = "SELECT EsRowID,EsName FROM Ds_EditStencil ORDER BY EsRowID";
DataSet data = Database.selectData(strSQL);
DataTable table = data.Tables[0];
for (int i = 0; i < ddls.Count; i++)
{
DropDownList ddl = (DropDownList)ddls[i];
ddl.Items.Add(new ListItem("默认","0"));
foreach (DataRow drow in table.Rows)
{
ddl.Items.Add(new ListItem(drow[1].ToString(),drow[0].ToString()));
}
}
}
3.文件上传判断类型:
function setData(id)
{
if(document.all.FileUpload1.value=="")
{
alert("请点击浏览按钮,选择您要上传的JPG文件!")
document.all.FileUpload1.focus;
return (false);
}
else
{
str= document.all.FileUpload1.value;
strs=str.toLowerCase();
lens=strs.length;
extname=strs.substring(lens-4,lens);
if(extname!=".jpg"&&extname!=".gif"&&extname!=".png"&&extname!=".bmp")
{
alert("请选择JPG,GIF,PNG,BMP格式的文件!");
return (false);
}
else
{
window.parent.document.all(id).src=document.all('<%=FileUpload1.ClientID %>').value;
window.parent.document.all("images2").src=document.all('<%=FileUpload1.ClientID %>').value;
parent.Layer1.style.visibility='hidden';
return true;
}
}
}
4.判断下拉列表框是否为空,文本框显示列表框的数据
function checkForm()
{
if(document.getElementById('<%=DropDownList1.ClientID%>').value==" ")
{
alert("请选择数据库!");
return false;
}
}
function checkForm()
{
if(document.getElementById("DropDatatype").value==" ")
{
alert("请选择数据库类型!");
return false;
}
}
function select()
{
var DropDefault=document.getElementById("DropDefault");
var pindex = DropDefault.selectedIndex;
document.getElementById("TextBox3").value=DropDefault.options[pindex].text;
}
5.遍历添加N条记录
for(int i=1;i<20;i++)
{
TextBox tb=(TextBox)this.FindControl("TextBox"+i.ToString());
DropDownList drop=(DropDownList)this.FindControl("DropDownList"+i.ToString());
DropDownList drop1=(DropDownList)this.FindControl("Drop"+i.ToString());
RadioButton radio=(RadioButton)this.FindControl("RadioButton"+i.ToString());
TextBox tb1=(TextBox)this.FindControl("txt"+i.ToString());//隐藏文本
if(tb1.Text!="")//更新
{
if(drop1.SelectedIndex!=0&&drop.SelectedIndex!=0)
{
if(radio.Checked)
{
check=1;
string updSql="update Ds_Formula set fieldID="+Convert.ToInt32(drop.SelectedValue)+",mathFormulaID="+Convert.ToInt32(drop1.SelectedValue)+",sortMath='"+tb.Text+"',sort="+check+" where FormulaID="+Convert.ToInt32(tb1.Text)+" and databaseid="+dbID+"";
if(Database.sqlExecute(updSql)>0)
{
flag=true;
}
}
else
{
string updSql="update Ds_Formula set fieldID="+Convert.ToInt32(drop.SelectedValue)+",mathFormulaID="+Convert.ToInt32(drop1.SelectedValue)+",sortMath='"+tb.Text+"',sort="+check+" where FormulaID="+Convert.ToInt32(tb1.Text)+" and databaseid="+dbID+"";
if(Database.sqlExecute(updSql)>0)
{
flag=true;
}
}
}
if(drop1.SelectedIndex==0||drop.SelectedIndex==0)
{
string delSql="delete from Ds_Formula where FormulaID="+Convert.ToInt32(tb1.Text)+" and databaseid="+dbID+"";
Database.sqlExecute(delSql);
}
}
else
{
if(drop1.SelectedIndex!=0&&drop.SelectedIndex!=0)//插入
{
if(radio.Checked)
{
check=1;
string inserSql="insert Ds_Formula (fieldID,mathFormulaID,databaseid,sortMath,sort) values ("+Convert.ToInt32(drop.SelectedValue)+","+Convert.ToInt32(drop1.SelectedValue)+","+dbID+",'"+tb.Text+"',"+check+")";
if(Database.sqlExecute(inserSql)>0)
{
flag=true;
}
}
else
{
string inserSql="insert Ds_Formula (fieldID,mathFormulaID,databaseid,sortMath,sort) values ("+Convert.ToInt32(drop.SelectedValue)+","+Convert.ToInt32(drop1.SelectedValue)+","+dbID+",'"+tb.Text+"',"+check+")";
if(Database.sqlExecute(inserSql)>0)
{
flag=true;
}
}
}
}
}
if(flag==true)
{
Response.Write("<script>alert('信息已经更改!')</script>");
}
6.保存DataGrid的数据
foreach (DataGridItem item in DataGrid1.Items)
{
int fID = Convert.ToInt32(DataGrid1.DataKeys[item.ItemIndex]);
TextBox showway = (TextBox)item.FindControl("showWay");
TextBox data = (TextBox)item.FindControl("data");
int typeID=DBEditor.getfChildTypeID(showway.Text);
string updSql = "update ds_field set fieldChildTypeID=" + typeID + ",showData='"+data.Text+"' where databaseid=" + dbID + " and fieldid=" + fID + "";
if (Database.sqlExecute(updSql) > 0)
{
flag = true;
}
}
if (flag)
{
Response.Write("<script>alert('数据已更新!')</script>");
}
7.遍历保存GridView数据
string sql = "";
for(int rIndex=0;rIndex<GridView1.Rows.Count;rIndex++)
{
GridView1.SelectedIndex = rIndex;
GridViewRow gvr = GridView1.SelectedRow;
int id = Convert.ToInt32(GridView1.SelectedDataKey.Value);
string name = ((TextBox)gvr.Cells[0].FindControl("name")).Text;
string idlist = ((HiddenField)gvr.Cells[1].FindControl("idlist")).Value;
if (name == null || name.Length == 0)
sql = "delete from Ds_UserGroup where ID=" + id;
else
sql = "update Ds_UserGroup set GroupName='" + name + "',GroupUser='" + idlist + "' where ID=" + id;
Database.sqlExecute(sql);
}
Response.Write("<script>alert('保存成功!');</script>");
8.DataGrid事件处理
aspx:
<asp:DataGrid runat="server" ID="lists"
AutoGenerateColumns="false" HeaderStyle-BackColor="#E1E1E1" HeaderStyle-Font-Bold="true"
OnItemCommand="OperateLists" OnItemCreated="DeleteSure">
<Columns>
<asp:BoundColumn HeaderText="用户名" DataField="ConfigUser" ItemStyle-Width="50%" />
<asp:TemplateColumn HeaderText="操作" ItemStyle-Width="50%">
<ItemTemplate>
<asp:LinkButton ID="SetUp" Text="设置" runat="server" CommandName="SetUp" />
<asp:LinkButton ID="User" Text="用户" runat="server" CommandName="User" />
<asp:LinkButton ID="Delete" Text="删除" runat="server" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
cs:
public void DeleteSure(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((LinkButton)e.Item.Cells[1].FindControl("Delete")).Attributes.Add("onclick", "return confirm('确定删除方案的该用户?');");
}
}
public void OperateLists(object sender, DataGridCommandEventArgs e)
{
string optype = ((LinkButton)e.CommandSource).CommandName;
string name = Request.QueryString["name"];
string user = e.Item.Cells[0].Text;
switch (optype)
{
case "SetUp":
Response.Redirect("DeskUserSetup.aspx?name="+name+"&user="+user,true);
break;
case "User":
Session["name"] = name;
Session["user"] = user;
//Response.Write("<script>window.name='"+ name +"';window.user='"+ user +
// "';showModalDialog('DialogChooseUser.aspx?user=" + user +
// "',window,'status:no;help:no;dialogWidth:400px;dialogHeight:300px');</script>");
Response.Write("<script>window.open('DialogChooseUser.aspx','_blank','menubar=no,toolbar=no,resizable=no,width=400,height=300,left=200,top=200');</script>");
break;
case "Delete":
string strDel = "DELETE FROM Ds_DeskConfig WHERE ConfigName='"+name+"' AND ConfigUser='"+user+"'" ;
Database.sqlExecute(strDel);
Response.Write("<script>alert('删除方案用户成功!');</script>");
AddLists();
break;
}
}
9.GridView事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;
if (gvr.RowType == DataControlRowType.DataRow)
{
((TextBox)gvr.Cells[0].FindControl("name")).Text = Convert.ToString(((DataRowView)gvr.DataItem)["GroupName"]);
((TextBox)gvr.Cells[1].FindControl("namelist")).Text = Tools.GetChooseName(Convert.ToString(((DataRowView)gvr.DataItem)["GroupUser"]));
((HiddenField)gvr.Cells[1].FindControl("idlist")).Value = Convert.ToString(((DataRowView)gvr.DataItem)["GroupUser"]);
string textboxid = gvr.Cells[1].FindControl("namelist").ClientID;
string hiddenid = gvr.Cells[1].FindControl("idlist").ClientID;
((Image)gvr.Cells[1].FindControl("choose")).Attributes.Add("onclick","openurl('"+
textboxid +"',document.getElementById('"+ textboxid +"').value,'"+
hiddenid +"',document.getElementById('"+ hiddenid +"').value)");
((Image)gvr.Cells[1].FindControl("choose")).Style["cursor"] = "hand";
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView1.SelectedIndex = int.Parse(e.CommandArgument.ToString());
string id = GridView1.SelectedDataKey.Value.ToString();
string sql = "delete from Ds_UserGroup where ID=" + Convert.ToInt32(id);
Database.sqlExecute(sql);
//GridViewRow GVR = GridView1.SelectedRow;
BindData();
Response.Write("<script>alert('删除成功!');</script>");
}
10. DataGrid 颜色交替
private void dgdDiary_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;
e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#A6D2FF';cursor='hand';";
if (itemType == ListItemType.Item)
{
e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#ffffff';";
}
else if (itemType == ListItemType.AlternatingItem)
{
e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#f3f3f3';";
}
/*
if(e.Item.ItemType!=ListItemType.Header)
{
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#A6D2FF'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");
}
*/
}