简体   繁体   中英

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

I have created a Repeater like so:

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

Issue 1 is, when I click SAVE() . In my save function I have no idea which Textbox belongs to which item in the DataBase.

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.

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. Yo can use that label to store the 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.

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>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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