Literal加入動態按鈕

LAIJUNG
各位前輩好

小弟想要用Literal物件,然後跑迴圈後,結果希望在每個<a>標籤中都塞入動態產生的按鈕

在前端加入了Literal物件,在後端用動態方式產生按鈕,

把產生的按鈕加入到Literal中後,但是都會出現文字的字串System.Web.UI.WebControls.Button

要怎麼方法加入進去或是用什麼方法轉類型

以下我的cdoe,希望前輩有可以指點~

****前端****

<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
</div>

</form>
</body>





****後端****
[code]
protected void Page_Load(object sender, EventArgs e)
{
Button[] btn = new Button[3];
for (int i = 0; i < btn.Length; i++)
{
btn[i] = new Button();
btn[i].ID = i.ToString();
btn[i].Text = i.ToString();
btn[i].Height = 25;
btn[i].Width = 70;
btn[i].Click += new EventHandler(btn_Click);
string table = "";
table += "<a>" + btn[i] + "</a>";
Literal1.Text = table;
}
}


protected void btn_Click(object sender, EventArgs e)
{
Button temp = (Button)sender;

if (temp.Text == "0")
{
temp.Enabled = false;
}
Response.Write(temp.ID);

}
[/code]
MIS2000 Lab
依照您的構想,想要讓按鈕按下去,就直接超連結嗎?

我猜應該使用 LinkButton控制項才對吧?
香帥
因您已經宣告Button,就可以不再用literal
您的程式碼可以改成以下這樣請參考
<%@ Page Language="C#" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
{
Button[] btn = new Button[3];
for (int i = 0; i < btn.Length; i++)
{
btn[i] = new Button();
btn[i].ID = i.ToString();
btn[i].Text = i.ToString();
btn[i].Height = 25;
btn[i].Width = 70;
btn[i].Click += new EventHandler(btn_Click);
Panel1.Controls.Add(btn[i]);
}
}


protected void btn_Click(object sender, EventArgs e)
{
Button temp = (Button)sender;

if (temp.Text == "0")
{
temp.Enabled = false;
}
Response.Write(temp.ID);

}
</script>
 
<html>
<head runat="server"></head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Panel id="Panel1" runat="server"></asp:Panel>
</div>
</div>

</form>
</body>
</html>

測試網址在
http://ap.a168a.com/test/literal2.aspx
LAIJUNG
TO MIS2000 Lab, 香帥

我用圖片比較清楚,就是想要把BUTTON或LinkButton塞入,如下


另外~在圖片中的名稱都是從db撈出來,再請前輩指導~



****後端****
  protected void Page_Load(object sender, EventArgs e)
        {

            //多少文章數量
            int getsum = 0;
            String sqlgetpostsum = @"select count(infor)as countsum from testtable ";
            using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["test"].ConnectionString))
            {
                SqlCommand command = new SqlCommand(sqlgetpostsum, conn);

                conn.Open();
                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    //
                    getsum = int.Parse(reader["countsum"].ToString());
                }
            }

            //
            String table1 = "";
            String sqlgetpost = @"select * from testtable";

            using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["test"].ConnectionString))
            {
                SqlCommand command = new SqlCommand(sqlgetpost, conn);
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();

                //
                int ibt = 0;
                int ia = 1;
                int ib = 1;
                while (reader.Read())
                {


                    //名稱
                    Label newlabel1 = new Label();
                    newlabel1.Text = reader["infor"].ToString();
                    //
                    Button[] btn = new Button[getsum];
                    btn[ibt] = new Button();
                    btn[ibt].ID = ibt.ToString();
                    btn[ibt].Text = ibt.ToString();
                    btn[ibt].Height = 25;
                    btn[ibt].Width = 70;
                    btn[ibt].Click += new EventHandler(btn_Click);
                    Panel1.Controls.Add(btn[ibt]);

                    //
                    table1 += "<table border=\"1\" cellpadding=\"3\" cellspacing=\"1\" style=\"border: 3px solid #FFAE7F;border-collapse: collapse; width:400px;\"><tbody><tr><td width=\"50\">文章編號</td><td>名稱</td><td width=\"60\">作者</td></tr><tr><td>" + ia + "</td><td>" + newlabel1.Text + "</td><td>" + btn[ibt] + "</td></tr></tbody></table><br/>";
                    Literal1.Text = table1;

                    //
                    ia++;
                    ib++;

                }
            }
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            Button temp = (Button)sender;

            if (temp.Text == "0")
            {
                temp.Enabled = false;
            }
            Response.Write(temp.ID);

        }

LAIJUNG
TO MIS2000 Lab, 香帥

回應3樓,剛剛用圖片未顯示~所以再回覆一次

https://imgur.com/vOB2he4
Allen
1.
單就字面上的意思, 其實是做不到"在Literal控制項裡加入Button控制項", 因為Literal 無法容納子控制項, 不過,要放入html當然是可以的
2.
單就您貼的圖, 其實用 Repeater就可以產生了

最有價值解答

Allen
我錄了影片放在這裡

Vimeo : https://vimeo.com/428039758
Youtube : https://youtu.be/ag4_tKfcKcQ

facebook 粉絲團 : https://www.facebook.com/wa.allenkuo/
LAIJUNG
TO Allen 香帥

謝謝前輩~

我使用你們方法~

改用 placeHolder 或 panel

也解決了
回到頂部