ASP.NET 2.0 开发手记—续

来源:互联网 发布:淘宝实名认证修改 编辑:程序博客网 时间:2024/06/03 11:38

一、向服务器控件(DropDownList)添加客户端事件(DropDownList)
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.Attributes.Add("onchange", "youfunction();");
         }
  } 

二、向ACCESS 插入行返回自动编号   Code
cmd.CommandText = "INSERT INTO yourTableName (field1,filed2) VALUES (value1,value2)";
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT @@IDENTITY";
int n = (int)cmd.ExecuteScalar();
System.Console.WriteLine(n); 

三、添加回车事件 //Code
<script language="javascript" type="text/javascript">
function document.onkeydown()            
{
        if(event.keyCode==13)
        {
                document.getElementById("ImageButton2").click(); 
                return false;                             
        }
}
</script>

四、DropDownList 选择 SelectedIndex 问题//
   <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Value="0">0</asp:ListItem>
            <asp:ListItem Value="1">1</asp:ListItem>
            <asp:ListItem Value="0">2</asp:ListItem>  //跟0 的Value 一样
        </asp:DropDownList>
这样的话 选择了 2  SelectedIndex 的值也为0 ,  DropDownList1.SelectedItem 会是0
DropDownList是按Value 索引的,这个问题特别需要注意,上次我程序出问题
就是因为注意这个,DropDownList绑定数据库的时候发现这样一个隐蔽的问题//
解决的办法 
<asp:ListItem Value="0*0">0</asp:ListItem>
            <asp:ListItem Value="1*1">1</asp:ListItem>
            <asp:ListItem Value="0*2">2</asp:ListItem>
</asp:DropDownList>
Value 后面加一个随机数也行,然后用String.Split(*)分开,就OK了..//

五、static 变量谨慎使用 
   昨天公司给我打电话实习的公司说定单系统 单子容易出问题
   真的我在本地测试的时候 用到了static 变量 个人版单机版肯定是没有问题的
   但是多线程 多请求呢 static  就要谨慎了
   我自己做了一个实验 证明了 asp.net 下IIS 对static 只做一次初始化 以后所有的都共用
   这个变量在服务器上只有一个..任何地方都可以赋值修改 
   今天这样一查 总算解释了 捆饶我半年的问题 
   现在我推荐用ViewState
 变量 这样既省 资源 又不互相影响 
   而且 static 比较破坏类的封装性  .....//

原创粉丝点击