gridview回发值中的动态模板字段

时间:2013-11-05 03:23:31

标签: asp.net

// Bedlow是在页面加载中添加动态控件的page_load代码。并加载page_load 将添加gvSecond的动态模板字段。我想知道为什么在每个回发值上附加点

 protected void Page_Load(object sender, EventArgs e)
    {

        gvFirst.DataSource = GetData("select top 10 * from Project_Master");
        gvFirst.DataBind();
}
//gvFirst has gvSecond and on rowdatabound of the gvFirst gvSecond populated with dynamic template fields. and all the template fields has the TextBoxtes in it 


protected void gvFirst_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    //below  code to generate dynamic template column    
    gvFirst.DataSource = GetData("select top 10 * from Project_Master");
                    gvFirst.DataBind();
            //Added dynamic controls in gvSecond
             protected void gvFirst_OnRowDataBound(object sender, GridViewRowEventArgs e)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        string customerId = gvFirst.DataKeys[e.Row.RowIndex].Value.ToString();
                        GridView gvSecond = e.Row.FindControl("gvSecond") as GridView;
                        DataTable dt= GetData1(Convert.ToInt32(customerId));
                        foreach (DataColumn col in dt.Columns)
                        {
                            //Declare the bound field and allocate memory for the bound field.
                            TemplateField bfield = new TemplateField();

                            //Initalize the DataField value.
                            bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);


                            //Initialize the HeaderText field value.
                            bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName);


                            //Add the newly created bound field to the GridView.
                            gvSecond.Columns.Add(bfield);
                        }
                        TotalColumns = dt.Columns.Count;

                        gvSecond.DataSource = dt; 
                        gvSecond.DataBind();
                    }
                }
}
//This is used to add the dynamic template in gvSecond.Now when I click on button textbox values appended with postback values and older values seperated by dot(.)

1 个答案:

答案 0 :(得分:0)

您需要将Page_Load中的代码包装在if(!IsPostback){}

编辑:

编辑在回发时创建第二个网格。

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        gvFirst.DataSource = GetData("select top 10 * from Project_Master");
        gvFirst.DataBind();
    }
    else
    {
        CreateSecondGridView();
    }
}

你的Rowdatabound可能看起来像这样:

protected void gvFirst_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    CreateSecondGridView();
}

protected void CreateSecondGridView()
{
    foreach(GridViewRow row in gvFirst.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            string customerId = gvFirst.DataKeys[row.RowIndex].Value.ToString();
            GridView gvSecond = row.FindControl("gvSecond") as GridView;
            DataTable dt= GetData1(Convert.ToInt32(customerId));
            foreach (DataColumn col in dt.Columns)
            {
                //Declare the bound field and allocate memory for the bound field.
                TemplateField bfield = new TemplateField();

                //Initalize the DataField value.
                bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);

                //Initialize the HeaderText field value.
                bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName);

                //Add the newly created bound field to the GridView.
                gvSecond.Columns.Add(bfield);
            }
            TotalColumns = dt.Columns.Count;

            gvSecond.DataSource = dt; 
            gvSecond.DataBind();
        }
    }
}