如下代码,回车不会自动提交输入框内容 ,查看代码确实生成了“submit”类型的按钮,死活不提交

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="go" /><br />

但是加入一个看不见的输入框就可以正确执行了。

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input style="display:none" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="go" /><br />

C#窗口程序开发中,很样式上的问题都是通过控件的Style属性来解决的。比如:

  1. 如何让窗口大小固定?
  2. 我对这个问题一直有误区,解决这个问题我用的原来的笨办法是把窗口的最大最小的Size设置和窗口大小一致,这样虽然可以保持窗口大小不变,但是窗口右下角还是会出现可以拖拽的标志。其实只要将FormBorderStyle从Sizable改为FixedDialog,就可以达到效果了。

  3. 如何让ComboBox只读?
  4. 将ComboBox的DropDownStyle从默认的DropDown改为DropDownList即可。

其实这都是一些很简单的问题,解决方法一旦掌握会很方便,但是如果不知道的话就会一头雾水…说起来用C#也有一段时间了,但这些小东西还是需要摸索才能掌握,算是属于“经验”的知识吧。如果你在控件样式上的要求不能实现,不妨找找Style类的属性,说不定答案就在那里等着你。

第二次进机房维护,这次终于搞定。上次去了Redhat Linux自带的Adaptec aic-7902的驱动加载了一半会卡住,当时失策地只带了一个发行版本(宿舍还有几个,当时为什么没带,想不通…)没办法只好拔了新硬盘换上旧的,打道回府。答辩完了之后气势汹汹带了N个Linux发行版本(Redhat AS4,CentOS,Ubuntu,Fedora Core,Windows 2003)就不信没一个能装得上…然后各种版本试了一遍,出现各种各样不同的问题,有的没驱动,有的卡住死机,有的光盘缺文件…终于,Fedora最终在我心惊胆颤下表现稳定,顺利安装好。

回来又做了一上午性能优化和一下午BUG修正(丢失了一些数据),估计修正到明天中午,同济网服务器应该可以以一个崭新的面貌迎接下面的5年~真希望能像jth和payaqa两位前辈一样,装个机器稳定跑3年。

放个机房照片,左上角插着我的光驱的就是同济网服务器~进机房是我从小到大一直的梦想…终于实现了,原来,机房里这么冷啊…

pict00173.jpg

今天中午就接到MSRA的第二个电话面试,有点奇怪,因为前面几个电话都是晚上,我一直以为微软的前辈们都按照美国时间工作…结果今天在我午睡正香的时候打来了电话,开始问了几个项目的事情,我晕晕乎乎答得语无伦次,我明白他想知道关于开发中遇到的难题,然后我是怎么克服的,无奈实在不清醒…都想不起来了,结果我想他没有得到太多想得到的吧。之后,给我出了一个简单的算法题目要求写出来,听工程院实习的同学讲,当面面试的时候都会直接写在白板上,然后由微软的人录入编译的,这样放给我自己做,我觉得还算是降低难度了吧。

题目是这样的:有一个矩阵,每个元素是一个数,要求,从左上角出发到达右下角,每次只能向右或向下,要求全路线经过的元素和最小,求出路径及这个和的值。要求分别用递归和非递归方法实现之。
结果图:
matrix1.jpg
我花了一下午,写出了这个程序,幸运的是,两种方法的结果竟然相同…
哈哈~~玩笑话,放出代码:

[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;

namespace Matrix
{
class Program
{
///

/// 矩阵元素的最大值,用于生成随机矩阵
///

private static int MAX_ELEMENT = 10;
///

/// 矩阵的高度
///

private static int MATRIX_X = 4;
///

/// 矩阵的宽度
///

private static int MATRIX_Y = 15;

///

主函数

/// 命令行参数 static void Main(string[] args)
{
//初始化一个目标矩阵
int[,] matrix = new int[MATRIX_X, MATRIX_Y];
//初始化一个方向矩阵
string[,] direction = new string[MATRIX_X, MATRIX_Y];
//随机生成目标矩阵的元素
Generate(ref matrix, MATRIX_X, MATRIX_Y);
Console.WriteLine(“Target Matrix is:”);
//打印矩阵
PrintMatrix(matrix, MATRIX_X, MATRIX_Y);

//递归法
Console.Write(“\r\nMethod No.1 Result\r\nMinCost=” + Step(direction, matrix, MATRIX_X, MATRIX_Y, MATRIX_X – 1, MATRIX_Y – 1));
//打印结果
PrintDirection(direction, MATRIX_X, MATRIX_Y);

//清空方向矩阵
direction = new string[MATRIX_X, MATRIX_Y];
//非递归法
PrintMinCostRoute(direction, matrix, MATRIX_X, MATRIX_Y);
}

///

随机生成目标矩阵

/// 目标矩阵的引用 /// 矩阵宽度 ///矩阵高度 static private void Generate(ref int[,] matrix, int x, int y)
{
//如果未初始化,先初始化之
if (matrix == null)
{
matrix = new int[x, y];
}
//随机生成元素
Random random = new Random();
for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { matrix[i, j] = random.Next(MAX_ELEMENT - 1) + 1; } } } ///

非递归寻找最小值及路径

///路径矩阵 ///目标矩阵 ///矩阵宽度 ///矩阵高度 static private void PrintMinCostRoute(string[,] direction, int[,] matrix, int x, int y)
{
//动态规划备忘录矩阵
int[,] cost = new int[x, y];
//初始化各节点为整型变量最大值
for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { cost[i, j] = int.MaxValue; } } //从左上角逐次扩大矩阵 for (int i = 0; i < (x >= y ? x : y); i++)
{
//左上角
if (i == 0)
{
//就是本身
direction[0, 0] = “S”;
cost[0, 0] = matrix[0, 0];
}
else
{
//顺次检查当前方阵周围的元素
for (int k = 0; k < i + 1; k++) { //如果范围合法 if (i < x && k < y) { //检查比较当前的最小值和从上面过来的值 if (cost[i, k] >= (cost[i – 1, k] + matrix[i, k]))
{
//上面更小则更新
cost[i, k] = cost[i – 1, k] + matrix[i, k];
direction[i, k] = “U”;
}
if (k >= 1)
{
//检查比较当前的最小值和从左面过来的值
if (cost[i, k] >= (cost[i, k – 1] + matrix[i, k]))
{
//左面更小则更新
cost[i, k] = cost[i, k – 1] + matrix[i, k];
direction[i, k] = “L”;
}
}
}
//如果范围合法
if (i < y && k < x) { //检查比较当前的最小值和从上面过来的值 if (cost[k, i] >= (cost[k, i – 1] + matrix[k, i]))
{
//上面更小则更新
cost[k, i] = cost[k, i – 1] + matrix[k, i];
direction[k, i] = “L”;
}
if (k >= 1)
{
//检查比较当前的最小值和从上面过来的值
if (cost[k, i] >= (cost[k – 1, i] + matrix[k, i]))
{
//左面更小则更新
cost[k, i] = cost[k – 1, i] + matrix[k, i];
direction[k, i] = “U”;
}
}
}
}
}
}
Console.Write(“\r\n\r\nMethod No.2 Result\r\nMinCost=” + cost[x – 1, y – 1]);
PrintDirection(direction, x, y);
}

///

一步递归求某位置的最小值

///路径矩阵 ///目标矩阵 ///矩阵宽度 ///矩阵高度 ///位置x坐标 ///位置y坐标 /// 这个位置的最小值
static private int Step(string[,] direction, int[,] matrix, int x, int y, int pos_x, int pos_y)
{
//Debug用
//Console.WriteLine(“pos_x=” + pos_x + “,pos_y=” + pos_y);
//第一行的元素
if (pos_x == 0)
{
//第一个元素,递归结束,返回本身的值
if (pos_y == 0)
{
//特殊位置,路径指向Self=”S”
direction[pos_x, pos_y] = “S”;
//返回本身的值
return matrix[pos_x, pos_y];
}
//第一行非第一个,必然从左边走过来
else
{
//路径指向Left=”L”
direction[pos_x, pos_y] = “L”;
//左边一个的最优值加上本身得到最优值
return Step(direction, matrix, x, y, pos_x, pos_y – 1) + matrix[pos_x, pos_y];
}
}
//第一列的元素,必然从上边走过来
if (pos_y == 0)
{
//路径指向Up=”U”
direction[pos_x, pos_y] = “U”;
//上边一个的最优值加上本身得到最优值
return Step(direction, matrix, x, y, pos_x – 1, pos_y) + matrix[pos_x, pos_y];
}
//避免重复计算,先取到上面一个和左边一个的最优值
int up = Step(direction, matrix, x, y, pos_x – 1, pos_y);
int left = Step(direction, matrix, x, y, pos_x, pos_y – 1);
//如果从上面走合算
if (up <= left) { //路径指向Up="U" direction[pos_x, pos_y] = "U"; //上边一个的最优值加上本身得到最优值 return up + matrix[pos_x, pos_y]; } //如果从左面走合算 else { //路径指向Left="L" direction[pos_x, pos_y] = "L"; //左边一个的最优值加上本身得到最优值 return left + matrix[pos_x, pos_y]; } } ///

打印矩阵

/// ///矩阵宽度///
///矩阵高度
static private void PrintMatrix(int[,] matrix, int x, int y)
{
for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { Console.Write(matrix[i, j].ToString() + " "); } Console.Write("\r\n"); } } ///

打印所走的路径矩阵及走法

///路径矩阵 ///路径矩阵宽度 ///路径矩阵高度 static private void PrintDirection(string[,] matrix, int x, int y)
{
//打印路径矩阵
Console.WriteLine(“\r\nThe Direction Matrix:”);
for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { Console.Write(matrix[i, j] + " "); } Console.Write("\r\n"); } //打印走法 Console.Write("The Route is:"); //走法的逆序存放数组 string[] Reverse = new string[x + y - 2]; int pos_x = x - 1; int pos_y = y - 1; //从终点开始向路径矩阵指示的方向回溯走法 for (int i = 0; i < (x + y - 2); i++) { //从左边来的 if (matrix[pos_x, pos_y].Equals("L")) { pos_y--; Reverse[i] = "right"; } //从上边来的 else { pos_x--; Reverse[i] = "down"; } } //倒序打印走法的逆序存放数组,得到真正的从起点出发的走法 for (int i = 0; i < (x + y - 2); i++) { //倒序打印 Console.Write(Reverse[(x + y - 2) - i - 1]); if (i < (x + y - 3)) { //不是最后一个的时候打一个分隔号 Console.Write(","); } } } } } [/code]

在中创软件实习一个月期满了,好长一个月啊。做了不少东西,每天工作满8个小时,感觉就是累。而且觉得手腕疼的厉害,整天就捏着鼠标了。收获嘛,呵呵,不能说没有,但也不能说很多。公司的生活和我想象的差不多,软件开发也还算顺利,就是工具差了点,用VC++6.0做开发,这个东西可是我早就淘汰的…用.Net用惯了,对于无微不至的6.0已开始还真摸不着头脑。用了好几天才找到感觉。

cis.gif

做的工作基本上是两个。我所在的交通事业部是做高速公路软件和信息化的,什么都做,从路灯路标控制到电气甚至土木建筑。我做的第一个是给他们开发的一个用于路上显示设备的控制软件做个硬件模拟器,通过模拟器可以不使用硬件设备进行软件测试。基于SOCKET通讯的,显示相应图形图像,模拟故障和返回。第二个是一个SQL Server的存储过程,用在广州的路上,将一年以前的数据备份到一个文件然后删除,保持数据库的整洁。写一个Windows服务使得这个备份操作能够在指定的时间自动执行,无需用户操作和干预。

MySQL是一个优秀的小型数据库,应用很广。但是新版本出来之后,困扰PHPMyAdmin用户的问题也出现了:

shell> mysql
Client does not support authentication protocol requested
by server; consider upgrading MySQL client

官方的说法是

MySQL 4.1 and up uses an authentication protocol based on a password hashing algorithm that is incompatible with that used by older clients. …..

如果你升级mysql到4.1以上版本后遇到以上问题,请先确定你的mysql client 是4.1或者更高版本.(WINDOWS下有问题你就直接跳到下面看解决方法了,因为MYSQL 在WINDOWS是client和server一起装上了的)
请使用以下两种方法之一
其一:
[code=’sql’]mysql> SET PASSWORD FOR
-> ’some_user’@’some_host’ = OLD_PASSWORD(’newpwd’);[/code]
其二:
[code=’sql’]mysql> UPDATE mysql.user SET Password = OLD_PASSWORD(’newpwd’)
-> WHERE Host = ’some_host’ AND User = ’some_user’;
mysql>; FLUSH PRIVILEGES;[/code]
上面用户名密码的部分请按自己实际情况修改….
这样做后,连接就会正常了@!
但是在我的应用中,只有第二种方法管用…

下面是我实现的一个数据文件随机读取类,可以随机读取大型文本文件的某一行。在我机器上对一个130MB的文本文件,读取第200000的速度从传统做法的400ms提高到了3ms。
一般对文本文件进行读取时,一般采用ReadLine()进行逐行读取。在这种情况下,C#内的FileStream和BufferedStream类处理绰绰有余了。它不会将整个文件全部读入,而是有缓冲的读。但是,要想随机读取某一行,在行数据长度不统一的情况下,如果每次这样遍历到指定行,其效率显然是很低下的。
当然,代价也是有的,引入了第一次打开文件的打开时间,且占用了少部分内存(占用多少是可以设置的,当然占得越小速度也越慢,但最大值也比全部读入要小很多)。

FileConfig.cs;
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;

namespace DataBuffer
{
public static class FileConfig
{
public static int STREAM_BUFFER_SIZE = 1024000;
public static int MAP_DISTANCE = 10;
}
}
[/code]

DataFile.cs:
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace DataBuffer
{
public class DataFile
{
///

/// 数据文件名
///
public string fileName = “”;
///

/// 初始化读取完标志
///
public bool done = false;
///

/// 当前流位置
///
public long Position = 0;

private Hashtable head=new Hashtable();
///

/// 文件头部信息
///
public Hashtable Head
{
get
{
return head;
}
set
{
head=value;
}
}

private ArrayList map = new ArrayList();
///

/// 文件地图
///
public ArrayList Map
{
get
{
return map;
}
set
{
map = value;
}
}

private long lines = 0;
///

/// 文件数据行行数
///
public long Lines
{
get
{
return lines;
}
set
{
lines = value;
}
}
}
}
[/code]

DataBuffer.cs:
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Threading;

namespace DataBuffer
{
public class DataBuffer
{
private FileStream fs = null;
private BufferedStream bs = null;
private StreamReader sr = null;
private StreamWriter sw = null;
///

/// 文件信息数据结构
///
public DataFile dataFile = new DataFile();
///

/// 构造函数
///
/// 数据文件名

public DataBuffer(string name)
{
dataFile.fileName = name;
}

///

/// 打开文件
///
/// 成功标志
public bool Open()
{
try
{
//初始化各流
fs = new FileStream(dataFile.fileName, FileMode.Open, FileAccess.ReadWrite);
bs = new BufferedStream(fs, FileConfig.STREAM_BUFFER_SIZE);
sr = new StreamReader(fs);
sw = new StreamWriter(fs);
Thread initFile = new Thread(new ThreadStart(InitDataFile));
initFile.Start();

return true;
}
catch (Exception ee)
{
ErrorHandler.ErrorHandler eh = new ErrorHandler.ErrorHandler(ee, “文件打开”);
return false;
}
}

private void InitDataFile()
{
//另开一个读取流
BufferedStream bs = new BufferedStream(fs);
StreamReader sr = new StreamReader(bs);

//读入数据文件头信息。共14行
string thisLine = NextLine(ref sr);
dataFile.Head.Add(“Subject”, thisLine.Substring(11));
thisLine = NextLine(ref sr);
dataFile.Head.Add(“Date”, thisLine.Substring(8));
thisLine = NextLine(ref sr);
dataFile.Head.Add(“Time”, thisLine.Substring(8));
thisLine = NextLine(ref sr);
dataFile.Head.Add(“Channels”, thisLine.Substring(12));
thisLine = NextLine(ref sr);
dataFile.Head.Add(“Rate”, thisLine.Substring(8));
thisLine = NextLine(ref sr);
dataFile.Head.Add(“Type”, thisLine.Substring(8));
thisLine = NextLine(ref sr);
dataFile.Head.Add(“Rows”, thisLine.Substring(8));
thisLine = NextLine(ref sr);
thisLine = NextLine(ref sr);
dataFile.Head.Add(“Electrode Labels”, thisLine);
thisLine = NextLine(ref sr);
thisLine = NextLine(ref sr);
thisLine = NextLine(ref sr);
thisLine = NextLine(ref sr);
thisLine = NextLine(ref sr);
//降低自己的优先级
//Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;

//数行数,建立地图
long lines = 1;
//在地图中加入首条数据的位置信息
dataFile.Map.Add(dataFile.Position);
//顺序建立文件地图
while (!sr.EndOfStream)
{
thisLine = NextLine(ref sr);
if ((++lines) % FileConfig.MAP_DISTANCE == 0)
{
dataFile.Map.Add(dataFile.Position);
}
}
dataFile.Lines = lines;
dataFile.done = true;
}

///

/// 文件关闭
///
/// 成功标志
public bool Close()
{
try
{
//顺序关闭各流
sw.Close();
sr.Close();
bs.Close();
fs.Close();
return true;
}
catch (Exception ee)
{
ErrorHandler.ErrorHandler eh = new ErrorHandler.ErrorHandler(ee, “文件关闭”);
return false;
}
}

///

/// 顺序读取下一行。效率低不建议大规模使用,只在打开文件的时候使用一次
///
/// 流读取器引用

/// 下一行内容
public string NextLine(ref StreamReader sr)
{
string next = sr.ReadLine();
//+2是指Windows换行回车。Linux下要改为+1
dataFile.Position += next.Length+2;
return next;
}

///

/// 快速随机读取一行数据文件内容
///
/// 指定的行数

/// 目标行内容
public string ReadLine(long line)
{
try
{
//如果载入完毕
if (dataFile.done)
{
//确定数据块索引号
int index = (int)line / FileConfig.MAP_DISTANCE;
//移动到指定位置
bs.Seek(long.Parse(dataFile.Map[index].ToString()),SeekOrigin.Begin);
//创建流读取器
sr = new StreamReader(bs);
//移动到指定行
for (int i = 1; i <= (line - index * FileConfig.MAP_DISTANCE); i++) { sr.ReadLine(); } //返回指定行的值 return sr.ReadLine(); } else { return ""; } } catch (Exception ee) { ErrorHandler.ErrorHandler eh = new ErrorHandler.ErrorHandler(ee, "文件读取"); return ""; } } } } [/code]