此上下文不支持代码块.将 html 表与数据集绑定时

本文关键字:数据集 绑定 定时 html 上下文 不支持 代码 | 更新日期: 2023-09-27 18:35:51

在下面的代码中,我有html数据表和数据集.我想将数据集绑定到html表。我的数据行计数是 25,它抛出错误"此上下文不支持代码块"。请帮助我纠正问题。

 public string getWhileLoopData()
{
 string htmlStr = "";
  MastersClient objIndent = new MastersClient();
                DataSet ds = objIndent.GetIndent(hidIndentID.Value);
                DataRow[] drIndentID = ds.Tables[0].Select("IndentID =" + hidIndentID.Value);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    txtQty.Value = drIndentID[i]["RecommentedQuantity"].ToString();
                    string Qty=txtQty.Value ;
                    string strProductID = drIndentID[i]["ProductID"].ToString();
                    ddlProduct.Text = strProductID;
                    txtDate.Text = drIndentID[i]["ProductRequiredDate"].ToString();
                    string date= txtDate.Text;
                    htmlStr += "<tr><td>" + Qty + "</td><td>" + strProductID + "</td><td>" + date + "</td></tr>"; 
                }

        return htmlStr;
}


<table id="dataTable" width="350px" border="1" runat="server">
        <tr <%Response.Write(getWhileLoopData())%>>
            <td><input id="checkbox" type="checkbox" name="chk" runat="server"/></td>
            <td><input type="text" name="txt" id="txtQty" runat="server"/></td>
            <td>
            <asp:DropDownList ID="ddlProduct" runat="server"  Style="width: 100%; height:23px" ></asp:DropDownList>  

            </td>
           <td>
           <asp:TextBox ID="txtDate" Style="text-align: left" onkeypress="return isNumberKey(event, false);"
                                                        onblur="DateValidation(this)" onkeyup="ValidateDate(this, event.keyCode)" onkeydown="return DateFormat(this, event.keyCode)"
                                                        Height="20px" runat="server" Width="80px"> </asp:TextBox>

           </td>
        </tr>
    </table>   

此上下文不支持代码块.将 html 表与数据集绑定时

这种情况

需要 GridView 或中继器。下面是转发器外观的简要视图:

<table>
    <asp:Repeater runat="server" id="ProductRepeater">
        <ItemTemplate>
            <tr>
                <td>
                    <asp:CheckBox runat="server" id="chk" />
                </td>
                <td>
                    <asp:Label runat="server" id="txtQty" Text='<%#: Eval("RecommentedQuantity") %>' />
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

然后在页面加载时绑定数据。

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        MastersClient objIndent = new MastersClient();
        DataSet ds = objIndent.GetIndent(hidIndentID.Value);
        ProductRepeater.DataSource = ds.Tables[0].Select("IndentID =" + hidIndentID.Value);
        ProductRepeater.DataBind();
    }
}

脚本标记<% %>应放置在 ASP 占位符内。

将所有<%%>代码添加到<asp:PlaceHolder>your code with <%><%> here </asp:PlaceHolder>

法典:

<asp:PlaceHolder ID="plhTable" runat="server">
<table id="dataTable" width="350px" border="1" runat="server">
        <tr <%Response.Write(getWhileLoopData())%>>
            <td><input id="checkbox" type="checkbox" name="chk" runat="server"/></td>
            <td><input type="text" name="txt" id="txtQty" runat="server"/></td>
            <td>
            <asp:DropDownList ID="ddlProduct" runat="server"  Style="width: 100%; height:23px" ></asp:DropDownList>  

            </td>
           <td>
           <asp:TextBox ID="txtDate" Style="text-align: left" onkeypress="return isNumberKey(event, false);"
                                                        onblur="DateValidation(this)" onkeyup="ValidateDate(this, event.keyCode)" onkeydown="return DateFormat(this, event.keyCode)"
                                                        Height="20px" runat="server" Width="80px"> </asp:TextBox>

           </td>
        </tr>
    </table>   
</asp:PlaceHolder>