单击编辑/更新/删除按钮后,GridView动态控件将清空。Asp.net

单击编辑/更新/删除按钮后,GridView动态控件将清空。Asp.net,asp.net,gridview,dynamic,controls,onupdate,Asp.net,Gridview,Dynamic,Controls,Onupdate,我有一个使用自定义模板动态填充的gridview,但是命令字段是静态的。单击命令字段后,可以理解这些控件将丢失。这个问题在编辑期间不会影响我,因为一旦gridview恢复,它就会知道它处于编辑模式,并创建动态编辑模板字段。但一旦我对这些字段(即文本框)进行了更改,我就需要单击Update命令字段来实例化我的更新方法。单击后,控件将立即丢失,因此在我的更新方法中,我无法找到已进行更改的控件。有什么办法解决这个问题吗?我会张贴代码,如果需要的话 网格视图模板。这里是动态生成时发生数据绑定的地方。从表

我有一个使用自定义模板动态填充的gridview,但是命令字段是静态的。单击命令字段后,可以理解这些控件将丢失。这个问题在编辑期间不会影响我,因为一旦gridview恢复,它就会知道它处于编辑模式,并创建动态编辑模板字段。但一旦我对这些字段(即文本框)进行了更改,我就需要单击Update命令字段来实例化我的更新方法。单击后,控件将立即丢失,因此在我的更新方法中,我无法找到已进行更改的控件。有什么办法解决这个问题吗?我会张贴代码,如果需要的话

网格视图模板。这里是动态生成时发生数据绑定的地方。从表中获取数据并在网格视图中重新生成数据效果良好

public class GridViewTemplate : System.Web.UI.Control, System.Web.UI.ITemplate
{
    // static attributes here

    // constructor
    public GridViewTemplate(DataControlRowType type, string columnName, string categoryID, string itemControl, string editControl, string footerControl, string dataBinds)
    {
        DataRowType = type; // Header, DataRow,
        st_columnName = columnName; // Header name
        st_categoryId = categoryID;
        if (itemControl != null)
            st_itemControl = itemControl.ToUpper(); // Control type for Item Template
        if (editControl != null)
            st_editControl = editControl.ToUpper(); // Control type for Edit Template
        if (footerControl != null)
            st_footerControl = footerControl.ToUpper(); // Control type for Footer Template
        if (dataBinds != null)
            st_dataBinds = dataBinds;
    }

    public void InstantiateIn(Control container)
    {
        switch (DataRowType)
        {
            case DataControlRowType.Header:
                {
                    // Build the header for this column
                    Label lb_header = new Label();
                    lb_header.Text = "<b>" + st_columnName + "</b>";
                    lb_header.ID = st_categoryId;
                    container.Controls.Add(lb_header);
                }
                break;
            case DataControlRowType.DataRow:
                {
                    if (Regex.IsMatch(st_categoryId,"^(xxI_)")) // item mode
                    {
                        if (st_itemControl.Equals(LABEL))
                        {
                            // For Label
                        }
                        else if (st_itemControl.Equals(TEXTBOX))
                        {
                            TextBox dcrt_textbox = new TextBox();
                            dcrt_textbox.ID = st_categoryId;
                            dcrt_textbox.Visible = true;
                            dcrt_textbox.Enabled = false;
                            dcrt_textbox.DataBinding += new EventHandler(this.TextBox_DataBinding);
                            container.Controls.Add(dcrt_textbox);
                        }
                        else if (st_itemControl.Equals(CHECKBOX))
                        {
                            // For checkbox
                        }

                    }
                    else if (Regex.IsMatch(st_categoryId, "^(xxE_)")) // edit mode
                    {
                        if (st_editControl.Equals(LABEL))
                        {
                            // For label
                        }
                        else if (st_editControl.Equals(TEXTBOX))
                        {
                            TextBox dcrt_textbox = new TextBox();
                            dcrt_textbox.ID = st_categoryId;
                            dcrt_textbox.Visible = true;
                            dcrt_textbox.EnableViewState = true;
                            dcrt_textbox.AutoPostBack = false;
                            dcrt_textbox.ViewStateMode = ViewStateMode.Enabled;
                            dcrt_textbox.DataBinding += new EventHandler(this.TextBox_DataBinding);

                            container.Controls.Add(dcrt_textbox);
                        }
                        else if (st_editControl.Equals(CHECKBOX))
                        {
                            // For checkbox
                        }
                    }
                }
                break;
            case DataControlRowType.EmptyDataRow:
                // To be implemented when necessary
                break;
            case DataControlRowType.Pager:
                // To be implemented when necessary
                break;
            case DataControlRowType.Separator:
                // To be implemented when necessary
                break;
            default:
                break;
        }
    }

    public void TextBox_DataBinding(Object sender, EventArgs e)
    {
        TextBox tb_databound = (TextBox)sender;
        GridViewRow row = (GridViewRow)tb_databound.NamingContainer;
        string RawValue = DataBinder.Eval(row.DataItem, st_columnName).ToString();
        tb_databound.Text = RawValue;
    }

    public void Label_DataBinding(Object sender, EventArgs e)
    {
        Label lb_databound = (Label)sender;
        GridViewRow row = (GridViewRow)lb_databound.NamingContainer;
        string RawValue = DataBinder.Eval(row.DataItem, st_columnName).ToString();
        lb_databound.Text = RawValue;
    }

    public void CheckBox_DataBinding(Object sender, EventArgs e)
    {
        CheckBox cb_databound = (CheckBox)sender; // get the control that raised this event
        GridViewRow row = (GridViewRow)cb_databound.NamingContainer; // get the containing row
        string RawValue = DataBinder.Eval(row.DataItem, st_columnName).ToString();

        if (RawValue.ToUpper().Equals("TRUE"))
        {
            cb_databound.Checked = true;
        }
        else
        {
            cb_databound.Checked = false;
        }
    }
}
}
Gridview:

<asp:GridView ID="gv" EnableViewState="true" ViewStateMode="Enabled" OnRowEditing="onRowEditing" OnRowCancelingEdit="onRowCancelingEdit" OnRowUpdating="onRowUpdating" OnRowDeleting="onRowDeleting" EnableModelValidation="true" ShowFooter="true" OnRowCommand="onRowCommand" AutoGenerateColumns="False" runat="server">
    <Columns>  
        <asp:ButtonField Text="Analysis" ButtonType="Button" HeaderText="" ShowHeader="True"  /> 
        <asp:CommandField EditText="Edit" ButtonType="Button" HeaderText="" ShowEditButton="True" ShowHeader="True" ValidationGroup="Edit_Group"/>
        <asp:CommandField EditText="Delete" ButtonType="Button" HeaderText="" ShowDeleteButton="True" ShowHeader="True" />    
    </Columns>
</asp:GridView>


明确显示代码的相关部分。这是得到好答案的最好方法。尽你最大的努力把它提炼成必要的东西。创建一个重复该问题的小示例是一个很好的方法。显示您的GridView标记和相关代码。有人有什么想法吗?或者至少是关于常规gridview如何在进入OnRowUpdate处理程序时保持新值的一些线索?
<asp:GridView ID="gv" EnableViewState="true" ViewStateMode="Enabled" OnRowEditing="onRowEditing" OnRowCancelingEdit="onRowCancelingEdit" OnRowUpdating="onRowUpdating" OnRowDeleting="onRowDeleting" EnableModelValidation="true" ShowFooter="true" OnRowCommand="onRowCommand" AutoGenerateColumns="False" runat="server">
    <Columns>  
        <asp:ButtonField Text="Analysis" ButtonType="Button" HeaderText="" ShowHeader="True"  /> 
        <asp:CommandField EditText="Edit" ButtonType="Button" HeaderText="" ShowEditButton="True" ShowHeader="True" ValidationGroup="Edit_Group"/>
        <asp:CommandField EditText="Delete" ButtonType="Button" HeaderText="" ShowDeleteButton="True" ShowHeader="True" />    
    </Columns>
</asp:GridView>