访问我的Datalists ASP C#

时间:2017-03-10 15:11:26

标签: c# asp.net

我想知道是否有办法访问位于我的asp代码VIA c#代码中的m1List和m2List数据列表,而不使用m1List。(property)或m2List。(property)。我尝试使用新的DataList(),但它似乎删除了我创建的数据列表并创建了一个新的数据列表。

任何人都可以指出我正确的方向吗?

这是我的asp代码

<td>
    <div id="m1" runat="server" class="day">
    <a href="?sday=m1" class="btn btn-default btn-sm"> 1 </a>
    <div class="smalldiv"></div>
        <asp:DataList ID="m1List" runat="server" DataKeyField="Id">
            <ItemTemplate>
                <div class="full-container"> 
                    <label class="col-sm-2 "><%# Eval("time") %></label>
                    <label class="col-sm-6 uk-text-truncate"><%# Eval("program") %></label> 
                    <label class="col-sm-4 uk-text-truncate"><%# Eval("channel") %></label> 
                </div>
            </ItemTemplate>
        </asp:DataList>
        <center>
            <a data-toggle="modal" data-target="#myModal">See More</a>
        </center>
    </div>
</td>
<td>
    <div id="m2" runat="server" class="day">
        <a href="?sday=m2" class="btn btn-default btn-sm"> 2 </a>
        <div class="smalldiv"></div>
        <asp:DataList ID="m2List" runat="server" DataKeyField="Id">
            <ItemTemplate>
                <div class="full-container"> 
                    <label class="col-sm-2 "><%# Eval("time") %></label>
                    <label class="col-sm-6 uk-text-truncate"><%# Eval("program") %></label> 
                    <label class="col-sm-4 uk-text-truncate"><%# Eval("channel") %></label> 
                </div>
            </ItemTemplate>
        </asp:DataList>
        <center>
            <a data-toggle="modal" data-target="#myModal">See More</a>
        </center>
    </div>
</td>

这是我的c#代码

bind.datasource = new SqlDataSource();
bind.datasource.ID = "SqlDataSource1";
this.Page.Controls.Add(bind.datasource);
bind.datasource.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["tvcomponentsStringConnection"].ConnectionString;
bind.datasource.SelectCommand = "SELECT * FROM [thisMonth] WHERE [Day] = 'm1'";

bind.conn = bind.datasource.ConnectionString;
bind.con = new SqlConnection(bind.conn);
bind.con.Open();

bind.str = bind.datasource.SelectCommand;
bind.cmd = new SqlCommand(bind.str, bind.con);
bind.da = new SqlDataAdapter(bind.cmd);
bind.ds = new DataSet();

bind.da.Fill(bind.ds);

bind.mlist = new DataList();
bind.mlist.ID = "m1List";
this.Page.Controls.Add(bind.mlist);
bind.mlist.DataSource = bind.ds;
bind.mlist.DataBind();
//m1List.DataSource = bind.ds;
//m1List.DataBind();
bind.pds.DataSource = bind.ds.Tables[0].DefaultView;
bind.pds.AllowPaging = true;
bind.pds.PageSize = 5;
//m1List.DataSource = bind.pds;
//m1List.DataBind();
bind.mlist.DataSource = bind.pds;
bind.mlist.DataBind();


bind.con.Close();

bind.mlist.AlternatingItemTemplate = Page.LoadTemplate("DynamicData/FieldTemplates/templateData.ascx");

1 个答案:

答案 0 :(得分:0)

无论如何,我似乎找到了我的问题的答案。我只需要在div中添加一个控件,我希望我的datalist出现。 我的代码现在看起来像这样

C#

try
            {
                for(int i = 0; i < 5; i++)
                {
                    bind.datasource = new SqlDataSource();
                    bind.datasource.ID = "SqlDataSource"+(i+1)+"";
                    this.Page.Controls.Add(bind.datasource);
                    bind.datasource.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["tvcomponentsStringConnection"].ConnectionString;
                    bind.datasource.SelectCommand = "SELECT * FROM [thisMonth] WHERE [Day] = 'm" + (i + 1) + "'";

                    bind.conn = bind.datasource.ConnectionString;
                    bind.con = new SqlConnection(bind.conn);
                    bind.con.Open();

                    bind.str = bind.datasource.SelectCommand;
                    bind.cmd = new SqlCommand(bind.str, bind.con);
                    bind.da = new SqlDataAdapter(bind.cmd);
                    bind.ds = new DataSet();

                    bind.da.Fill(bind.ds);



                    bind.mlist = new DataList();
                    bind.mlist.ID = "m" + (i + 1) + "List";

                    bind.mlist.ItemTemplate = Page.LoadTemplate("DynamicData/FieldTemplates/templateData.ascx");
                    bind.mlist.AlternatingItemTemplate = Page.LoadTemplate("DynamicData/FieldTemplates/templateData.ascx");

                    this.Page.Controls.Add(bind.mlist);
                    bind.mlist.DataSource = bind.ds;


                    bind.mlist.DataBind();
                    bind.pds = new PagedDataSource();
                    bind.pds.DataSource = bind.ds.Tables[0].DefaultView;
                    bind.pds.AllowPaging = true;
                    bind.pds.PageSize = 5;
                    bind.mlist.DataSource = bind.pds;
                    bind.mlist.DataBind();

                    bind.con.Close();

                    int switche = i + 1;

                    switch(switche)
                    {
                        case 1:
                            m1.Controls.Add(bind.mlist);
                            break;
                        case 2:
                            m2.Controls.Add(bind.mlist);
                            break;
                        case 3:
                            m3.Controls.Add(bind.mlist);
                            break;
                        case 4:
                            m4.Controls.Add(bind.mlist);
                            break;
                        case 5:
                            m5.Controls.Add(bind.mlist);
                            break;
                        default:
                            break;
                    }

                }

                //m1.InnerHtml = "<center><a data-toggle='modal' data-target='#myModal'>See More</a></center>";


            }
            catch (Exception ex)
            {
                //Response.
            }