动态添加ASP.Net控件 [英] Adding ASP.Net Controls Dynamically

查看:30
本文介绍了动态添加ASP.Net控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,该过程根据数据库中存储的记录数返回许多行,现在,我想拥有一种使用包含以下内容的控件创建< div> 标签的方法该行中的值,如果从数据库中返回了10行,则必须创建10个< div> 标记,我有下面的代码可以从中获取结果数据库,但我不知道如何从这里继续.

I have a stored procedure that returns a number of rows based on the number of records stored in the database, now I want to have a way of creating a <div> tag with controls containing values from that row, if there are 10 rows returned from the database, then 10 <div> tags must be created, I have the code below to fetch the results from the database but I don't know how to continue from here.

            String sql = "exec dbo.spLoadCandidates @NationalID, @PostID";
            SqlDataReader reader;
            using (SqlCommand cmd = new SqlCommand(sql, conn)) 
            {
                cmd.Parameters.AddWithValue("@NationalID",TextBox2.Text.Trim());
                cmd.Parameters.AddWithValue("@PostID,", DropDownList1.SelectedValue);
                conn.Open();
                reader = cmd.ExecuteReader();
                while (reader.HasRows)
                {
                    //Dynamic Data here 
                }
                conn.Close();
            }      

更新:我正在使用网络表单执行此操作

update: Am using web forms to do this

推荐答案

您有几种选择.

替代1 .在您的aspx中添加一个 PlaceHolder ,并在其中填充所需的内容.

Alternative 1. Add a PlaceHolder to your aspx, and fill it with the contents you want.

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

然后,您可以从后面的代码中向此 PlaceHolder 添加内容.

Then you can add contents to this PlaceHolder from code behind.

String sql = "exec dbo.spLoadCandidates @NationalID, @PostID";
SqlDataReader reader;
using (SqlCommand cmd = new SqlCommand(sql, conn)) 
{
    cmd.Parameters.AddWithValue("@NationalID",TextBox2.Text.Trim());
    cmd.Parameters.AddWithValue("@PostID,", DropDownList1.SelectedValue);
    conn.Open();
    reader = cmd.ExecuteReader();
    while (reader.HasRows)
    {
         // Create div.
         var div = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");      
        createDiv.InnerHtml = "" // Fill with your content...
        // Add to placeholder.
        PlaceHolder1.Add(createDiv);
    }
    conn.Close();
}      

替代2 .对您的aspx使用 Repeater .

Alternative 2. Use a Repeater to your aspx.

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <div><%# Eval("Name") %></div>
    </ItemTemplate>
</asp:Repeater>

以及您后面的代码:

// Some class to keep your info...
public class MyModel
{
    public string Name { get; set; }
}

以及您的代码:

String sql = "exec dbo.spLoadCandidates @NationalID, @PostID";
SqlDataReader reader;
using (SqlCommand cmd = new SqlCommand(sql, conn)) 
{
    cmd.Parameters.AddWithValue("@NationalID",TextBox2.Text.Trim());
    cmd.Parameters.AddWithValue("@PostID,", DropDownList1.SelectedValue);
    conn.Open();
    reader = cmd.ExecuteReader();
    var list = new List<MyModel>();
    while (reader.HasRows)
    {
        var model = new MyModel();
        model.Name = "foo"; // Fill with your content...
        list.Add(model);
    }
    conn.Close();

    // Bind to repeater.
    Repeater1.DataSource = list;
    Repeater1.DataBind();
}      

这篇关于动态添加ASP.Net控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆