如何动态地将标签和按钮添加到组框?

时间:2015-01-05 18:36:06

标签: c# mysql visual-studio-2013

我正在构建一个程序,用于在我的数据库中插入/更新/删除条目。

待办事项列表的下一步是根据在数据库中找到的条目数动态填充带有标签和按钮的组框,但我不确定如何执行此操作。

我的查询看起来像"SELECT id, image FROM images WHERE slider = 1";

数据库中所有带滑块= 1的条目在网站上的图像滑块中显示图像,而滑块= 0则不显示它。

所以在程序中我想显示图像的标签和一个按钮,从我在设计视图中设置的组框中的滑块中删除图像。 (当您单击删除按钮时,它将更新数据库并根据ID将列滑块设置为0)。

E.G:

Groupbox

image1 <delete> <--button
image2 <delete> <--button

修改

我忘了提到当我点击顶部菜单中的工具条时会出现人口

E.G:

private void imagesToolStripMenuItem_Click(object sender, EventArgs e)
{
    Pnl_Media.Visible = true;

    Lbl_Welcome_admin.Visible = false;
    Lbl_Text_Admin.Visible = false;

    //Populate the groupbox here
}

/ EDIT

所以我的问题是:如何使用上面的代码块填充现有的空组框?

谢谢

吉姆

3 个答案:

答案 0 :(得分:1)

如果我理解正确,您想在代码中创建标签和按钮,并在按钮上附加OnClick

你可以这样做。

如果这不是您想要的,请澄清您的意思。

您可以像这样创建标签和按钮等对象:

        Label lbl = new Label();
        lbl.Name = "label1";
        lbl.Parent = groupBox1;
        lbl.Text = "hello world";
        lbl.SetBounds(10, 10, 75, 21);

        Button btn = new Button();
        btn.Name = "button1";
        btn.Parent = groupBox1;
        btn.Text = "delete something";
        btn.SetBounds(10, 50, 75, 21);
        btn.Click += btn_Click;

    void btn_Click(object sender, EventArgs e)
    {
        if (sender is Button)
        {
            MessageBox.Show("clicked on " + ((Button)sender).Name);
        }
    }

答案 1 :(得分:0)

&#13;
&#13;
<script type="text/javascript" language="javascript">
        function removeImage(id) {
            PageMethods.RemoveImage(id, onSucess, onError);
        }
        function onSucess(result) {
            alert(result);
            document.getElementById("btnClick").click();
        }

        function onError(result) {
            alert('Something wrong.');
        }
    </script>
&#13;
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DynamicData_JIM._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" language="javascript">
        function removeImage(id) {
            PageMethods.RemoveImage(id, onSucess, onError);
        }
        function onSucess(result) {
            alert(result);
            document.getElementById("btnClick").click();
        }

        function onError(result) {
            alert('Something wrong.');
        }
    </script>

    

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
    <div>
        <asp:Button ID="btnClick" Text="Click" runat="server" OnClick="btnClick_Click" />
        <asp:Panel ID="pnldynamic" runat="server">
        </asp:Panel>
    </div>
    </form>
</body>
</html>
&#13;
&#13;
&#13;

    protected void btnClick_Click(object sender, EventArgs e)
    {
        try
        {
            using (DBM.SqlCommandEx sqlCmd = new DBM.SqlCommandEx(String.Format("SELECT  [id],[image] FROM images WHERE slider = 1", "", "")))
            {
                System.Data.DataTable dt = new System.Data.DataTable();
                dt = sqlCmd.GetDataTable();


                Int32 icount = 0;
                pnldynamic.Controls.Clear();
                pnldynamic.Controls.Add(new LiteralControl("<table>"));
                foreach (System.Data.DataRow dr in dt.Rows)
                {

                    String LabelId = "lbl" + icount.ToString();
                    String buttonid = dr["id"].ToString();

                    pnldynamic.Controls.Add(new LiteralControl("<tr><td>"));

                    pnldynamic.Controls.Add(new LiteralControl("<span>" + dr["image"].ToString() + "</span>"));
                    pnldynamic.Controls.Add(new LiteralControl("</td><td>"));

                    pnldynamic.Controls.Add(new LiteralControl("<input type='button' id=" + buttonid + " value='Remove' onclick='removeImage(" + dr["id"].ToString() + ");' />"));


                    pnldynamic.Controls.Add(new LiteralControl("</td></tr>"));


                    icount++;
                }
                pnldynamic.Controls.Add(new LiteralControl("</table>"));

            }
        }
        catch (Exception ex)
        {
            string err = ex.ToString();
        }


    }


    [WebMethod]
    public static string RemoveImage(string id)
    {
        String msg = "Success";
        Int32 iresult = 0;
        try
        {
            iresult = DBM.ExecuteNonQuery("UPDATE   images SET  slider = 0 WHERE id = " + id + "");
        }
        catch (Exception ex)
        {
            string err = ex.ToString();
        }
        return msg;
    }

}

答案 2 :(得分:0)

DBM.cs

private static string GetConnectionString()
{
    return ConfigurationManager.ConnectionStrings["StackOverflow"].ConnectionString;
}

public static string CleanSQL(string data)
{
    return data.Replace("'", "''");
}

public static int ExecuteNonQuery(string SQL)
{
    using (SqlConnection conn = new SqlConnection(GetConnectionString()))
    {
        conn.Open();

        SqlCommand cmd = new SqlCommand(SQL, conn);
        int result = cmd.ExecuteNonQuery();

        conn.Close();
        return result;
    }
}
public static object ExecuteScalar(string SQL)
{
    using (SqlConnection conn = new SqlConnection(GetConnectionString()))
    {
        conn.Open();

        SqlCommand cmd = new SqlCommand(SQL, conn);
        object result = cmd.ExecuteScalar();

        conn.Close();
        return result;
    }
}

public class SqlCommandEx : IDisposable
{
    private SqlConnection conn;
    public SqlCommand CommandObject;

    //public static System.Collections.Stack inuse = new System.Collections.Stack();

    public SqlCommandEx(string SQL)
    {
        conn = new SqlConnection(GetConnectionString());
        conn.Open();

        CommandObject = new SqlCommand(SQL, conn);
        //inuse.Push(DateTime.Now);
    }

    public DataTable GetDataTable()
    {
        SqlDataAdapter da = new SqlDataAdapter(CommandObject);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

    public void AddWithValue(string parameter, object value)
    {
        CommandObject.Parameters.AddWithValue(parameter, value);
    }

    public void Dispose()
    {
        //inuse.Pop();
        conn.Close();
    }
}

public static DataTable GetDataTable(string SQL)
{
    using (SqlConnection conn = new SqlConnection(GetConnectionString()))
    {
        conn.Open();

        SqlDataAdapter da = new SqlDataAdapter(SQL, conn);
        DataTable result = new DataTable();
        da.Fill(result);

        conn.Close();
        return result;
    }
}