GridView取不到值的问题总结

      在ASP.NET开发过程中,使用GridView进行数据表现的时候遇到过两次取不到值的问题。分别是初学的时候与

用了一年多以后。出现的问题并不是身边么高深的技术,但是可能会经常遇到,所以这里我做一下总计。GridView

取不到值的情况主要是在使用GridView中自带的编辑的过程中取不到TextBox的值。但是问题源就不只一个了。

      1.回传后重新绑定(IsPostBack)

      这个问题并不是GridView这一个控件的问题。其他控件也有可能存在同样的问题。大家都知道我们在页面中

的Page_Load事件中经常会写:     

if ( ! IsPostBack)
{
// 数据绑定
}

来避免不必要的数据回传。当在使用GridView过程中。如果数据绑定没有写在上面if块内,那么页面当我们使用

GridView中“编辑”功能点击“更新”时触发页面数据回传。首先执行 Page_Load事件。那么GridView就会

重新绑定。那么我们点击“更新”的时候取到的值就会是GridView 重新绑定后的值。这个就不用做过多解释了,只要

是搞过的人就会明白。

      2.使用了模板列(ItemTemplate)

      这个问题并不多见,用的时候比较巧合。出现的情况就是当我点击“编辑”中“更新”按钮的时候取值只有取到绑定

到上面的值,但是取不到要更新的值。问题出在哪里呢???下面这段关于GridView的这段代码就会出现这个问题。。

 

ContractedBlock.gif ExpandedBlockStart.gif Code
    /// <summary>
    
/// 点击更新事件
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void gvClass_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        
string classId = (this.GridView.Rows[e.RowIndex].FindControl("lblClassID"as Label).Text.ToString();
        
string className = ((TextBox)(this。GridView.Rows[e.RowIndex].Cells[0].FindControl("txtClassName"))).Text.ToString();
      
       PropertiesClass propertiesClass 
= new PropertiesClass();
       propertiesClass.ClassID 
= Convert.ToInt32(classId);
       propertiesClass.ClassName 
= className;

       
int i = ClassBll.UpClass(propertiesClass);
       
if (i > 0)
       {
           Page.ClientScript.RegisterStartupScript(
this.GetType(), "UpClass""alert('成功!');"true);
       }
       
else 
       {
           Page.ClientScript.RegisterStartupScript(
this.GetType(), "UpClass""alert('成功!');"true);
       }
        
this.GridView.EditIndex = -1;
        Bing();
//GridView数据绑定
    }
     
/// <summary>
    
/// 点击更新事件
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

protected void gvClass_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
if (e.CommandName == "SortCategroy")
        {
           
            
int ClassID = int.Parse(e.CommandArgument.ToString());
            ClassDAL.SetSort(ClassID);
            
if (!object.Equals(Cache["HomeTopShow"], null))
            {
                Cache.Remove(
"HomeTopShow");
            }
            
        }
        
if (e.CommandName == "ShowBtn")
        {
            
string[] Args = e.CommandArgument.ToString().Split('$');
            
string ClassID = Args[0];
            
string Show = Args[1];
            
if (Show.ToLower() == "true")
            {
                PropertiesClassDAL.SetShow(
int.Parse(ClassID), 0);
            }
            
else
            {
                PropertiesClassDAL.SetShow(
int.Parse(ClassID), 1);
            }
            
if (!object.Equals(Cache["HomeTopShow"], null))
            {
                Cache.Remove(
"HomeTopShow");
            }
        }
         Bing();();
//GridView数据绑定

    }

问题就出在使用了模板列与GridView自带的更新事情。由于回传后GridView先执行了RowCommand事件,然后才执行

RowUpdating事件,而且在RowCommand事件中执行了Bing()方法。所以当RowUpdating执行的时候获得的值是

GridView重新绑定的值。

解决办法就是:

      1.不使用RowUpdating,而是使用RowCommand事件进行数据修改。

      2.或者当RowUpdating执行时RowCommand不进行绑定。对于上面的修改就是把Bing移动到if块里面。

转载于:https://www.cnblogs.com/liucfy/archive/2009/05/24/1488199.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值