在ASP.NET GridView中定制ITemplate

问题描述:

所以。在ASP.NET GridView中定制ITemplate

我有一个自定义下拉列表:

public class LabelTemplate : ITemplate 
{ 

    string __field; 

    public LabelTemplate(string _field) 
    { 
     __field = _field; 
    } 

    private void Combo_DataBinding(Object sender, EventArgs e) 
    { 
     Label l = (Label)sender; 
     GridViewRow row = (GridViewRow)l.NamingContainer; 
     l.Text = DataBinder.Eval(row.DataItem, __field).ToString(); 
    } 

    public void InstantiateIn(Control container) 
    { 
     Label _l = new Label(); 
     _l.DataBinding += new EventHandler(this.Combo_DataBinding); 
     container.Controls.Add(_l); 
    } 

} 


public class ListTemplate : ITemplate 
{ 
    public ListTemplate(string _field, string _query, WebLogic _transport) 
    { 
     _vfield = _field; 
     _vquery = _query; 
     _vtransport=_transport; 
    } 


    protected WebLogic _vtransport; 
    protected string _vfield; 
    protected string _vquery; 
    protected DataRow _vrow; 

    private void Combo_DataBinding(Object sender, EventArgs e) 
    { 
     DropDownList l = (DropDownList)sender; 
     GridViewRow row = (GridViewRow)l.NamingContainer; 
     l.SelectedValue = DataBinder.Eval(row.DataItem, "SowingID").ToString(); 
    } 



    public void InstantiateIn(Control container) 
    { 

     DropDownList _l = new DropDownList(); 
     try 
     { 
      DataTable _z = _vtransport.GetList(_vquery); 
      foreach (DataRow _r in _z.Rows) 
      { 
       ListItem _ll = new ListItem(_r[1].ToString(), _r[0].ToString()); 
       _l.Items.Add(_ll); 
      } 
     } 
     catch { 
      _l.Items.Add(new ListItem("Error", Guid.NewGuid().ToString())); 
     } 
     _l.DataBinding += new EventHandler(this.Combo_DataBinding); 


     container.Controls.Add(_l); 
    } 
} 



public partial class edtRegistry : System.Web.UI.Page 
{ 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      TemplateField _fld = new TemplateField(); 
      //Label 
      LabelTemplate _lab = new LabelTemplate("SowingID"); 
      // DropDownList 
      ListTemplate _lal = new ListTemplate(_cc.ColumnName, _reg._vrfqueries [_reg._valuefields.IndexOf(_cc.ColumnName)], Transport); 

      _fld.HeaderText = _cc.ColumnName; 
      _fld.ItemTemplate = _lab; 
      _fld.EditItemTemplate = _lal; 

      GridView1.Columns.Add(_fld); 
     } 
    } 
} 

等。 GridView1自动生成了“编辑”按钮,所以当我点击它时,出现错误 - “GridView'GridView1'触发了未处理的事件RowEditing”。

我怎样才能避免这个错误,只是编辑数据?

谢谢。

+0

是否使用AutoGenerateEditButton属性= “真” 在你的网格? –

+0

如果是,则将其设为false,如AutoGenerateEditButton =“false”。 –

+0

是的,它在那里是“真的”,但是......我应该使用自己的编码编辑按钮,对吗?我喜欢autoedit按钮。 –

解决方案1:

AutoGenerateEditButton = false

解决方案2:

如果你想使用AUTOEDIT按钮,然后处理类似的事件:

在aspx页面,在你的网格:

<asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"> 

并在后面的代码:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    //Your code here 
} 

protected void GridView1_RowUpdating(object sender, GridViewEditEventArgs e) 
{ 
    // Write here code for edit Rows 
} 
+0

对不起,我到底该在RowEditing代码中放置什么? –

+0

编辑您的记录的代码。如果没有,则将其留空。 –