asp.net2.0的几个标准控件使用的小技巧

给自己留个记录
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;
        }

    }
posted @ 2007-02-04 01:05  lu xu  阅读(1185)  评论(1编辑  收藏  举报