在detaiilview的编辑模式,自定义dropdownlist

((Label)DetailsView1.FindControl("tmpLast")).Text;

tmpLast是自定义了一个label,可以设成visible="false".来存值。

                if (DetailsView1.CurrentMode == DetailsViewMode.Edit)

((Label)DetailsView1.FindControl("tmpLast")).Text;

tmpLast是自定义了一个label,可以设成visible="false".来存值。

                if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
                {
                    ddlLast((DropDownList)DetailsView1.FindControl("sf_last"));
                    ((DropDownList)DetailsView1.FindControl("sf_last")).SelectedValue = ((Label)DetailsView1.FindControl("tmpLast")).Text;
                }

 

        protected void ddlLast(DropDownList myddlLast)
        {
            myddlLast.Items.Add(new ListItem("15天", "15"));
            myddlLast.Items.Add(new ListItem("30天", "30"));
            myddlLast.Items.Add(new ListItem("半年", "180"));
            myddlLast.Items.Add(new ListItem("1年", "365"));
        }

在asp.net中统计字符串A在字符串B中出现的次数

思路有多种。

  1. 正则验证匹配
  2. split分割
  3. replace替换算长度

下面以第三种方法举例

            string strReplace = "http";
            int CountHttp = ((strContent.Length – (strContent.Replace(strReplace, string.Empty)).Length)) / strReplace.Length;

思路有多种。

  1. 正则验证匹配
  2. split分割
  3. replace替换算长度

下面以第三种方法举例

            string strReplace = "http";
            int CountHttp = ((strContent.Length – (strContent.Replace(strReplace, string.Empty)).Length)) / strReplace.Length;
 

实现的思路,想算A字符串在B字符串出现的次数.

可以用 (B字符串的长度, 去除含有的A字符串后的B字符串的长度) 除以A字符串的长度。

 

 

 

asp.net中login控件,RememberMe可能是个bug

利用vs2008中的login控件时,转成模板,后台却得不到RememberMe的值。

public partial class userlogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

利用vs2008中的login控件时,转成模板,后台却得不到RememberMe的值。

public partial class userlogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Login1.DestinationPageUrl = "LoginMessage.aspx";

            if (Request.IsAuthenticated == true)
            {
                Response.Redirect(Login1.DestinationPageUrl);
            }
           
           
        }

        protected void userLogin_Authenticate(object sender, AuthenticateEventArgs e)
        {
            string strUserName = Login1.UserName;
            string strPassWord = FormsAuthentication.HashPasswordForStoringInConfigFile(Login1.Password, "MD5");
            CheckBox rm = (CheckBox)Login1.FindControl("RememberMe");//估计是个微软的bug
           
            string[] ReResult = Users.ValidateUserNameAndPwd(strUserName, strPassWord);
            string ReStr = ReResult[1];

            switch (ReStr)
            {
                case "noUser":
                    e.Authenticated = false;
                    break;
                case "ErrorPWD":
                    e.Authenticated = false;
                    break;
                case "Access":
                    HttpCookie objCookie = new HttpCookie("UserInfo");
                    Response.Cookies.Remove("UserInfo");
                    objCookie.Values.Add("UserName", strUserName);
                    objCookie.Values.Add("UserPwd", strPassWord);
                    objCookie.Values.Add("UserID", ReResult[0]);
                    if (rm.Checked)
                    {
                        objCookie.Expires = DateTime.Now.AddDays(15);
                    }

                    Response.AppendCookie(objCookie);

                    e.Authenticated = true;
                    break;
            }           
             
        }

在asp.net中将detailview设为默认编辑状态

 <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
        DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px" Width="100%" 

 <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
        DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px" Width="100%" 
        DefaultMode="Edit" onitemupdated="DetailsView1_ItemUpdated"
        onitemupdating="DetailsView1_ItemUpdating" >

DefaultMode="Edit" 来实现, 但你会发现,detailview不会自动回到readonly的状态了。

解决方法:onitemupdating="DetailsView1_ItemUpdating"
cs文件代码

        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            DetailsView1.DefaultMode = DetailsViewMode.ReadOnly;
        }

 

利用存储过程,实现GridView中的删除

cs文件中       

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlDataSource1.DeleteParameters["InfoID"].DefaultValue = Convert.ToString(GridView1.DataKeys[e.RowIndex].Value);

cs文件中       

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlDataSource1.DeleteParameters["InfoID"].DefaultValue = Convert.ToString(GridView1.DataKeys[e.RowIndex].Value);
;        }

 

.aspx文件中

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="id" 
                AutoGenerateColumns="False"  HorizontalAlign="Center" Width="100%"
        CellPadding="4" ForeColor="#333333"
            GridLines="None" 
        onrowdeleting="GridView1_RowDeleting">

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="GetRecordFromPages"
        SelectCommandType="StoredProcedure" DeleteCommand="DeleteUserInfoBySelf"
        DeleteCommandType="StoredProcedure" OldValuesParameterFormatString="InfoID">

        <DeleteParameters>            
            <asp:Parameter Name="InfoID" Type="Int32" />
            <asp:Parameter Name="UserID" Type="Int32" />           
        </DeleteParameters>

FileUpload上传之文件大小与类型的限制方法

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string savePath = Server.MapPath("/Upload");//保存的路径

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string savePath = Server.MapPath("/Upload");//保存的路径
            if (FileUpload1.HasFile)
            {
                try
                {
                    string fileName = Server.HtmlEncode(FileUpload1.FileName);
                    string extension = Path.GetExtension(fileName);//获得后缀

                    if (FileUpload1.PostedFile.ContentLength > 100000)//文件大小限制
                    {
                        lblPic.Text = "文件不能超过100K";
                        return;
                    }

                    if ((extension == ".jpg") || (extension == ".gif") || (extension == ".png") || (extension == ".jpeg"))
                    {
                        FileUpload1.SaveAs(savePath + fileName);
                    }
                    else
                    {
                        lblPic.Text = "限上传:gif,jpg,jpeg,png格式的图片";
                        return;
                    }
                }
                catch (Exception ex)
                {
                    //
                }            
           
            }

中<system.web>中加入

      <httpRuntime executionTimeout="90" maxRequestLength="200000" useFullyQualifiedRedirectUrl="false" />

上传是先校验这里的大小的。默认最大为4M。上传超过此大小的就会报错了。

 

 

asp.net中的upload上传

.asp页

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpLoad.aspx.cs" Inherits="gqlm.UpLoad" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

.asp页

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpLoad.aspx.cs" Inherits="gqlm.UpLoad" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>上传</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="保存" />
   
    </div>
    </form>
</body>
</html>
 

 

cs页

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace gqlm
{
    public partial class UpLoad : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string str = string.Empty;
            if (FileUpload1.HasFile)
            {
                try
                {
                    FileUpload1.SaveAs(Server.MapPath()+ "\Upload\" + FileUpload1.FileName);

                }
                catch (Exception ex)
                {
                    //
                }            
           
            }
        }
    }
}