ASP.NET GridView中的自定义ITemplate

时间:2014-03-05 11:51:24

标签: c# 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,但没有处理”。

如何避免此错误并仅编辑数据?

谢谢。

1 个答案:

答案 0 :(得分:0)

解决方案1:

制作AutoGenerateEditButton = false

解决方案2:

如果您想使用AutoEdit按钮,请处理以下事件:

在aspx页面中,在你的网格中:

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

并在Code背后:

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 
}