ASP.NET Web開發實用程式碼舉例(一)

iDotNetSpace發表於2009-01-20

1.彈出對話方塊.點選轉向指定頁面

Response.Write("
Response.Write("

2.彈出對話方塊

Response.Write("
alert('產品新增成功!')
");

3.刪除檔案

string filename ="20059595157517.jpg";
pub.util.DeleteFile(HttpContext.Current.Server.MapPath("../file/")+filename);

4.繫結下拉選單框datalist

System.Data.DataView dv=conn.Exec_ex("select -1 as code,'請選擇經營模式' as content from dealin union select code,content from dealin");
this.dealincode.DataSource=dv;
this.dealincode.DataTextField="content";
this.dealincode.DataValueField="code";    
this.dealincode.DataBind();
this.dealincode.Items.FindByValue(dv[0]["dealincode"].ToString()).Selected=true;

5.時間去秒顯示

6.標題帶連結

"+ DataBinder.Eval(Container.DataItem,"proname")+""%>

7.修改轉向

"+"修改"+""%>

8.彈出確定按鈕

"+"刪除"+""%>
9.輸出資料格式化 "{0:F2}" 是格式 F2表示小數點後剩兩位

10.提取動態網頁內容

Uri uri = new Uri("http://www.xxx.com/");
WebRequest req = WebRequest.Create(uri);
WebResponse resp = req.GetResponse();
Stream str = resp.GetResponseStream();
StreamReader sr = new StreamReader(str,System.Text.Encoding.Default);
string t = sr.ReadToEnd();
this.Response.Write(t.ToString());

11.獲取" . "後面的字元

i.ToString().Trim().Substring(i.ToString().Trim().LastIndexOf(".")+1).ToLower().Trim()

12.開啟新的視窗並傳送引數:

傳送引數:

response.write("<script>window.open(’*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+...+"’)</script>")
接收引數:

string a = Request.QueryString("id");
string b = Request.QueryString("id1");

12.為按鈕新增對話方塊

Button1.Attributes.Add("onclick","return confirm(’確認?’)");
button.attributes.add("onclick","if(confirm(’are you sure...?’)){return true;}else{return false;}")

13.刪除表格選定記錄

int intEmpID = (int)MyDataGrid.DataKeys[e.Item.ItemIndex];
string deleteCmd = "Delete from Employee where emp_id = " + intEmpID.ToString

14.刪除表格記錄警告

private void DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e)
{
    switch(e.Item.ItemType)
    {
        case ListItemType.Item :
        case ListItemType.AlternatingItem :
        case ListItemType.EditItem:
        TableCell myTableCell;
        myTableCell = e.Item.Cells[14];
        LinkButton myDeleteButton ;
        myDeleteButton = (LinkButton)myTableCell.Controls[0];
        myDeleteButton.Attributes.Add("onclick","return confirm(’您是否確定要刪除這條資訊’);");
        break;
        default:
        break;
    }
}

15.點選表格行連結另一頁

private void grdCustomer_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    //點選表格開啟
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    e.Item.Attributes.Add("onclick","window.open(’Default.aspx?id=" + e.Item.Cells[0].Text + "’);");
}
雙擊表格連線到另一頁

在itemDataBind事件中

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
    string rderItemID =e.item.cells[1].Text;
    e.item.Attributes.Add("ondblclick", "location.href=’../ShippedGrid.aspx?id=" + orderItemID + "’");
}
雙擊表格開啟新一頁
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
    string rderItemID =e.item.cells[1].Text;
    e.item.Attributes.Add("ondblclick", "open(’../ShippedGrid.aspx?id=" + orderItemID + "’)");
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-541541/,如需轉載,請註明出處,否則將追究法律責任。

相關文章