none
GridView动态生成列,无法找到控件 RRS feed

  • 问题

  • 我的aspx中的代码如下:

    <asp:GridView ID="GV_Project" runat="server" AllowPaging="True" CellPadding="4" 
                                    ForeColor="#333333" GridLines="None" PageSize="3" 
                                    AutoGenerateColumns="False" onpageindexchanging="GV_Project_PageIndexChanging" 
                                    onrowcancelingedit="GV_Project_RowCancelingEdit1" 
                                    onrowdeleting="GV_Project_RowDeleting" onrowediting="GV_Project_RowEditing" 
                                    onrowupdating="GV_Project_RowUpdating" 
                                    onrowdatabound="GV_Project_RowDataBound" >
                                    <RowStyle BackColor="#EFF3FB" />
                                    <Columns>
                                        <asp:TemplateField HeaderText="序号">
                                        <ItemTemplate></ItemTemplate>
                                        </asp:TemplateField>
                                         <asp:TemplateField Visible="false">
                                         <EditItemTemplate>
                                         <asp:DropDownList ID="DDL_7" runat="server" AutoPostBack="true" DataSource="<%#_listSchool %>" Visible="true">
                                            </asp:DropDownList>
                                         </EditItemTemplate>                                      
                                                                           
                                            
                                          </asp:TemplateField>
                                           <asp:TemplateField Visible="false">
                                             <ItemTemplate>                                            
                                                <asp:DropDownList ID="DDL_6" runat="server" AutoPostBack="true" DataSource="<%#_listMajor %>" Visible="false">
                                                </asp:DropDownList>
                                                </ItemTemplate>
                                          </asp:TemplateField>
                                          <asp:TemplateField Visible="false">
                                             <ItemTemplate>                                            
                                                <asp:DropDownList ID="DDL_5" runat="server" AutoPostBack="true" DataSource="<%#_listProjectType %>" Visible="false">
                                                </asp:DropDownList>
                                                </ItemTemplate>
                                          </asp:TemplateField>
                                          <asp:TemplateField Visible="false">
                                                <ItemTemplate>                                            
                                                <asp:DropDownList ID="DDL_4" runat="server" AutoPostBack="true" DataSource="<%#_listPostgraduate %>" Visible="false">
                                                </asp:DropDownList>
                                                </ItemTemplate>
                                          </asp:TemplateField>
                                          <asp:TemplateField Visible="false">
                                             <EditItemTemplate>                                     
                                                <asp:DropDownList ID="DDL_3" runat="server" AutoPostBack="true" DataSource="<%#_listYear %>" Visible="true">
                                                </asp:DropDownList>
                                             </EditItemTemplate>  
                                                
                                          </asp:TemplateField>
                                        <asp:CommandField DeleteText="&lt;div id=&quot;delete&quot; onclick=&quot;JavaScript:return confirm('删除此信息的同时,其他相关的信息也将一并删除,确定删除此信息?')&quot;&gt;删除&lt;/div&gt;" 
                                            HeaderText="删除" ShowDeleteButton="True" />
                                        <asp:CommandField EditText="编辑" HeaderText="编辑" ShowEditButton="True" 
                                            CancelText="取消" UpdateText="更新" />
                                    </Columns>
                                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                    <EditRowStyle BackColor="#2461BF" />
                                    <AlternatingRowStyle BackColor="White" />
                                </asp:GridView>

    后台cs中:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DDL_ProjectTable.DataSource = dataManageCommonL.programmanageL.GetProjectTableNameL();
                DDL_ProjectTable.DataTextField = "ProjectTableName";
                DDL_ProjectTable.DataBind();
                GVDataBind();
            }
           
        }
       
        private void GVDataBind()
        {
            if (DDL_ProjectTable.SelectedItem != null&&!String.IsNullOrEmpty(DDL_ProjectTable.SelectedItem.Text.Trim()))
            {
                _dtProject = dataManageCommonL.GetAllDataL(DDL_ProjectTable.SelectedItem.Text.Trim());
                if (_dtProject.Rows.Count.Equals(0))
                {
                    divNoData.Style["display"] = "block";
                    GV_Project.DataSource = _dtProject;
                    GV_Project.DataBind();
                }
                else
                {
                    divNoData.Style["display"] = "none";
                    //清空原有列                
                    int count = GV_Project.Columns.Count;//gridview的总列数
                    for (int i = 1; i < count - 7; i++)
                    {
                        GV_Project.Columns.RemoveAt(1);
                    }
                    //清空_dicColumn中的值
                    _dicColumn.Clear();
                    ////设置gridview的显示列
                    for (int i = 0; i < _dtProject.Columns.Count; i++)
                    {
                        string columnName = _dtProject.Columns[i].ColumnName;
                        if (columnName.Equals("id"))
                            continue;
                        switch (columnName)
                        {
                            case "id"://id列不显示 
                                break;
                            case "学校":
                            case "承办单位":
                                
                                GV_Project.Columns[GV_Project.Columns.Count - 7].HeaderText = columnName;
                                GV_Project.Columns[GV_Project.Columns.Count - 7].Visible = true;
                                _dicColumn.Add(columnName, 7);
                                break;
                            case "年份":
                            case "时间":
                                GV_Project.Columns[GV_Project.Columns.Count - 3].HeaderText = columnName;
                                GV_Project.Columns[GV_Project.Columns.Count - 3].Visible = true;
                                _dicColumn.Add(columnName, 3);
                                break;
                            case "专业":
                               
                                GV_Project.Columns[GV_Project.Columns.Count - 6].HeaderText = columnName;
                                GV_Project.Columns[GV_Project.Columns.Count - 6].Visible = true;
                                _dicColumn.Add(columnName, 6);
                                break;
                            case "项目类型":
                                GV_Project.Columns[GV_Project.Columns.Count - 5].HeaderText = columnName;
                                GV_Project.Columns[GV_Project.Columns.Count - 5].Visible = true;
                                _dicColumn.Add(columnName, 5);
                                break;
                            case "研究生层次":
                                //_listPostgraduate = dataManageCommonL.GetAllDataL("TPostgraduate", "PostgraduateName");
                                GV_Project.Columns[GV_Project.Columns.Count - 4].HeaderText = columnName;
                                GV_Project.Columns[GV_Project.Columns.Count - 4].Visible = true;
                                _dicColumn.Add(columnName, 4);
                                break;
                            default:
                                //是数据列的时候
                                BoundField colum = new BoundField();
                                colum.DataField = _dtProject.Columns[i].ColumnName;
                                colum.HeaderText = _dtProject.Columns[i].Caption;
                               
                                GV_Project.Columns.Insert(i, colum);
                                break;
                        }
                    }
                    GV_Project.DataSource = _dtProject;
                    GV_Project.DataKeyNames = new string[] { "id"};
                    GV_Project.DataBind();
                }
            }
            else
            {
                divNoData.Style["display"] = "block";
            }
        }

    由于其中的某些列(BoundField )是我动态生成的,所以导致当页面第一次加载时,我调试中可以找到模板列中的DDL_7等dropdownlist控件,但当我点击编辑时,此时页面重载,就无法找到DDL_7等控件了,有什么方法可以解决的?

    2012年5月15日 5:36

答案

全部回复