如何在按钮单击时在表格单元格中插入标签文本 [英] How can I insert label text in tablecell on button click

查看:118
本文介绍了如何在按钮单击时在表格单元格中插入标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,我根据用户输入动态生成。我试图在按钮单击时将标签文本插入表格单元格。按下按钮,应使用javascript / ajax将演示方法的标签文本插入到表格单元格中。有人可以建议我怎么做?



我尝试过:



I have a table which i am dynamically generating based on user input. I am trying to insert a label text into table cell on button click. onclick of button a label text of a demo method should be inserted into table cells using javascript/ajax. Can someone suggest me how can i do this?

What I have tried:

<pre>function CreateTable(){
    var rowCtr;
    var cellCtr;
    var rowCnt;
    var cellCnt;

    var myTableDiv = document.getElementById('myDynamicTable');

    var table = document.createElement('table');
    table.setAttribute("contenteditable", "true");
    table.border = "1";
    table.id = "myTable";


    var tableBody = document.createElement('tbody');
    table.appendChild(tableBody);

    rowCnt = document.getElementById('txtrows').value;
    cellCnt = document.getElementById('txtcols').value;

    for (rowCtr = 0; rowCtr < rowCnt; rowCtr++) {

        var tr = document.createElement('tr');
        tableBody.appendChild(tr);

        for (cellCtr = 0; cellCtr < cellCnt; cellCtr++) {

            var td = document.createElement('td');
            td.width = '120';
            td.appendChild(document.createTextNode("Row:" + rowCtr + " Column:" + cellCtr));
            tr.appendChild(td);
        }
    }
    myTableDiv.appendChild(table);
  }



asp按钮:


asp button:

<asp:Button ID="button1" Text="button" runat="server" OnClick="demo" />





C#代码:





C# Code:

public void demo()
{
    TableCell tcell = new TableCell();
    RadLabel rlbl = new RadLabel();
    rlbl.Text = "test";
    tcell.Controls.Add(rlbl);

}

protected void demo(object sender, EventArgs e)
{
    demo();
}

推荐答案

您正在动态创建 HTML 客户端表(JavaScript),您试图在 TableCell 上插入一个 Label 控件,赢得了'因为:



(1)您的C#代码不知道您创建的生成的HTML表 - 它们是在客户端创建的,它们之间存在断开的空间。

(2)您的动态HTML将在每次回发时丢失,因为您的服务器不知道动态控件的状态





您不应该将JavaScript实现与逻辑背后的C#代码混合在一起。如果您使用JavaScript生成表,则使用JavaScript插入标签元素。
You are dynamically creating your HTML table at the client (JavaScript) and you are trying to insert a Label control on a TableCell which won't work because:

(1) Your C# code doesn't know the generated HTML Table you created - they are created on the client and there's a disconnected space between them.
(2) Your dynamic HTML will be lost on each postbacks as your server doesn't know the state your dynamic controls


You shouldn't mix your JavaScript implementation with your C# code behind logic. If you are generating your Table using JavaScript, then use JavaScript to insert a label element.


这篇关于如何在按钮单击时在表格单元格中插入标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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