asp.net2.0的幾個標準控制項使用的小技巧

來源:互聯網
上載者:User

給自己留個記錄
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;
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.