为什么HTML表中的行不能从后面的代码读取(vb.net)? [英] Why can the row in a HTML table not be read from code behind (vb.net)?

查看:135
本文介绍了为什么HTML表中的行不能从后面的代码读取(vb.net)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML表,并通过 onclick 事件的一个按钮插入一行。

I have an HTML table and insert a row using Javascript via the onclick event of a button.

这是我的代码。

HTML:

<!-- language: lang-html -->

<table id="PopTable" class="imagetable" runat = "server" align="center">
    <tr>
        <th></th>
        <th>ShipTo</th>
        <th>CustName</th>
        <th>Alamat</th>
        <th>Dari</th>
        <th>Ke</th>
    </tr>
</table>

Javascript:

Javascript:

function AddRow(Shipid,custname, grup, valid, alamat)
{
    var temp11;
    valid = "YES";
    for (var i = 0; i < arrloc.length; i++)
    {
        temp11 = arrloc[i];
        if (Shipid == temp11){ valid = "No" };
    }
    if (valid == "YES"){
        var table = document.getElementById("PopTable");
        var row = table.insertRow(1);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        var cell4 = row.insertCell(3);
        var cell5 = row.insertCell(4);
        var cell6 = row.insertCell(5);
        if (grup == "")
        {
            var element2 = document.createElement("select");
            var option1 = document.createElement("option");
            option1.innerHTML = "DK";
            option1.value = "1";
            element2.add(option1, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LK";
            option2.value = "2";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LJ";
            option2.value = "3";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LD";
            option2.value = "4";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LP";
            option2.value = "5";
            element2.add(option2, null); }
        else if (grup == "DK")
        {
            var element2 = document.createElement("select");
            var option1 = document.createElement("option");
            option1.innerHTML = "LK";
            option1.value = "1";
            element2.add(option1, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LJ";
            option2.value = "2";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LD";
            option2.value = "3";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LP";
            option2.value = "4";
            element2.add(option2, null);}
        else if (grup == "LK")
        {
            var element2 = document.createElement("select");
            var option1 = document.createElement("option");
            option1.innerHTML = "DK";
            option1.value = "1";
            element2.add(option1, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LJ";
            option2.value = "2";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LD";
            option2.value = "3";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LP";
            option2.value = "4";
            element2.add(option2, null);}
        else if (grup == "LJ")
        {
            var element2 = document.createElement("select");
            var option1 = document.createElement("option");
            option1.innerHTML = "DK";
            option1.value = "1";
            element2.add(option1, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LK";
            option2.value = "2";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LD";
            option2.value = "3";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LP";
            option2.value = "4";
            element2.add(option2, null);}
        else if (grup == "LD")
        {
            var element2 = document.createElement("select");
            var option1 = document.createElement("option");
            option1.innerHTML = "DK";
            option1.value = "1";
            element2.add(option1, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LJ";
            option2.value = "2";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LK";
            option2.value = "3";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LP";
            option2.value = "4";
            element2.add(option2, null);}
        else if (grup == "LP")
        {
            var element2 = document.createElement("select");
            var option1 = document.createElement("option");
            option1.innerHTML = "DK";
            option1.value = "1";
            element2.add(option1, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LJ";
            option2.value = "2";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LD";
            option2.value = "3";
            element2.add(option2, null);
            var option2 = document.createElement("option");
            option2.innerHTML = "LK";
            option2.value = "4";
            element2.add(option2, null);}


        var element3 = document.createElement("input");
        element3.type = "checkbox";

        cell5.innerHTML = grup;
        cell4.innerHTML = alamat;
        cell6.appendChild(element2);
        cell2.innerHTML = Shipid;
        cell3.innerHTML = custname;
        cell1.appendChild(element3);

        arrloc.push(Shipid);}
    }

在代码隐藏中,我尝试获取表的内容:

In the code-behind I try to get the contents of the table:

For i = 0 To PopTable.Rows.Count

    shipid = PopTable.Rows(1).Cells(1).InnerText
    'shipid = PopTable.Rows(1).Cells(1).innerhtml -- i also try this

    stringsql = "insert into cs_graph_ubah_dklk_log (shipid, dklk_awal, dklk_ubah, tgl_ubah, user_name, flag_proses, mid ) values ('" & shipid & "', '" & dklk_awal & "','" & dklk_ubah & "', sysdate , '" & user & "', 1, cs_graph_ubah_dklk_seq.nextval ) "
    dtsql = SPKOraConnGetDataWS.CreateDataSet(stringsql)

Next

我尝试通过Javascript插入HTML表格行,但在VB.NET中,结果是 rows.count 只有1行,行只是标题。为什么会发生这种情况?在客户端,Javascript成功地向表中添加行。

I try to insert the HTML table rows via Javascript, but in VB.NET the result is that rows.count only has 1 row, and the row is only the header. Why is this happening? On the client-side, Javascript successfully adds rows to the table.

为什么代码隐藏只读标题行?缺少什么?

Why does the code-behind only read the header row? What is missing?

请询问您是否需要更多信息。

Please ask if you need more information from me.

推荐答案

主要原因可以在Amir Sherafatian的评论中被发布:

the main reason can be fured out by post in the comment by Amir Sherafatian:

当网站创建时,所有html都被渲染 - 具有在页执行期间,runat =server属性具有1:1对象生成的服务器端。

when the website is created, all html is rendered - the elements that have runat="server" attribute have 1:1 objects generated sever side during page execution.

请注意,当html写入客户端时,页面执行结束,除非您将某些数据存储在Cache / Session / ViewState / Cookie或任何其他持久存储器中关于呈现页面的数据在Web服务器上丢失。

Mind you, page execution ends when the html is written to the client and then unless you store some data in Cache /Session/ ViewState/ cookies or any other persistent storage all the data regarding the rendered page is lost on the web server.

当客户点击提交页面的状态从网页上的持久存储和输入中恢复(这基本上是一种形式,因此每个回发都会发送所有输入/选择/隐藏的字段回到服务器)想象一下,用户通过自己的java脚本使用控制台修改你的页面 - 它根本不能被接受到服务器。在回发时,html对web服务器没有任何意义。通信的唯一意义是通过从表单域映射的回发属性。

when client clicks submit the state of the page is recovered from those persistent storages and inputs that are on the webpage (which is basically a form hence every postback sends ALL inputs / selects / hidden fields back to the server) Imagine, that the user modifies your page via own java script using console - it simply cannot be accepted bu the server. the html means nothing to the web server on postbacks. The only mean of communication is via postback attributes which are mapped from form fields.

任何其他不输入字段/隐藏字段的对象都将从aspx / html(在您的服务器上)在开始执行页面的某个地方。

any other objects that are not input fields / hidden fields are rebuild from the aspx/html (on your server) somewhere along begining page execution.

您的代码后面只看到标题行,因为只有标题行在页面的状态被构建的表中定义。

Your code behind sees only header row because only header row is defined in the table from which the state of the page is built.

如果你想实现你的目标,你需要在runat = server中定义至少一个隐藏的字段,你可以在添加/删除数据时添加/删除数据/用JavaScript删除行,然后将其数据作为表单回发参数发送到服务器,您可以访问它,就像访问隐藏字段一样(如我所解释的,隐藏字段值将从回发参数重建),请阅读数据,解析并做任何你想要的。

if you want to achieve your goal, you need to define at least one hidden field with runat=server" to which you'd add / remove data as you add / remove rows with JavaScript. Its data will then be sent as a form postback argument to the server. There you can access it just like accessing a hidden field (as i explained the hidden field value will be rebuild from postback arguments), read the data, parse it and do whatever you want.

如果你想要一个示例代码(ASP.Net C#),请大声喊叫:)

If you want a sample code ( ASP.Net C#), please shout :)

这篇关于为什么HTML表中的行不能从后面的代码读取(vb.net)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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