简体   繁体   中英

Value in hidden field repeating with comma

I have a hidden field inside a gridview and the values are bound to the gridview.

Let us assume the value of hidden field is 1.

when I tried to retrieve data from it in code behind, the value of hidden field is changed to "1,1".

When for some reason you post-back with many elements with the same id/name, then the value you get server-side for that id/name is a comma separated list of the values from the duplicated elements.

For example you post this 3 hidden input elements, with the same name

<input type="hidden" name="par" value="1">
<input type="hidden" name="par" value="1">
<input type="hidden" name="par" value="1">

you get this on code behind par="1,1,1"

Use asp.net HiddeField controls, this way you will make sure each hiddenfield will have a unique client id.

<ItemTemplate>
    <asp:Literal ID="MyLiteral" runat="server" Text='<%# Bind("VisibleValue") %>'/>
    <asp:HiddenField ID="MyHiddenField" runat="server" Value='<%# Bind("HiddenValue")%>' />
</ItemTemplate>

In the code behind you might access the hiddenfield values with something like this:

For Each gvRow As GridViewRow In GridView.Rows
    Dim Value as string= CType(gvRow.FindControl("MyHiddenField"), HiddenField).Value
Next

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