我正在使用vb.NET中的Recipe Book,但是有PHP背景知识。我正在尝试建立功能,以便在保存之前将多种成分添加到配方中。
在PHP中我会有一个自动完成输入和添加按钮。当按下添加按钮时,成分被添加到成分列表中(带有成分id的隐藏字段),然后当我发布配方时,我将简单地循环遍历帖子数据并提取id如下:
<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);
}
但是我不确定如何在.NET中完成这样的任务。目前我的添加按钮导致回页面(我不想要)并将数据添加到asp:repeater。任何人都可以请我指出正确的方向或代码示例。
在旁注中,我的下一个任务是允许用户上传多个图像。哪个功能大致相同,但在文件附加到现有输入时添加新文件输入。
答案 0 :(得分: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和文本。如果你在更新面板中有这个,它会顺利执行,只加载带有按钮,文本框和转发器的部分。
答案 1 :(得分: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变量发送。