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 .Live 阅读(598) 评论(1)  编辑 收藏 网摘 所属分类: 程序算法和技巧

  回复  引用  查看    
#1楼 2008-08-21 11:34 | 不若相忘于江湖      
这也上精华集。 唉!

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-02-04 19:04 编辑过
Google站内搜索

相关文章:

相关链接: