如何动态地将标签和按钮添加到组框?
How do I dynamically add a label and button to a groupbox?
我正在构建一个程序,它将 insert/update/delete 我的数据库中的条目。
待办事项列表的下一步是根据在数据库中找到的条目数动态填充带有标签和按钮的组框,但我不确定如何执行此操作。
我的查询看起来像 "SELECT id, image FROM images WHERE slider = 1";
其中数据库中所有带有 slider = 1 的条目显示网站上图像滑块中的图像,而 slider = 0 不显示它。
所以在程序中,我想显示图像的标签和一个按钮,用于从我在设计视图中设置的组框中的滑块中删除图像。 (当您单击删除按钮时,它将更新数据库并根据 id 为该图像将列滑块设置为 0)。
例如:
Groupbox
image1 <delete> <--button
image2 <delete> <--button
编辑
我忘了说当我点击顶部菜单中的工具条时应该出现填充
例如:
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
}
/编辑
所以我的问题是:如何使用上面的代码块填充我现有的空组框?
谢谢
吉姆
如果我没理解错的话,您想在代码中创建标签和按钮,并在按钮上附加一个 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);
}
}
<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>
<%@ 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>
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;
}
}
DBM.cs
private static string GetConnectionString()
{
return ConfigurationManager.ConnectionStrings["Whosebug"].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;
}
}
我正在构建一个程序,它将 insert/update/delete 我的数据库中的条目。
待办事项列表的下一步是根据在数据库中找到的条目数动态填充带有标签和按钮的组框,但我不确定如何执行此操作。
我的查询看起来像 "SELECT id, image FROM images WHERE slider = 1";
其中数据库中所有带有 slider = 1 的条目显示网站上图像滑块中的图像,而 slider = 0 不显示它。
所以在程序中,我想显示图像的标签和一个按钮,用于从我在设计视图中设置的组框中的滑块中删除图像。 (当您单击删除按钮时,它将更新数据库并根据 id 为该图像将列滑块设置为 0)。
例如:
Groupbox
image1 <delete> <--button
image2 <delete> <--button
编辑
我忘了说当我点击顶部菜单中的工具条时应该出现填充
例如:
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
}
/编辑
所以我的问题是:如何使用上面的代码块填充我现有的空组框?
谢谢
吉姆
如果我没理解错的话,您想在代码中创建标签和按钮,并在按钮上附加一个 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);
}
}
<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>
<%@ 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>
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;
}
}
DBM.cs
private static string GetConnectionString()
{
return ConfigurationManager.ConnectionStrings["Whosebug"].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;
}
}