1

I'm working on a Recipe Book in vb.NET but have a background in PHP. I'm trying to build up functionality to add multiple ingredients to a recipe before its saved.

In PHP I would have an auto complete input and add button. When the add button is pressed the ingredient is added to the list of ingredients (with a hidden field for the ingredient id) then when I post the recipe I would simple loop through the post data and extract the id's like this:

<div id="ingredient-list">
 <div>
  <input type="hidden" name="ingredient[0]" value="32" />
  <span>Potatoes</span>
 </div>
</div>
<div>
 <input type="text" onKeyUp="IngredientSearch(this)" />
 <div id="food-search-results" class="auto-complete"></div>
</div>

foreach($_POST['ingredient'] as $ingredient) {
 recipe.addIngredient($ingredient);
}

However I am unsure how to accomplish such a task in .NET. Currently my add button is causing a post-back to the page (which I don't want) and adding the data to a asp:repeater. Could anyone please point me in the right direction or to a code example please.

On a side note, my next task to allow the user to upload multiple images. Which will function much the same but add a new file input when ever a file is attached to the existing input.

4

2 回答 2

1

如果您不想导致回发,您可以将按钮作为 html 按钮,或者您可以将按钮和转发器放在更新面板中。这将导致部分回发。这可能是更好的选择,因为我认为您无法更新 asp:repeater。

正如 Titopo 所说,我建议使用列表来存储 ID,或者您可以使用数据集在 Viewstate 中存储 ID 和文本值。

在你的按钮点击你可以做这样的事情......

        Dim ds As DataSet
        Dim dt As DataTable
        If Not ViewState("Ingredients") Is Nothing Then
            ds = ViewState("Ingredients")
            dt = ds.Tables(0)

            Dim dr As DataRow = dt.NewRow
            dr.Item("IngredientID") = IngredientID' However you would get that.
            dr.Item("Ingredient") = txtIngredient.Text
            dt.Rows.Add(dr)
        Else
            ds = New DataSet
            dt = New DataTable
            dt.Columns.Add(New DataColumn("IngredientID"))
            dt.Columns.Add(New DataColumn("Ingredient"))

            Dim dr As DataRow = dt.NewRow
            dr.Item("IngredientID") = IngredientID' However you would get that.
            dr.Item("Ingredient") = txtIngredient.Text
            ds.Tables.Add(dt)
            ds.Tables(0).Rows.Add(dr)
        End If

        Dim dv As DataView = dt.DefaultView
        dv.Sort = "Ingrediant ASC"
        repIngrediants.DataSource = dv
        repIngrediants.DataBind()         ' -- Bind to your repeater here

        ViewState("Ingredients") = ds

这将存储每种成分的 ID 和文本。如果你在更新面板中有这个,它会顺利执行,并且只加载带有按钮、文本框和中继器的部分。

于 2012-09-25T17:46:13.170 回答
0

遗嘱ASP:Button总是会导致回发。我建议您在按下按钮时存储每种成分,但是,如果您想将所有成分作为 POST 发送,您可以使用ViewState存储成分列表。

关于Page_Load活动:

If Not Me.IsPostBack Then
  ViewState("Ingredients") = New List(Of Integer)
End If

AddButton OnClick事件中:

Dim list = CType(ViewState("Ingredients"), List(Of Integer)
list.add(ingredient_id)
ViewState("Ingredients") = list

在此之后,您可以使用该For Each语句提取所有成分,形成字符串并将其作为 POST 变量发送。

于 2012-09-25T10:25:19.737 回答