GridView编辑时动态将值转给用户自定义控件

  在 GridView 中点编辑时,将当前的值转给用户控件。当然要在 GridView 中添加一个自定义控件,首先要将该列变成模板列。在编辑模板中添加一个自定义控件。
一个简单的自定义控件,里面只有一个控件 TextBox:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WUCTest.ascx.cs" Inherits="WUCTest" %>
<asp:TextBox ID="TextBox1" runat="server" BackColor="#C0FFFF"></asp:TextBox>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class WUCTest : System.Web.UI.UserControl
{
    
private string text;
    
public string Text
    
{
        
get {
            GetText();
            
return text;
        }

        
set {
            SetText();
            text 
= value;
        }

    }

    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
private void GetText()
    
{
        text
=TextBox1.Text.Trim();
    }

    
private void SetText()
    
{
        TextBox1.Text 
= text;
    }

}

GridView控件定义:

< asp:GridView  ID ="GridView1"  runat ="server"  AutoGenerateColumns ="False"  OnRowEditing ="GridView1_RowEditing"  OnRowCancelingEdit ="GridView1_RowCancelingEdit"  OnRowCommand ="GridView1_RowCommand"  OnRowCreated ="GridView1_RowCreated"  OnRowDataBound ="GridView1_RowDataBound"  OnRowUpdating ="GridView1_RowUpdating" >
            
< Columns >
                
< asp:BoundField  DataField ="OrderID"   />
                
< asp:BoundField  DataField ="CustomerID"   />
                
< asp:BoundField  DataField ="EmployeeID"   />
                
< asp:BoundField  DataField ="OrderDate"   />
                
< asp:TemplateField >
                    
< EditItemTemplate >
                        
&nbsp; < uc1:WUCTest  ID ="WucCtrl"  runat ="server"  Text ='<%#  Bind("ShipName") % > ' />
                    
</ EditItemTemplate >
                    
< ItemTemplate >
                        
< asp:Label  ID ="LblTemp"  runat ="server"  Text ='<%#  Bind("ShipName") % > '> </ asp:Label >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:BoundField  DataField ="ShipRegion"   />
                
< asp:CommandField  ShowEditButton ="True"   />
            
</ Columns >
        
</ asp:GridView >
页面代码实现:
public  partial  class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            BindGvw();
        }


    }

    
//定义要传的值对象
    private string Text;
    
private void BindGvw()
    
{
        DataTable dt
=new DataTable ();
        
string Connstr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; 
        SqlDataAdapter da
=new SqlDataAdapter ("select top 10 * from Orders",Connstr);
        da.Fill (dt);
        
this.GridView1.DataSource = dt;
        
this.GridView1.DataBind();
       
    }

    
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        
//保存要传递的值
        Text =((Label)GridView1.Rows[e.NewEditIndex].Cells[4].FindControl("LblTemp")).Text;
        GridView1.EditIndex 
= e.NewEditIndex;
        BindGvw();
    }

   
    
//在RowCreated实现传值
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    
{
        
string temp = e.Row.ToString();
        
if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
        
{
            
//重点
           ((WUCTest)e.Row.FindControl("WucCtrl")).Text =Text;
        }

    }

    
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{

    }

    
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        BindGvw();
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值