[英]WebForms Repeater, setting text-box attribute to be used as identifier on Save
I am building a relatively simple WebForm that has two columns with about 15 fixed input fields in each and 1 that is "Other" which has count of (n). 我正在构建一个相对简单的WebForm,该WebForm具有两列,每列中有大约15个固定输入字段,而1列是“其他”,其计数为(n)。
I have created a Repeater like so: 我已经创建了一个Repeater,如下所示:
<asp:Repeater ID="rptClientItems" runat="server" OnItemDataBound="rptClientItems_ItemDataBound">
<ItemTemplate>
<div class="row">
<div class="col-md-8">
<asp:Label ID="lblItemType" runat="server"></asp:Label>
<asp:TextBox ID="txtOtherItemTypeDescription" runat="server" Visible="false" CssClass="lblandinput"></asp:TextBox>
</div>
<div class="col-md-4">
<asp:TextBox ID="txtItemTextBoxArea" runat="server"></asp:TextBox>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
Now in my ItemDataBound()
function I loop over all my DataBase elements and populate values in these text areas and label. 现在,在
ItemDataBound()
函数中,我遍历所有DataBase元素,并在这些文本区域和标签中填充值。
Issue 1 is, when I click SAVE()
. 问题1是,当我单击
SAVE()
。 In my save function I have no idea which Textbox
belongs to which item in the DataBase. 在我的保存功能中,我不知道哪个
Textbox
属于数据库中的哪个项目。
Issue 2 my txtOtherItemTypeDescription
is dynamically created so on save I am using Request.Form.GetValues("key")
to get all the values however I feel this is not the most productive way to do this as this also leads to above issue after first save editing these items will be a pain. 问题2我的
txtOtherItemTypeDescription
是动态创建的,因此保存时我正在使用Request.Form.GetValues("key")
获取所有值,但是我觉得这不是最有效的方法,因为这样做也会导致上述问题保存编辑这些项目会很痛苦。
I am relatively new to this so good example and explanation will go a long way. 对于这个很好的例子,我还比较陌生,因此说明会很长。 Thank you
谢谢
If you want to keep using a Repeater, the simplest solution would be to add a Label that is hidden since a Repeater does not have an DataKeyField
property. 如果要继续使用中继器,最简单的解决方案是添加一个隐藏的标签,因为中继器没有
DataKeyField
属性。 Yo can use that label to store the ID. 您可以使用该标签存储ID。
<ItemTemplate>
<asp:Label ID="hiddenID" runat="server" Text='<%# Eval("ID") %>' Visible="false"></asp:Label>
</ItemTemplate>
Is your Save method you can then loop the Repeater Items and get the value of the TextBox and the Label. 是您的Save方法,然后可以循环Repeater Items并获取TextBox和Label的值。
foreach (RepeaterItem item in rptClientItems.Items)
{
Label lb = item.FindControl("hiddenID") as Label;
TextBox tb = item.FindControl("txtItemTextBoxArea") as TextBox;
Label1.Text += lb.Text + ": " + tb.Text + "<br>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.