代码改变世界

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

2007-08-01 22:17  ruinet  阅读(1010)  评论(0编辑  收藏  举报
 GridView中点编辑时,将当前的值转给用户控件。当然要在GridView中添加一个自定义控件,首先要将该列变成模板列。在编辑模板中添加一个自定义控件。
一个简单的自定义控件,里面只有一个控件TextBox:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WUCTest.ascx.cs" Inherits="WUCTest" %>
<asp:TextBox ID="TextBox1" runat="server" BackColor="#C0FFFF"></asp:TextBox>
控件实现代码:

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();
    }


free web counter