給自己留個記錄
1.給刪除按鈕添加個確認頁面
給普通的button按鈕和LinkButton增加個確認視窗,只要在他們的OnClickClient屬性裡寫上“return confirm('是否確認刪除這個項目?');”就可以了。在GridView和DetailsView控制項的TemplateField裡添加個delete按鈕,也可以用相同的方法實現,但如果使用DetailsView的AutoGenerateDeleteButton="True"產生出來的刪除按鈕要怎麼增加這個確認視窗?因為我們不能在設計視窗裡設定自動產生的刪除按鈕的屬性,在網上找了一圈 找到一個方法:
在DetailsView的ItemCreated的事件裡寫上以下代碼
protected void DetailsViewTips_ItemCreated(object sender, EventArgs e)
{
// Test FooterRow to make sure all rows have been created
if (DetailsViewTips.FooterRow != null)
{
// The command bar is the last element in the Rows collection
int commandRowIndex = DetailsViewTips.Rows.Count - 1;
DetailsViewRow commandRow = DetailsViewTips.Rows[commandRowIndex];
// Look for the DELETE button
DataControlFieldCell cell = (DataControlFieldCell)commandRow.Controls[0];
foreach (Control ctl in cell.Controls)
{
LinkButton link = ctl as LinkButton;
if (link != null)
{
if (link.CommandName.ToLower() == "delete")
{
link.ToolTip = "Click here to delete";
link.OnClientClick = "return confirm('Do you really want to delete this record?');";
}
}
}
}
}
2.GridView的分頁
如果Gridview裡指定了DataSourceID,那你什麼都不用做,所有的分頁和排序都能很好的自己實現。但如果是自己使用Gridview.DataSource=source;Gridview.databind();來自己綁定,那你不得不自己實現GridView的PageIndexChanging事件,代碼大致是這樣的 protected void GridViewEditor_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
DataTable source=..;
GridView.DataSource = source;
GridView.PageIndex =e.NewPageIndex;
GridView.DataBind();
}
3.DetailsView的編輯時的控制項驗證
要使用到驗證控制項,DetailsView裡的只能在他的TemplateField裡,而且每個TemplateField裡的驗證控制項也只能找到相同TemplateField裡的TextBox等控制項,如果想誇Field驗證,只能使用CustomValidator控制項,然後再實現它的CustomValidator1_ServerValidate事件 類似代碼如下:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
TextBox tb = (TextBox)DetailsViewEditor.FindControl("TextBoxGPsw");
string psw1 = tb.Text;
tb = (TextBox)DetailsViewEditor.FindControl("TextBoxPassword");
string psw2 = tb.Text;
if (psw1==psw2)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}