一些简单的方法!!
checkBox全选
<script type="text/javascript">
function SelectAllCheckboxes(spanChk)
{
var oItem=spanChk.children;
var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.tiem[0];
xState=theBox.checked;
elm=theBox.form.elements;
for(i=0;i<elm.length;i++)
if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
{
if(elm[i].checked!=xState)
elm[i].click();
}
}
</script>
<asp:CheckBox ID="CheckBox1" runat="server" type="checkbox" onclick="javascript:SelectAllCheckboxes(this);" />
将gridview中的数据全部导出(包括分页)为excel文件
protected void btnLoad_Click(object sender, EventArgs e)
{
Response.Clear();
//可令中文文件名不为乱码
Response.AppendHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode (this.Title, System.Text.Encoding.UTF8) + ".csv");
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
for (int i = 1; i < GridView1.Columns.Count; i++)
{
Response.Write(GridView1.Columns[i].HeaderText + ",");
}
Response.Write("\r\n");
DataView DefaultDataView = ds.Tables[0].DefaultView;
int colCount = DefaultDataView.Table.Columns.Count;
foreach (DataRowView rowView in DefaultDataView)
{
// Display the value in each item of the DataRowView
for (int i = 0; i < colCount; i++)
{
string strvalue = rowView[i].ToString().Replace('\r', ' ').Replace('\n', ' ').Replace('\t', ' ').Replace(",", ",").Trim();
Response.Write(strvalue + ",");
}
Response.Write("\r\n");
}
Response.End();
}
所有的控件变成只读
public static void ControlReadOnly(Control c)
{
foreach (Control childControl in c.Controls)
{
if (childControl is TextBox)
((TextBox)childControl).ReadOnly = true;
else
ControlReadOnly(childControl);
if (childControl is Button)
((Button)childControl).Enabled = false;
else
ControlReadOnly(childControl);
if (childControl is DropDownList)
((DropDownList)childControl).Enabled = false;
else
ControlReadOnly(childControl);
if (childControl is GridView)
((GridView)childControl).Enabled = false;
else
ControlReadOnly(childControl);
if (childControl is RadioButton)
((RadioButton)childControl).Enabled = false;
else
ControlReadOnly(childControl);
if (childControl is RadioButtonList)
((RadioButtonList)childControl).Enabled = false;
else
ControlReadOnly(childControl);
if (childControl is CheckBox)
((CheckBox)childControl).Enabled = false;
else
ControlReadOnly(childControl);
if (childControl is CheckBoxList)
((CheckBoxList)childControl).Enabled = false;
else
ControlReadOnly(childControl);
}
}
简单的连接处理方法
public static Database db = DatabaseFactory.CreateDatabase("DatabaseConnectionString");
public static bool Add(User d)
{
string sqlCommand = "insert into Users (UserName,password,StoreID,Phone,Email,RealName,UserTypeID) values('" + d.UserName + "','" + d.Password + "','" + d.StoreID + "','" + d.Phone + "','" + d.Email + "','" + d.RealName + "','" + d.TypeID + "')";
DbCommand mydbCommand = db.GetSqlStringCommand(sqlCommand);
int i = db.ExecuteNonQuery(mydbCommand);
if (i == -1)
return false;
return true;
}
弹出提示
Response.Write("<script>alert('操作失败,请检查数据的有效性')</script>");
传值
string s_url;
s_url = "ShowCustomerInfo.aspx?name=" + this.tbPhone0.Text.Trim();
Response.Redirect(s_url);
取值
string name = Request.QueryString["name"];
编码密码
public static string MD5(string str)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
}
gridview中删除选定的记录!!
users d = new users();
User method = new User();
foreach (GridViewRow aa in this.GridView1.Rows)
{
bool isCheck = ((CheckBox)aa.FindControl("aa")).Checked;
if (isCheck)
{
d.getUserid = Convert.ToInt32(this.GridView1.DataKeys[aa.RowIndex]["ID"].ToString());
if (method.delete(d))
{
Response.Write("<script>alert('管理已删除!!')</script>");
}
this.DataBind();
}
}
gridview中添加提示对话框!!
在DeleteText中添加如下语句
<div id="de" onclick="JavaScript:return confirm('确定删除吗?')">删除</div>
显示完提示信息后打开新的窗体
public static void Show(string values,string PageURL)
{
HttpContext.Current.Response.Write("<script>alert('" + values + "');window.location.href='" + PageURL + "'</script>");
HttpContext.Current.Response.End();
}
将gridview中的记录变成超链接的同时,将绑定的记录带到指定的窗体,
然后在制定的窗体里调用Request.Params["article_title"]就可以获取到传过来的值!
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="article_title"
DataNavigateUrlFormatString="~/Show_Information.aspx?article_title={0}"
HeaderText=标题 DataTextField="article_title" />
</Columns>
先绑定一列后,将他删除,直接复制此段代码,将要传的值的字段修改即可!!