GridView的清空数据

Calchas 发表于 2010, June 22, 10:49 AM

有时候需要清空GridView的数据,填充新的数据。

可以用GridView1.DataSource = null;
去除数据,重新绑定数据源。即可!

C#代码
  1. this.GridView1.DataSource = null;  
  2. this.GridView1.DataBind();  

SqlDataSourceCommandEventArgs 类在 SqlDataSource控件中的应用

Calchas 发表于 2009, August 20, 8:40 AM

MSDN说明:
因为 SqlDataSourceCommandEventArgs 类是从 CancelEventArgs 类派生的,所以可以通过将

Cancel 属性设置为 true,取消挂起的 SqlDataSource 数据库命令。通过访问由 Command 属

性公开的 DbCommand 对象,可以在运行此命令之前,检查和操作 CommandText、Parameters

集合以及其他命令属性。

OnUpdating、OnInserting 和 OnDeleting 方法使用 SqlDataSourceCommandEventArgs 类,以

在运行 SqlDataSource 数据库命令前提供对此命令的访问。SqlDataSource 控件公开了许多事

件,可处理这些事件以在数据操作过程中使用基础数据对象。下表列出了这些事件、关联的

EventArgs 和事件处理程序类,以更好地引导您使用各种与使用 SqlDataSource 控件的数据操

作的生存期相对应的事件。
示例一:
MSDN的updating/updated事件片段
该示例演示,当使用 SqlDataSource 控件更新数据时,如何使用 DbTransaction 对象来添加

事务上下文。
private void OnSqlUpdating(Object source, SqlDataSourceCommandEventArgs e) {
DbCommand command = e.Command;
DbConnection cx = command.Connection;
cx.Open();
DbTransaction tx = cx.BeginTransaction();
command.Transaction = tx;
}

private void OnSqlUpdated(Object source, SqlDataSourceStatusEventArgs e) {
DbCommand command = e.Command;
DbTransaction tx = command.Transaction;

// In this code example the OtherProcessSucceeded variable represents
// the outcome of some other process that occurs whenever the data is
// updated, and must succeed for the data change to be committed. For
// simplicity, we set this value to true.
bool OtherProcessSucceeded = true;

if (OtherProcessSucceeded) {
tx.Commit();
Label2.Text=”The record was updated successfully!”;
}
else {
tx.Rollback();
Label2.Text=”The record was not updated.”;
}
}
示例二:数据插入过程中更改数据
//根据 @slevel传递来的数据,改变@fk参数中的数据
protected void SqlDataSource_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
//@slevel为Sql语句中参数
if (e.Command.Parameters["@slevel"].Value == “C级”)
e.Command.Parameters["@FK"].Value = 30;
}
示例三:取消删除
protected void SqlDataSource_Deleting(object sender, SqlDataSourceCommandEventArgs e)
{
if (e.Command.Parameters["@输入日期"].Value == null)
e.Cancel = true;
}

磁盘空间不足

Calchas 发表于 2009, August 20, 8:39 AM

在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
在 System.IO.FileStream..ctor(String path, FileMode mode)
在 System.Web.HttpPostedFile.SaveAs(String filename)
在 System.Web.UI.WebControls.FileUpload.SaveAs(String filename)
在 PowerEasy.SiteFactory.WebSite.Admin.Accessories.FileUpload.BtnUpload_Click(Object sender, EventArgs e)
在 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

解决过程:
检查磁盘空间剩余量大于1G
发现程序所在磁盘为NETWORK SERVICE的配额最大只有50M,而相关文件夹也是在50M,调整此配置,问题解决。

标签: asp.net

HTML表单通过 AJAX 向.NET提交数据(包含下拉框、单选框)

Calchas 发表于 2009, July 16, 9:12 AM

X.ASPX

 

XML/HTML代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="UTF-8">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  5. <title>AJAX</title>  
  6. <script language="javascript" type="text/javascript">    
  7. function $(objName){   
  8.     if(document.getElementById){   
  9.         return eval('document.getElementById("' + objName + '")');   
  10.     }else if(document.layers){   
  11.         return eval("document.layers['" + objName +"']");   
  12.     }else{   
  13.         return eval('document.all.' + objName);   
  14.     }   
  15. }   
  16. var xmlHttp = false;    
  17. try {    
  18.     xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");    
  19. }    
  20. catch (e)    
  21. {    
  22.     try {    
  23.         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");    
  24.     }    
  25.     catch (e2)   
  26.     {    
  27.         xmlHttp = false;    
  28.     }    
  29. }    
  30. if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {    
  31.     xmlHttp = new XMLHttpRequest();    
  32. }    
  33. function SendData(){   
  34.     var myxml = "<?xml version=\"1.0\"?><CollegeApply><Personal>";   
  35.     myxml += "<loginedusername>"+ $("loginedusername").value +"</loginedusername>";   
  36.     myxml += "<howtoknow>"+ $("txthowtoknow").options[$("txthowtoknow").selectedIndex].value +"</howtoknow>";      
  37.     myxml += "<otherway>"+GetRadioValue("txtotherway")+"</otherway>";   
  38.     myxml += "</Personal></CollegeApply>";   
  39.     xmlHttp.open("POST","/WebHandler/x.ashx?type=personal",false);   
  40.     xmlHttp.onreadystatechange = function() {    
  41.     if (xmlHttp.readyState < 4) {    
  42.         cname.innerHTML="loading...";    
  43.     }    
  44.     if (xmlHttp.readyState == 4) {    
  45.         var response = xmlHttp.responseText;    
  46.         cname.innerHTML = response;    
  47.     }   
  48.     }   
  49.     xmlHttp.send(myxml);       
  50.     //alert(xmlHttp.responseText);   
  51.     return false;   
  52. }   
  53. function GetRadioValue(RadioName){      
  54.     var obj;   
  55.     obj=document.getElementsByName(RadioName);      
  56.     if(obj!=null){   
  57.         var i;   
  58.         for(i=0;i<obj.length;i++){   
  59.             if(obj[i].checked){   
  60.                 return obj[i].value;   
  61.             }   
  62.         }   
  63.     }   
  64.     return null;   
  65. }    
  66. </script>  
  67. </head>  
  68. <body>  
  69. <form name="form1" action="#" method="post">  
  70. <ul>       
  71. <li>当前登陆用户<input type="hidden" id="loginedusername" value="admin" /></li>  
  72. <li class="right"><select id="txthowtoknow">  
  73. <option value="谷歌(Google)" selected="selected">谷歌(Google)</option>  
  74. <option value="雅虎(Yahoo)">雅虎(Yahoo)</option>  
  75. <option value="百度(Baidu)">百度(Baidu)</option>  
  76. <option value="搜狐">搜狐</option>  
  77. <option value="网络新闻">网络新闻</option>  
  78. <option value="论坛帖子">论坛帖子</option>  
  79. <option value="其他网站的链接" >其他网站的链接</option>  
  80. <option value="朋友介绍">朋友介绍</option>  
  81. </select></li>  
  82. <li class="right">你有没有曾经使用别名或者旧护照进入新加坡?   
  83. <input type="radio" id="txtotherway1" name="txtotherway" value="有"  />有   
  84. <input type="radio" id="txtotherway2" name="txtotherway" value="没有" checked />没有   
  85. </li>  
  86. <li><input type="submit" id="btnsubmit" value="提交" onclick="return SendData();"></li>  
  87. </ul>  
  88. </form>  
  89. </body>  
  90. </html>  

 

x.ashx

 

C#代码
  1. <%@ WebHandler Language="C#" Class="CollegeApply" %>   
  2.   
  3. using System;   
  4. using System.Web;   
  5. using System.Xml;   
  6. using System.IO;   
  7. using System.Text;   
  8.   
  9. public class CollegeApply : IHttpHandler {       
  10.     public void ProcessRequest (HttpContext context) {   
  11.         try  
  12.         {   
  13.             context.Response.ContentType = "text/plain";   
  14.             string RequestType = context.Request.QueryString["type"].ToString();   
  15.             if (RequestType == "personal")   
  16.             {   
  17.                 Encoding encode = Encoding.GetEncoding("utf-8");   
  18.                 StreamReader sr = new StreamReader(context.Request.InputStream, encode);   
  19.   
  20.                 Char[] read = new Char[1024];   
  21.                 int count = sr.Read(read, 0, 1024);   
  22.                 String str = null;   
  23.                 String sXML = null;   
  24.                 sXML = "";   
  25.   
  26.                 while (count > 0)   
  27.                 {   
  28.                     str = new String(read, 0, count);   
  29.                     sXML = sXML + str;   
  30.                     count = sr.Read(read, 0, 1024);   
  31.                 }   
  32.   
  33.                 XmlDocument document = new XmlDocument();   
  34.                 document.LoadXml(sXML);   
  35.                 XmlNode rootNode = document.DocumentElement;   
  36.                 string loginedusername = "";   
  37.                 try  
  38.                 {   
  39.                     loginedusername = rootNode.FirstChild.SelectNodes("loginedusername").Item(0).InnerText.ToString();   
  40.                     string howtoknow = rootNode.FirstChild.SelectNodes("howtoknow").Item(0).InnerText.ToString();   
  41.                     string otherway = rootNode.FirstChild.SelectNodes("otherway").Item(0).InnerText.ToString();   
  42.   
  43.                     //此处忽略执行SQL语句,直接输出   
  44.                     context.Response.Write("已经更新资料;用户名:" + loginedusername + " 如何知道我们的:"  + howtoknow + " 其他途径:" + otherway );   
  45.                 }   
  46.                 catch  
  47.                 {   
  48.                 }   
  49.             }   
  50.         }   
  51.         catch  
  52.         {   
  53.         }   
  54.     }    
  55.     public bool IsReusable {   
  56.         get {   
  57.             return false;   
  58.         }   
  59.     }   
  60. }  

标签: html, ajax, asp.net

编译器错误信息: CS0016: 未能写入输出文件

Calchas 发表于 2009, June 25, 2:43 PM

编译错误

说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。

编译器错误信息: CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\d5ed7960\90f6b658\en-us\App_GlobalResources.gsyxhw8p.resources.dll”--“拒绝访问。 ”

 

 这个问题的解决方法,其实很简单,只要在windows/temp权限设置里面把Network service(如果是win2000则是asp.net用户)的权限加上就行

关于DropDownList、RadioButtonList和CheckBoxList绑定

Calchas 发表于 2009, June 5, 1:53 PM




一、DropDownList:
1、选项值保存到数据库:
   Hashtable ht=new Hashtable();//这里用Hashtable
   ht.Add("字段名",DropDownListID.SelectedItem.Text.ToString());//保存选项Text
   ht.Add("字段名",DropDownListID.SelectedItem.Value.ToString());//保存选项Value

2、选项值由数据库绑定到DropDownList:
   首先DropDownListID.ClearSelection();//清除选项
    DropDownListID.Items.FindByText(dr["字段名"].ToString()).Selected = true;//选项Text
    DropDownListID.Items.FindByValue(dr["字段名"].ToString()).Selected = true;//选项Value

二、RadioButtonList:
1、选项值保存到数据库(同DropDownList):
   Hashtable ht=new Hashtable();//这里用Hashtable
   ht.Add("字段名",RadioButtonListID.SelectedItem.Text.ToString());//保存选项Text
   ht.Add("字段名",RadioButtonListID.SelectedItem.Value.ToString());//保存选项Value

2、选项值由数据库绑定到RadioButtonList
   string SelectItem = dr["字段名"].ToString();//将数据库中的选项值从DataRow中读出赋给变量SelectItem
   for (int i = 0; i < RadioButtonListID.Items.Count; i++)
   {//用for循环判断那项被选种
       if (RadioButtonListID.Items[i].Text == SelectItem)RadioButtonListID.Items[i].Selected = true;
   }

三、CheckBoxList:
1、选项值保存到数据库
   string SelectItem = "";//声明一个变量来接受选项
   for (int i = 0; i < CheckBoxListID.Items.Count; i++)
   {//用for循环将所有选项用","隔开连接起来
        if (CheckBoxListID.Items[i].Selected)
        {
            SelectItem = SelectItem + CheckBoxListID.Items[i].Value + ",";//选项后加","隔开
        }
   }
   ht.Add("字段名",SelectItem.ToString());

2、选项值由数据库绑定到CheckBoxList
   string SelectItem = dr["字段名"].ToString();
   string[] arrStr = SelectItem.Split(',');//字段是以","隔开
   foreach (string str in arrStr)
   {
       for (int i = 0; i <CheckBoxListID.Items.Count; i++)
       {
          if (this.CheckBoxListID.Items[i].Value == str)
          {
             this.CheckBoxListID.Items[i].Selected = true;
          }
       }
   }

标签: asp.net

获取 FormView 编辑模板中的控件

Calchas 发表于 2009, May 19, 12:52 PM

如果页面里面有一个 FormView , 里面编辑模式下有一个TextBox 控件 txtMajor, 如果想在CS文件中对其赋值的话。

单纯使用 this.txtMajor.Text = ""; 这样是不行的,会提示找不到 txtMajor 此名称的控件。

编译器错误消息: CS0117: “Index”并不包含“txtMajor”的定义

解决办法是:


TextBox txtMajor = (TextBox)fvPlanInfo.FindControl("txtMajor");

txtMajor= "";

标签: asp.net, formview

ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

Calchas 发表于 2009, April 24, 12:59 PM

用C#+ODBC做的BS系统,在VS2005里面调试没有问题,但是发布后就会出现"ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序"的错误,问题在于DSN建立的类型不对.

在"ODBC数据源管理器"中,设置用于连接数据库的DSN有三种: 用户DSN、系统DSN和文件DSN.

IIS是系统级的NT服务,因此无法访问"用户DNS"建立的数据源,应当改成"系统DNS".

标签: odbc, asp.net

Records:6012345678