ASP.NET学习笔记4
2016.4.15
1、TextChanged事件
当控件失去焦点时自动回发到服务器触发事件,需要AutoPostBack属性设为true
2、指定格式转化
int.ToString(string format);
format:一个数值格式的字符串,返回值是有format指定的字符串表示形式
格式化为金额:
double money = Convert.ToDouble(tb5.Text);
lb1.Text = money.ToString("$#,###.00");
3、ListBox控件
动态添加数据:this.ListBox1.Items.Add(new ListItem("1" + "\t" + "北京"));
隔行换色:
1 for (int i = 0; i < this.ListBox1.Items.Count; i++) 2 { 3 if ((i % 2) == 0) 4 { 5 this.ListBox1.Items[i].Attributes.Add("style", "background-color:#EEEEEE");//Attributes获得属性名称的集合,值为类不直接支持的ListItems对象 6 } 7 }
SelectedItem事件:
当控件选中一项属性时触发事件,需要AutoPostBack属性设为true
选中的内容:ListBox1.SelectedItem.Text;
2016.4.16
1、通过DropDownList控件的SelectedIndexChanged事件实现二级联动下拉菜单,需AutoPostBack属性设为true
2、SelecionMode为Single时,不能选择多项
3.单选控件 RadioButtonList
多选控件 CheckBoxList
4、用位运算实现加法
1 public static int jia(int a,int b) 2 { 3 if(a==0) 4 { 5 return b; 6 } 7 else if(b==0) 8 { 9 return a; 10 } 11 else if 12 { 13 int temp = a^b; 14 int wei = a&b; 15 wei =wei<<1; 16 return jia(temp,wei); 17 } 18 } 19
2016.4.17
1、Panel容器控件 显示和隐藏
p1.Visible = true;
p2.Visible = false;
动态添加控件:
1 Label lb3 = new Label(); 2 lb3.Text = "账号:"; 3 TextBox tb3 = new TextBox(); 4 tb3.ID = "tb3"; 5 Label lb4 = new Label(); 6 lb4.Text = "密码:"; 7 p2.Controls.Clear(); 8 p2.Controls.Add(lb3); 9 p2.Controls.Add(tb3); 10 p2.Controls.Add(new LiteralControl("<br/>")); 11
2、Calendar日期控件:
Selecteddate:选定的单个日期
SelectedDates:选定的日期范围
3、Login控件 验证用户的登陆信息
Authenticate事件:e.Authenticated = true;
4、CreateUserWizard控件 注册新用户
Web.config文件<add>节点中:
minRequiredPasswordLength//密码长度
minRequireNonalphanumericCharacters//混合其他字符个数
5、TextBox不包含onkeyup属性,在浏览器中被解释成input(Text)控件支持onkeyup属性
onkeyup:当光标在文本框中时,键盘中按键弹起时会触发
onkeyup="sp.innerText = this.value.length+tb1.value.length;"
6、默认焦点
<from>表单的defaultfocus属性设置Web窗体的默认焦点控件
defaultbutton属性设置Web窗体的默认按钮,输入控件中按Enter键触发click事件
7、VewState视图状态
int[] num ={2,4,6,8};
ViewState["num"]=num;
int[] temp = ViewState["num"] as int[];
视图状态适合应用于存储数据较少的场合
8、隐藏域HiddenField 在浏览器中不可见
9、超链接
LinkButton控件先将请求回传至服务器端,再由服务器端传到目标网页
HyperLink控件则不经过服务器端直接跳转到目标网页
10、热键
Label控件的AccessKey属性设置热键
AssociatedControlID属性设置获得焦点的控件
<asp:Label ID = "lbUserName" runat="server"
AccessKey = "U" AssociatedControlID = "tbUserName" Text="热键ALT+U"></ASP:Label>
TextBox也可以使用AccessKey属性
2016.4.18
WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping。
解决办法如下:
1、添加Global.asax文件
2、如果在本地有JQuery的源代码,可以在Application_Start中添加如下代码:
1 ScriptManager.ScriptResourceMapping.AddDefinition("jquery", newScriptResourceDefinition 2 { 3 Path = "~/js/jQuery/jquery-1.9.1.js" 4 });
1 ScriptManager.ScriptResourceMapping.AddDefinition("jquery", newScriptResourceDefinition 2 { 3 Path = "~/scripts/jquery-1.7.2.min.js", 4 DebugPath = "~/scripts/jquery-1.7.2.js", 5 CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.2.min.js", 6 CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.2.js" 7 });
Literal l1 = new Literal();
l1.Text = " ";
2、动态添加控件
Page.Controls.Add(tb1);//在页面上添加
PlaceHolder1.Controls.Add(btn2);//在容器控件上添加
3、提交表单时返回确认对话框
string Text = "return confirm('是否确认提交?')";
ClientScript.RegisterOnSubmitStatement(this.GetType(), "ConfirmSubmit", Text);
4、PostBackUrl属性实现页面传值
<asp:Button ID="btnSave" runat="server" Text="跳转" PostBackUrl="~/Target.aspx" />
跳转到的页面:
string temp = (this.PreviousPage.FindControl("tb1") as TextBox).Text;
Response.Write(temp);
5、ValidationSummary控件
<asp:ValidationSummary ID="vs1" runat="server" HeaderText="错误信息:" DisplayMode="BulletList" ShowMessageBox="True" ShowModelStateErrors="False" />
2016.4.20
连接字符串:
1 <connectionStrings> 2 <add name="ConnectionString" connectionString="data source=.;initial catalog=db_Tome1;integrated security=True;" providerName="System.Data.SqlClient" /> 3 </connectionStrings>
连接数据库:
1 string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 2 string strsql = "SELECT 产品编号, 单价, 库存量, 已订购量, 订单日期,单价 * 已订购量 AS 订货金额 FROM tb_OrderForm"; 3 SqlConnection myConn = new SqlConnection(strCon); 4 if (myConn.State.Equals(ConnectionState.Closed)) 5 myConn.Open(); 6 SqlDataAdapter da = new SqlDataAdapter(strsql, strCon); 7 DataSet ds = new DataSet(); 8 da.Fill(ds); 9 GridView1.DataSource = ds; 10 GridView1.DataBind(); 11 myConn.Close();
GridView控件换行操作:
1 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 2 { 3 // 提取当前的数据行。 4 GridViewRow row = e.Row; 5 // 如果正被创建的数据行是一个脚注,则更新数据列加总。 6 if (row.RowType == DataControlRowType.Footer) 7 { 8 // 取得脚注当中的标签控件 OrderTotalTotal 。 9 Label total = (Label)(e.Row.FindControl("OrderTotalLabel")); 10 11 // 以货币格式来显示订货金额加总。 12 if (total != null) 13 { 14 total.Text = orderTotal.ToString("c"); 15 } 16 } 17 } 18 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 19 { 20 if (e.Row.RowType == DataControlRowType.DataRow) 21 { 22 decimal totalMoney = 23 Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "订货金额")); 24 //累加订货金额并赋给变量 orderTotal。 25 orderTotal += totalMoney; 26 } 27 } 28 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 29 { 30 GridView1.PageIndex = e.NewPageIndex; 31 GridView1.DataBind(); 32 DbBind(); 33 }