0

我在运行时动态构建了 gridview 绑定到数据表,以及保存放置在 gridview 之外的 gridview 数据的按钮

1-创建网格视图

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
         CreateGrid();
    }
}

      void CreateGrid()
      {
                int nTransID = Convert.ToInt32(Session["trans_id"]);
                //
                string strSQL = @"EXEC [dbo].[sp_GetTransaction] " + nTransID;
                DataTable dtData = clsGlobal.GetDataTable(strSQL);
                //
                if (ViewState["dtTransDetail"] == null) ViewState.Add("dtTransDetail", dtData);
                else ViewState["dtTransDetail"] = dtData;
                //
                foreach (DataColumn dc in dtData.Columns)
                {
                    if (dc.ColumnName.Contains("!;"))
                    {
                        TemplateField tField = new TemplateField();
                        tField.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, dc.ColumnName);
    //\\ --- template contain textbox
                        tField.HeaderText = dc.ColumnName;
                        GridView1.Columns.Add(tField);
                    }
                }
            }

这是我的模板类:

public class AddTemplateToGridView : ITemplate
    {
        ListItemType _type;
        string _colName;

        public AddTemplateToGridView(ListItemType type, string colname)
        {
            _type = type;
            _colName = colname;
        }

        void ITemplate.InstantiateIn(System.Web.UI.Control container)
        {
            switch (_type)
            {
                case ListItemType.Item:
                    TextBox text = new TextBox();
                    text.ID = "txtAmount";
                    text.DataBinding += new EventHandler(txt_DataBinding);
                    container.Controls.Add(text);
                    break;
            }
        }

        void txt_DataBinding(object sender, EventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            GridViewRow container = (GridViewRow)textBox.NamingContainer;
            object dataValue = DataBinder.Eval(container.DataItem, _colName);
            if (dataValue != DBNull.Value)
            {
                textBox.Text = dataValue.ToString();
            }
        }  
    }

所以我有一个带有 textboxe 的 gridview 可以立即编辑问题是,当我点击保存按钮“它在 gridview 之外”时,所有 textboxe 都消失了

protected void btnSave_Command(object sender, CommandEventArgs e)
        {
for (int nRow = 0; nRow < GridView1.Rows.Count; nRow++)
            {
                for (int nCol = 0; nCol < GridView1.Columns.Count; nCol++)
                {
                    if (GridView1.Rows[nRow].Cells[nCol].Controls.Count == 0) continue;
//\\ --- Controls.Count always = 0
//\\ --- However each cell contain textbox
//\\ --- textbox disappear after save button clicked
                    TextBox txt = (TextBox)GridView1.Rows[nRow].Cells[nCol].Controls[0];
                }
            }
}
4

1 回答 1

0

看起来您在回发后没有创建 GridView,并且 Save 按钮正在导致回发。您需要在每个页面加载时动态创建 GridView。此外,我发现这个关于 ASP.NET 页面生命周期的文档在很多情况下都有帮助。

在文档中,您将看到为什么您的代码无法按您希望的方式运行的稍微不直观的原因 - btnSave_Command 直到回发和 Page_Load 之后才会运行。

于 2012-12-08T15:24:26.307 回答