【问题标题】:How to bind the data from a string in asp:grid?如何在 asp:grid 中绑定字符串中的数据?
【发布时间】:2012-09-06 07:13:12
【问题描述】:

我有以下字符串:

for(#some conditions){    
 ##
result += "<tr><td>" + dc + "</td><td>" + al + "</td><tr>"; 
}

现在,我想使用 aspx:grid 在我的 HTML 输入文本区域中显示 dcal 的负载值。

例如result的值为:

result =  <tr><td>1111</td><td>23</td><td><tr><td>22222</td><td>43</td><tr>

现在我想使用grid以以下格式显示数据

dc       al
1111     23
222222   43

现在,我正在使用以下命令填充文本区域。

<script type = "text/javascript">
    function submit_a()
    {
        $.post("../AllocationValidator.aspx", { "data": escape(validatorXML), "scenarioID": scenarioID }, function (result) {
            alert("Data Saved!");
            $("#allocations").empty();
            $("#allocations").html(result);
            BodyLoad();
        });
    }    
</script>

<div id = "allocations" style = "width: 650px; padding: 10px; border: 1px solid black;height: 150px; overflow:scroll;"></div>

我的问题是如何实现asp:grid来显示数据?

【问题讨论】:

  • 到底是什么问题?网格在哪里?
  • 没有网格,但我想把它放在给定的id,即“allocationstatus”
  • 为什么需要网格?普通的html表格还不够吗?
  • @AmiramKorach 实际上我的老板需要它。我已经完成了简单的 HTML :( 有可能吗?
  • 网格应该更好有什么原因吗?在客户端,网格正在变成一个 html 表格。

标签: c# asp.net html gridview


【解决方案1】:

如果您只是想在 GridView 控件中显示检索到的数据。您可以将数据源绑定到控件或以编程方式添加列和行。

您可以绑定任何实现 IListSource 或 IList 接口的数据源。这意味着您不能直接绑定 result 字符串,如问题标题中所述。您必须将检索到的数据存储在兼容的数据结构中,例如 List 以将其绑定为数据源。

要使用数据绑定,您可以将dcal 保存在类似字典的数据结构中。假设您只想显示两列数据。

var data = new Dictionary<string, string>();
for (/* Condition */)
{ 
    data.Add(dc, al);
}
grid.DataSource = data;
grid.DataBind();

对应的网格是

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField DataField="Key" HeaderText="dc"/>
    <asp:BoundField DataField="Value" HeaderText="al" />
</Columns> 
</asp:GridView>

如果您使用 foreach 来生成 result 字符串,那么您应该考虑使用 foreach 语句中的对象作为数据源的可能性。

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 2019-12-11
    • 1970-01-01
    • 2015-11-30
    • 2021-10-24
    • 2014-02-02
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多