繁体   English   中英

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

[英]Code blocks are not supported in this context.When Binding Html Table With dataset

在下面的代码中,我具有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>   

这种情况对GridView或Repeater不利。 以下是中继器的外观的简要视图:

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM