猿问

ScriptManager.RegisterStartupScript 仅在 foreach

我有一个数据表,其中包含我从数据库中获得的记录。当页面加载时,我想浏览每条记录并将值提交给 JavaScript 函数,该函数将它们显示到页面上,但是,它仅适用于表中的第一条记录。如果有人有办法解决这个问题或有更好的方法来做我想做的事情,我们将不胜感激。


我背后的代码:


foreach(DataRow row in tlds.Rows) {

    TLDid = Convert.ToInt32(row["tldID"]);

    tldName = row["tldName"].ToString();

    date = row["releaseDate"].ToString();

    price = Convert.ToDouble(row["price"]);

    tags = row["tags"].ToString();

    ScriptManager.RegisterStartupScript(this, this.GetType(), "tld", "addTLD('" + TLDid + "','" + tldName + "','" + date + "','" + price + "')", true);

我的js函数:


function addTLD(tldID, tldName, releaseDate, price) {

    $('<tr><td>' + tldName + '</td><td>' + releaseDate + '</td><td>' + price + '</tr>').insertAfter('tr');

}


慕森卡
浏览 122回答 1
1回答

红颜莎娜

您可以使用 ASP.NET 服务器端表格控件,然后动态添加行并为每一行添加所需的单元格,如下面的代码ASPX<asp:Table ID="myTable" runat="server" Width="100%">&nbsp;</asp:Table>ASPX.CS&nbsp; &nbsp; foreach (DataRow row in tlds.Rows)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date = row["releaseDate"].ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; price = Convert.ToDouble(row["price"]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tags = row["tags"].ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TableRow row = new TableRow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TableCell TLIdCell= new TableCell();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TLIdCell.Text = Convert.ToInt32(row["tldID"]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;row.Cells.Add(TLIdCell);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TableCell tldNameCell = new TableCell();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tldNameCell .Text = Convert.ToString(row["tldName"]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row.Cells.Add(tldNameCell);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Similarly add code for date, price and tags cells&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Finally add row to table rows collection&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myTable.Rows.Add(row);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;
随时随地看视频慕课网APP
我要回答