在此上下文中不支持代码块。当使用数据集绑定Html表时

时间:2015-01-02 14:23:17

标签: c# asp.net

在下面的代码中,我有html数据表和数据集.i想要将数据集绑定到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>   

2 个答案:

答案 0 :(得分:0)

这种情况需要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();
    }
}

答案 1 :(得分:-2)

脚本标记<% %>应放在asp占位符中。

<%%>

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

CODE:

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