.NET实在是容易把人惯坏啊。。几乎全是图形化操作。。下面总结总结我的拖控件心得。。。
1、要想实现DataGridView里面单击ButtonField获取该绑定行的数据的ID,需要将ButtonField设置为模板列,添加模板绑定,操作截图如下:
然后代码中直接使用如下代码即可输入ID号:
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "order")
{
Int32 id = Convert.ToInt32(e.CommandArgument.ToString());
}
}
2、获得母版页中的控件
((Label)Master.FindControl("welcome")).Text = username.Text +"," + ((Label)Master.FindControl("welcome")).Text;
另外,这个修改之后要修改母版页的Page_Onload函数,才能保证所有页面都修改哦~
3、aspx文件中定义JS脚本,在后台C#中调用
在</asp:Content>标签里面写脚本,如:
注意用location来跳转,如果调用完JS再用redirect会有问题。
<script type="text/javascript">
function No_Login() {
alert("请先登录!");
}
function Password_wrong() {
alert("两次密码不一致!请重新输入~");
}
function Change_Ok() {
alert("修改成功!稍后为您跳转回查询页面~");
location = "Query.aspx";
}
</script>
</asp:Content>
C#调用如下:
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>No_Login();</script>");
4、数据库中Date类型读出来不能直接和null比较,即使数据库中为空,读出也不是null,例如:
想根据return_date是否为空显示状态信息,效果如下:
则将状态那一栏添加为模板列,然后aspx中主要代码如下:
<asp:TemplateField HeaderText="状态">
<ItemTemplate>
<%# State((Object)Eval("return_date")) %>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="40px" />
</asp:TemplateField>
<script language="C#" runat="server">
public String State(Object time)
{
if (time.ToString().Equals(""))
{
return "未还";
}
else
{
return "已还";
}
}
</script>
5、GridView鼠标移上去颜色改变的方法
在后台cs文件中添加如下事件和方法:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// 如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#BDD7FD'");
//鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
}
}
6、读取web.config内容
数据库连接字符串这种东西写在web.config里,用的时候读取就好,既能避免写错,又能改起来方便。。
比如:
web.config中有如下内容
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
<add name="apparatusManagerConnectionString" connectionString="Data Source=localhost;Initial Catalog=apparatusManager;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
</connectionStrings>
注意到在connectionStrings标签里面,则程序中可以这样使用,但需要using System.Configuration
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["apparatusManagerConnectionString"].ToString());
7、判断字符串是否是数字
try
{
Convert.ToInt32(borrow_num.Text);
}
catch(Exception)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('预借数量必须是数字!')</script>");
return;
}
8、SelectedIndexChanged不执行要将dropdownlist的autopostback属性改为true。
9、下拉列表数据绑定之后要加其他的数据项需要添加属性 AppendDataBoundItems="True"
10、后台C#获取非asp控件的值,如input标签中用request.Form["控件name"]即可。
11、获得上次插入后的主键id,注意获得的是int64需要转化。执行完插入语句后接着执行select,主要代码如下:
sql = "insert into Student(name,phone,college,grade) values('" + name + "','" + phone + "','" + college + "','" + grade + "')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
sql = "select @@identity as ID from Student";
cmd.CommandText = sql;
reader = cmd.ExecuteReader();
if (reader.Read())
{
sid = reader["ID"].ToString();
}
12、sql语句中SUM如果没有的话会返回NULL,造成处理的不便,将SQL语句改为
select coalesce (sum(num),0) from PreBorrow where Gid='GBGZ0008' 则可返回0。
13、服务器控件如果是密码框的话,不允许在服务器端赋值,解决方法如下:
HttpCookie cookie = Request.Cookies["userInfo"];
if (cookie != null)
{
TBUserName.Text = cookie.Values["username"];
TBPassword.Attributes.Add("value", cookie.Values["pwd"]);
}
14、asp服务端控件的话onclick时间可以写为onClientClick来调用JS函数。