简体   繁体   English

重复输入vb.net

[英]Repeating inputs vb.net

I'm working on a Recipe Book in vb.NET but have a background in PHP. 我正在vb.NET中编写食谱书,但是在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. 在PHP中,我将具有一个自动完成的输入和添加按钮。 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: 当按下添加按钮时,配料将添加到配料列表中(配料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);
}

However I am unsure how to accomplish such a task in .NET. 但是我不确定如何在.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. 当前,我的添加按钮导致回发到页面(我不想要)并将数据添加到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. 它将具有几乎相同的功能,但是只要将文件附加到现有输入,就会添加一个新的文件输入。

If you are not trying to cause a postback you could either do the button as an html button, or you could put the button and the repeater in an update panel. 如果您不尝试引起回发,则可以将该按钮作为html按钮进行操作,也可以将该按钮和转发器放在更新面板中。 This would cause a partial postback. 这将导致部分回发。 This would probably be the better option because I do not think you could update the asp:repeater otherwise. 这可能是更好的选择,因为我认为您不能以其他方式更新asp:repeater。

As Titopo said though, I would suggest either using the list to store the IDs or you could use a dataset to store both the ID and text value in the Viewstate. 正如Titopo所说,我建议您使用列表存储ID,也可以使用数据集在Viewstate中存储ID和文本值。

On your button click you could do something like this... 在按钮上单击,您可以执行以下操作...

        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

This would store the ID and Text for each ingredient. 这将存储每种成分的ID和文本。 If you had this in an update panel it would preform smoothly and only load the section with the button, text box, and repeater. 如果在更新面板中具有此功能,则它将顺利进行,并且仅使用按钮,文本框和转发器加载该部分。

An ASP:Button will always causes a post back. ASP:Button将始终导致回发。 I suggest you to store each ingredient when you press the button, but, if you want to send all of them as POST, you can use the ViewState to store a list of ingredientes. 我建议您在按下按钮时存储每种成分,但是,如果要将所有成分都发送为POST,则可以使用ViewState存储成分列表。

On the Page_Load event: Page_Load事件上:

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

And in the AddButton OnClick event: 并在AddButton OnClick事件中:

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

After this, you can use the For Each statment for extract all the ingredients, form the string and send it as POST variable. 之后,您可以使用For Each语句提取所有成分,形成字符串并将其作为POST变量发送。

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

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