.NET新手系列(四)

1遇到的问题:

如何获得上传图片的的文件名后缀?

解决方法:

假设你的HtmlInputFileIDupfile,要取得上传的文件夹的后缀名示例如下:

if(upfile.PostedFile!=null)

string fileext=System.IO.Path.GetExtention(upfile.PostedFile.FileName);

另:以上方法将获得文件后缀,包括后缀前的"."

 

 

2遇到的问题:

如何在表格出现滚动条?

解决方法:

<table>

<tr>

<td class='bodytd' align='center' valign='top' >

    <div style='height:178;overflow-y: auto;overflow-x:hidden'>

    </div>

</td>";

</tr>

</table>

其中style中可将overflow属性设置成:autohiddenscroll,

须设定divheight,在超过该高度时,autoscroll将出现滚动条。

 

 

3遇到的问题:

制作细线表格

解决方法:

方法1:把表格的cellspacing设置为1cellpaddingborder均为0,分别填充表格的背景色和单元格的背景色。举例如下:

<table border="0" cellspacing="1" cellpadding="0" bgcolor="#87a4c5"

<tr 

<td height=26 width=120 bgcolor=white> </td

</tr

</table

则出现淡蓝色细框,效果较好

 

方法2:把cellspacingcellpaddingborder均设置为1,分别设定表格的边框色和内部单元格的边框色,注意使内部单元格的边框色和网页背景色或表格背景色相同,这样内部的边框看上去就和背景融为一体了。举例如下:

<table width="550" border="1" cellspacing="1" cellpadding="1" bordercolor="#666666"

<tr bordercolor="#FFFFFF" 

<td> </td

</tr

<tr bordercolor="#FFFFFF" 

<td> </td

</tr

</table



方法3:用CSS定义表格的边框宽度为1个像素。先设置CSStable1 { border-top: 0px; border-right: 0px; border-bottom: 0px; border-left:0px},然后在<table>标签里添加class="table1"



方法4:做一个和表格等大的图片作为表格的背景,尤其当你想使表格边线有倒角时。

 

 

4遇到的问题:

如何将用户上传的图片存入文件夹,并将文件路径保存到数据库中

解决方法:

示例:

        '预定义一个本地保存路径

        Dim strDir As String = "e:\\vs.net\\myweb\\img\\"

 

        If Me.upfile.PostedFile.ContentLength <> 0 Then

            '获得上传文件在客户端的全路径

            Dim strClientFullPath As String = Me.upfile.PostedFile.FileName

            '获得文件名,包括其后缀

            'Dim strFileName As String = Path.GetFileName(strClientFullPath)

            '从全路径中获得文件的后缀

            Dim strExtention As String = Path.GetExtension(strClientFullPath)

            '对文件格式进行约束

            If strExtention = ".jpg" Or strExtention = ".bmp" Or strExtention = ".gif" Then

                '对文件大小进行约束

                If Me.upfile.PostedFile.ContentLength <= 100 * 1024 Then

                    '以上传时间为文件名,以防止出现重名

                    Dim strFileName As String = DateTime.Now.Year.ToString

                    strFileName += DateTime.Now.Month.ToString

                    strFileName += DateTime.Now.Day.ToString

                    strFileName += DateTime.Now.Hour.ToString

                    strFileName += DateTime.Now.Minute.ToString

                    strFileName += DateTime.Now.Second.ToString

                    strFileName += strExtention

 

                    If Not Directory.Exists(strDir) Then

                        Directory.CreateDirectory(strDir)

                    End If

                    Dim strServerFullPath As String = strDir + strFileName

                    Me.upfile.PostedFile.SaveAs(strServerFullPath)

 

                    Try

                        Dim strSQL As String =

"insert into imgpath(path) values('" & strServerFullPath & "')"

                        Dim dbop As New DBoperation

                        dbop.ExecuteNonQuery(strSQL)

                        Me.tbTime.Value = "上传操作成功,文件另存为" + strFileName

                    Catch ex As Exception

                        Me.tbTime.Value = "数据库操作失败!"

                    End Try

                Else

                    Me.tbTime.Value = "操作失败!文件大小不得超过100K"

                End If

            Else

                Me.tbTime.Value = "操作失败!只允许上传.jpg.bmp.gif图片"

            End If

        Else

            Me.tbTime.Value = "请选择所要上传的文件!"

        End If

 

 

5遇到的问题:

关于时间差和timespan

解决方法:

        Dim oldday As New DateTime(2005, 10, 10)

        Dim nowday As DateTime = DateTime.Now

        Dim ts As TimeSpan = nowday.Subtract(oldday)

        Me.Label1.Text = ts.Days.ToString

 

 

6遇到的问题:

关于获取主目录等相关问题

解决方法:

        Dim a1 As String = Server.MapPath("CopyWebForm1.aspx")             'e:\vs.net\myweb\CopyWebForm1.aspx

        Dim a2 As String = Me.MapPath(".")   'e:\vs.net\myweb

        Dim a3 As String = Server.MapPath(".")   'e:\vs.net\myweb

        Dim a4 As String = Me.MapPath("/")   'e:\vs.net\     获得主目录

        Dim a5 As String = Server.MapPath("/")  'E:\vs.net\  获得主目录

 

 

7遇到的问题:

关于文件夹、文件拷贝等问题

解决方法:

(当然要引入IOimportsSystem.IO

Directory.CreateDirectory(strDir + "\\" + strFolder)

创建文件:(指定全路径,包括文件后缀)

File.Create(strDir + "\\" + strFolder + "\\some.aspx")

拷贝文件:(some1.aspx不必预先存在,copy过程中自动生成)

     File.Copy("e:\\vs.net\\myweb\\CopyWebForm2.aspx","e:\\vs.net\\myweb\\1111\\some1.aspx")

 

 

8遇到的问题:

如何使用cookie对象?

解决方法:

页面一:

              HttpCookie myCookie=new HttpCookie("myCookie");

              DateTime dt=DateTime.Now ;

              TimeSpan ts=new TimeSpan(0,0,5,0,0);//有效期5分钟

              myCookie.Expires =dt+ts;

              myCookie["username"]="abei";

              myCookie["password"]="123456";

              Response.Cookies.Add(myCookie);

页面二:

              HttpCookie coo=Request.Cookies["myCookie"];

              if(coo!=null)

              {

                   System.Collections.Specialized.NameValueCollection valueColl=coo.Values ;

                   string name=valueColl["username"];

                   string password=valueColl["password"];

                   if(name=="abei"&&password=="123456")

                   {

                       this.Label1.Text ="用户已经登录";

                   }

              }

              else

              {

                   this.Label1.Text ="未登录,不允许提交";

              }

 

 

9遇到的问题:

关于session的问题

解决方法:

页面一中放置两个按钮:

后台事件:

         private void Button1_Click(object sender, System.EventArgs e)

         {

              HttpSessionState sess=HttpContext.Current.Session;

              sess.Add("username","abei");

              sess.Add("password","123456");

         }

 

         private void Button2_Click(object sender, System.EventArgs e)

         {

              Session.Add("username","abei");

              Session.Add("password","123456");

         }

//以上两种方法均可设置成功session

页面二中:

HttpSessionState sess=HttpContext.Current.Session;

//display "abei 123456789"

//该窗口必须在页面一中以链接等方式打开,如果另打开ie,手动输入本页面的地址将无法获得session

//即两个窗口须有父子关系

this.Label1.Text =sess["username"].ToString()+Environment.NewLine+sess["password"].ToString();

 

 

0遇到的问题:

关于html中的<font>标识

解决方法:

     <font size="10" color="red" >侧身</font>

 

     <font style ="font-size:10pt" color="red">侧身</font>

第一行中size无法指定以pt为单位的字号,字号10表示较大的一个字体,约为word中的“小初”

第二行中可以通过style中指定font-size指定以pt为单位的字体大小,10pt字体约为“小五”

 

posted @ 2005-11-13 22:45  后厂村思维导图馆  阅读(1199)  评论(0编辑  收藏  举报