1 .有时为防止SQL注入需要过滤字符,如对Request.QueryString[]查询字符串的过滤:
1 /// <summary> 2 /// 过滤字符 3 /// </summary> 4 /// <param name="Input"></param> 5 /// <returns></returns> 6 public static string Filter(string sInput) 7 { 8 if (sInput == null || sInput == "") 9 return null; 10 string sInput1 = sInput.ToLower(); 11 string output = sInput; 12 string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'"; 13 if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success) 14 { 15 throw new Exception("字符串中含有非法字符!"); 16 } 17 else 18 { 19 output = output.Replace("'", "''"); 20 } 21 return output; 22 }
2 . 让TextBox里的文字换行
<asp:TextBox ID="txt" TextMode="MultiLine" runat="server" Width="600" Height="100"></asp:TextBox>
把TextBox的TextMode设为MultiLine 用 换行 txt.Text="我是第一行 "+"我是第二行 ";
3 . 为repeater等控件绑定的数据添加一列,这一列可以是删除,详细等
1 //dt是已经从数据库中读出来的数据,准备绑定到repeater控件上 2 if (dt != null) 3 { 4 if (dt.Rows.Count > 0) 5 { 6 //添加列 7 dt.Columns.Add("op", typeof(string)); 8 //添加操作列 9 for (int k = 0; dt.Rows.Count > k; k++) 10 { 11 dt.Rows[k]["op"] = "<a href="tbData_YH_Detail_list.aspx?Id=" + 12 dt.Rows[k]["id"] + "" class='list_link'>详细</a> <a href="tbData_YH.aspx? 13 Type=ShenHe&Id=" + dt.Rows[k]["id"] + "" class='list_link'>审核</a> <a 14 href="tbData_YH_update.aspx?Id=" + dt.Rows[k]["id"] + "" class='list_link'>修改 15 </a> <a href="tbData_YH.aspx?Type=Del&id=" + dt.Rows[k]["id"] + "" 16 class='list_link' alt='删除' onClick="{if(confirm('确定要删除吗?')){return true;}return 17 false;}">删除</a> <input type="checkbox" name="aid" value="" + dt.Rows[k] 18 ["id"] + "" />"; 19 } 20 } 21 } 22 repeater1.DataSource = dt; 23 repeater1.DataBind();
Html代码
1 <ItemTemplate> 2 <tr class="TR_BG_list" onmouseover="overColor(this)" onmouseout="outColor(this)"> 3 <td align="center" valign="middle" ><%#((DataRowView)Container.DataItem)["ID"]%></td> 4 <td align="center" valign="middle" ><%#((DataRowView)Container.DataItem)["tbDanWei"]%></td> 5 <td align="center" valign="middle" ><%#((DataRowView)Container.DataItem)["tbTime"]%></td> 6 <td align="center" valign="middle" ><%#((DataRowView)Container.DataItem)["sbND"]%></td> 7 <td align="center" valign="middle" ><%#((DataRowView)Container.DataItem)["shenHeFlag"]%></td> 8 <td align="center" valign="middle" ><%#((DataRowView)Container.DataItem)["op"]%></td> 9 </tr> 10 </ItemTemplate>
4 .<%%>与runat="server"控制元素的显示与否
1 <form id="form1" runat="server"> 2 <%if (visible){%> 3 <div> 4 <table> 5 <tr><td>1111</td></tr> 6 </table> 7 </div> 8 <%}%> 9 <hr /> 10 <div runat="server" id="div2"> 11 <table> 12 <tr><td>2222</td></tr> 13 </table> 14 </div> 15 <asp:Button ID="Button2" runat="server" Text="显示" onclick="Button2_Click"/> 16 <asp:Button ID="Button1" runat="server" Text="隐藏" onclick="Button1_Click" /> 17 </form>
C#代码
1 protected bool visible = true; 2 protected void Button1_Click(object sender, EventArgs e) 3 { 4 visible = false; 5 div2.Visible = false; 6 } 7 8 protected void Button2_Click(object sender, EventArgs e) 9 { 10 visible = true; 11 div2.Visible = true; 12 }
5 . SqlserverHelper
http://blog.csdn.net/luxin10/article/details/7003095
6.自己封装的ASP.NET MVC地区选择器控件(htmlhelper)
调用页