.NET新手系列(三)

好几天没上了,接(二)继续:

1遇到的问题:

如何设置方法参数为可选(vb.net中)?
解决方法:
使用optional关键字之后,在调用时,可以不输入那些可选参数,例如:
Public Function ExecuteDataset(ByVal strSql As String, ByVal strTableName As String, Optional ByVal SqlParas() As SqlClient.SqlParameter = Nothing, Optional ByVal CmdType As CommandType = Nothing) As DataSet
……
End Function
且设置为可选的参数必需给定默认值。

 

2遇到的问题:
关于DbNullIs Nothing
解决方法:
IsDBNull()方法:如果表达式的数据类型计算结果为DbNull 类型,则 IsDbNull函数返回 True;否则,IsDbNull返回 False
System.DbNull 值指示 Object所表示的数据丢失或不存在。DbNull Nothing并不相同,后者指示变量尚未初始化。

 

3遇到的问题
关于vb.net的构造函数?
解决方法:
类似java中的构造函数,当类声明中没有构造函数,vb.net提供默认构造器,但当类声明中有构造函数,则vb.net不提供默认构造器,特别是当你提供的构造器是有参的构造器,则通过 New ClassName来构造类是错误的。

 

4遇到的问题:
关于RegisterClientScriptBlockkey as string,script as string)方法?
解决方法:
Key:标识脚本块的唯一键,可任意取,只要不出现重复键
Script:要发送到客户端的脚本

 

5遇到的问题:
如何在点击删除时弹出“确定、取消”对话框,并有相应操作?
解决方法:
示例:
在页面上设置三个控件:服务器label1、服务器button1、一个客户端的text控件,将其width设置为0(即将它隐藏)
在后台添加代码:

    Private Sub Page_Load(……) Handles MyBase.Load

        '在此处放置初始化页的用户代码

        Dim scriptConfirm As String

        scriptConfirm = "<script language=javascript>" + Environment.NewLine

        scriptConfirm += "function showConfirm()" + Environment.NewLine

        scriptConfirm += "{" + Environment.NewLine

        scriptConfirm += "var b=confirm('change it?');" + Environment.NewLine

        scriptConfirm += "document.Form1.tx.value=b;" + Environment.NewLine

        scriptConfirm += "}" + Environment.NewLine

        scriptConfirm += "</script>" + Environment.NewLine

        RegisterClientScriptBlock("clientconfirm", scriptConfirm)

        Me.Button1.Attributes.Add("onclick", "return showConfirm();")

    End Sub

 

    Private Sub Button1_Click(……) Handles Button1.Click

        If Me.tx.Value = "true" Then

            Me.Label1.Text = "has changed"

        End If

    End Sub


 

6遇到的问题:
关于Attributes.Add()方法
解决方法:
如果想给aspx页面上的web form control加上一些javascript的特性,可以用Attributes.Add来实现,例如,对TextBox txt,可以:
txt.Attributes.Add("onclick", "func ( );");
那么,在web页面上click它的时候,就会调用func( )这个javascript函数。
而对于HTML control,则须加runat=server才可在后台调用方法。

 

7遇到的问题:
上传图片时实现即时预览
解决方法:
<script language="javascript">
        function showimg()
        {
                  document.all("image1").src=document.all("upfile").value;
         }

</script>

…………
<input type="file" id="upfile" name="upfile" runat="server" onpropertychange="showimg()">
<asp:Image id="Image1" style="…………" runat="server"></asp:Image>

 

8遇到的问题:
关于web.config
解决方法:
web.config存放数据库连接字符串:
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

 <appSetings>

  <add key = "test" value = "This is a test!"/>

  <add key = "connString" value = …………/>

</appSettings>   

<system.web>

</configuration>

在后台代码:

string appName = ConfigurationSettings.AppSettings["connString"];

this.Label1.Text = appName;

 

9遇到的问题:
关于为dropdownlist添加数据
解决方法:
方法一:

        Dim strSQL As String = "select SName from Student"

        Dim dbop As New DBoperation

        Dim DS As New DataSet

        DS = dbop.ExecuteDataset(strSQL, "table1")

        Me.DropDownList1.DataSource = DS

        Me.DropDownList1.DataTextField = "SName"

        Me.DropDownList1.DataValueField = "SName"  //可以textvalue分别对应不同的列

        Me.DropDownList1.DataBind()

方法二:

        Dim strSQL As String = "select SName from Student"

        Dim dbop As New DBoperation

        Dim DR As SqlClient.SqlDataReader

        DR = dbop.ExecuteReader(strSQL)

        While DR.Read

            Me.DropDownList1.Items.Add(New ListItem(CStr(DR.Item(0)), CStr(DR.Item(0))))

          //可以textvalue分别对应不同的列

        End While

        DR.Close()

 

0遇到的问题:
关于去除超链接的下划线
解决方法:
可写作:
<head></head>之间加上

<style>

<!--

a:link {TEXT-DECORATION: none }

a:visited {TEXT-DECORATION: none }

a:active {TEXT-DECORATION: none }

a:hover {TEXT-DECORATION: none }

-->

</style>

这样的话该格式将应用于该页面所有的超链接

 另:可以为该格式类取个名字:

         A.myLink:visited { TEXT-DECORATION: none }

         A.myLink:link { COLOR: #3f3f3f; TEXT-DECORATION: none }

         A.myLink:hover { TEXT-DECORATION: underline }

这样可以将该格式应用于某个特殊的类,如:

<a href="" class="myLink">…………</>

posted @ 2005-10-30 19:23  后厂村思维导图馆  阅读(1574)  评论(0编辑  收藏  举报