【问题标题】:load codebehind Gridview dynamically动态加载 Gridview 的代码隐藏
【发布时间】:2014-07-09 19:24:36
【问题描述】:

我正在使用 linq to sql 从 sql 动态(代码隐藏)生成此标签(IT、Marketing....)。

当您单击任何选项卡时,它将根据在数据库中创建的表显示 gridview

但是 gridview 绑定是在 page_load 事件期间完成的,所以一切都是在 page_load 期间生成的,现在当您单击任何蓝色选项卡时,它会显示预生成的 gridviews。

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

 <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>

后端代码:

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;

        }

【问题讨论】:

    标签: c# gridview


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2010-10-30
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 2011-08-26
      • 2014-08-06
      • 1970-01-01
      相关资源
      最近更新 更多