动态加载Gridbehind Gridview

时间:2014-07-09 19:24:37

标签: c# gridview

我使用linq到sql从sql动态生成这个标签(IT,Marketing ....)。

当您点击任何标签时,它会根据在数据库中创建的表格显示 gridview

但是gridview绑定是在page_load事件期间完成的,因此在page_load期间生成所有内容,现在当您单击任何蓝色选项卡时,它将显示预先生成的网格视图。

现在我想生成或加载网格视图,当我克隆任何选项卡的[+]符号而不是页面加载时

 <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
     <div id="CONTAINER" onclick="">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>

             <div class="clickable mfiles" onclick="showHide('subm');changesign('signm');">
                    <span id="signm" class="plusMinus">[+]</span><span> M-Files<br />
                    </span>
                </div>

                <div id="subm">


                    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

                </div>

            </ContentTemplate>
    </asp:UpdatePanel>





         </div>
    <br />

    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>

<script>


    function showHide(id) {
        __doPostBack('UpdatePanel1', id);
        var el = document.getElementById(id);
        //if (el && el.style.display == 'block') {
        //    el.style.display = 'none';
        //}

        //else {
        //    el.style.display = 'block';
        //}


    }

    function changesign(id1) {
        var xy = document.getElementById(id1);
        if (xy.innerHTML == "[-]") {
            xy.innerHTML = "[+]";
        }

        else {
            xy.innerHTML = "[-]";
        }
    }

</script>

banckend代码:

protected void Page_Load(object sender, EventArgs e)
        {
CONTACT_INFODataContext context = new CONTACT_INFODataContext("Data Source=BPM-IT116;Initial Catalog=FORM_GET;Persist Security Info=True;User ID=spreader;Password=Red_Sky");
                var depts = context.spi_GetNoOfDept();
                PlaceHolder1.Controls.Clear();

                int i = 1;
                foreach (spi_GetNoOfDeptResult dept in depts)
                {
                    Literal div = new Literal();
                    Contact deptinfo = new Contact();
                    deptinfo.NAME_LAST = dept.DEPT_NAME;
                    deptinfo.DEPT_ID = dept.DEPT_ID.ToString();



                    div.Text = "<div class=\"even clickable\" onclick=\"showHide('sub" + dept.DEPT_ID + "');changesign('sign" + dept.DEPT_ID + "');\">";
                    div.Text += "<span id=\"sign" + dept.DEPT_ID + "\">[+]</span><span>" + dept.DEPT_NAME + "</span>";
                    div.Text += "</div>";


                    div.Text += "<div id=\"sub" + dept.DEPT_ID + "\" style=\"margin-left:15px ;\">";



                    //GridView gd = CreateDynamicTable(dept.DEPT_ID);

                    i++;

                    PlaceHolder1.Controls.Add(div);

                    PlaceHolder1.Controls.Add(new LiteralControl("</div>"));
        }

   private GridView CreateDynamicTable(int x)
        {
            GridView gd = new GridView();

            gd.ID = "grd" + x;
            gd.AlternatingRowStyle.CssClass = "altrowstyle1";
            gd.RowStyle.CssClass = "rowstyle1";
            gd.HeaderStyle.CssClass = "grdhdr";
            gd.GridLines = GridLines.None;
            List<Contact> contacts = new List<Contact>();


            CONTACT_INFODataContext context = new CONTACT_INFODataContext("Data Source=BPM-IT116;Initial Catalog=FORM_GET;Persist Security Info=True;User ID=spreader;Password=Red_Sky");
            var persons = context.spi_GetContacts();
            var items = context.spi_GetDept(x);
            var depts = context.spi_GetNoOfDept();

            foreach (spi_GetDeptResult item in items)
            {

                Contact contact = new Contact();
                contact.NAME_LAST = item.NAME_LAST;
                contact.NAME_FIRST = item.NAME_FIRST;
                contact.PHONE_CELL = item.PHONE_CELL;
                contact.ADDRESS = item.ADDRESS;
                contact.APT = item.APT;
                contact.DEPT_ID = item.DEPT_ID.ToString();
                contact.DEPT_NAME = item.DEPT_NAME;

                contacts.Add(contact);
                //ddl_db.Items.Add(new ListItem(person.NAME_FIRST));
            }
            gd.CssClass = "gdmain";


            gd.DataSource = contacts;
            gd.DataBind();

            return gd;

        }

1 个答案:

答案 0 :(得分:0)

如果您对标签容器使用Multiview控件,那么您应该能够绑定代码以将Gridview加载到ActiveViewChanged事件,并在用户在标签之间导航时更改该Multiview的ActiveViewIndex属性。