when I try to use a hidden field inside repeater inside repeater
<repeater>
<repeater>
<hiddenfield>
I tried this code to enter to the value
if (shiftrepeater.Items.Count > 0)
{
for(int shiftcount = 0 ; shiftcount<shiftrepeater.Items.Count ; shiftcount++)
{
Repeater temp = (Repeater)shiftrepeater.Items[shiftcount].FindControl("saturdayrepeater");
for (int count = 0; count < temp.Items.Count; count++)
{
DropDownList ds = (DropDownList)temp.FindControl("userdropdown");
HiddenField hf = (HiddenField)temp.FindControl("hiddenid");
SarcShiftUser user = new SarcShiftUser();
user.id = int.Parse(hf.Value);
user.workzone_id=1;
user.xdate = saturday.Text;
user.table_id = id;
user.shift_id = shiftcount+1;
user.user_id = int.Parse(ds.SelectedValue);
user.level_id= 1;
user.team_id=1;
}
}
}
the asp.net code :
<asp:Repeater ID="saturdayrepeater" runat="server" DataSourceID="saturdayrepeaterds">
<HeaderTemplate>
<table width="100%" cellpadding="0" cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HiddenField ID="hiddenid" runat="server" Value='<%#Eval("id") %>' />
<asp:DropDownList ID="userdropdown" CssClass="select" runat="server" DataSourceID="userdropdownds" DataTextField="name" DataValueField="id" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
</td></tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
every thing is ok ... but I got this error :
Object reference not set to an instance of an object.
at this line :
user.id = int.Parse(hf.Value);
Why do I get the error?
Looks like hf
is null because you're trying to find the HiddenField
using temp.FindControl
instead of temp.Items[count].FindControl
.
Change the following
DropDownList ds = (DropDownList)temp.FindControl("userdropdown");
HiddenField hf = (HiddenField)temp.FindControl("hiddenid");
to this
DropDownList ds = (DropDownList)temp.Items[count].FindControl("userdropdown");
HiddenField hf = (HiddenField)temp.Items[count].FindControl("hiddenid");
UPDATE
This line will throw an error if ds.SelectedValue
is an empty string or contains non numeric characters
user.user_id = int.Parse(ds.SelectedValue);
You need to change it to this
int userID = 0;
if (int.TryParse(ds.SelectedValue, out userID))
{
user.user_id = userID;
}
else
{
// do something when ds.SelectedValue is non numeric
}
Try This
foreach (RepeaterItem items in temp.Items)
{
DropDownList ds = (DropDownList)items.FindControl("userdropdown");
HiddenField hf = (HiddenField)items.FindControl("hiddenid");
//your code
}
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.