0

//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(.)
4

1 回答 1

0

您需要将代码包装Page_Loadif(!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();
        }
    }
}
于 2013-11-05T03:28:05.510 回答