Page cycle question asp.net
I am using nested repeaters. I render several groups and every group has members that I need to render out different properties to.
I am using ItemDataBound on the nested repeater to find out which member the nested repeater is rendering out at the moment. I have <% %> tags in the .aspx which checks if the current user has a certain property, and if it has, I render the divs and other HTML elements that need to go there.
All the data is correct. When I debug I collect the correct data and I get the right info about who the user is and what properties the user has. I save the current evaluated user in a protected string currentRenderedUser that I use to find out who the user is in the aspx file.
The problem is that by the time the <% %> on the aspx gets evaluated the repeater has already gone through all the sets or something, because the currentRenderedUser is the same during every evalutaion in the aspx file.
I don't know the correct terminology for "<% %>" code in the aspx file, sorry.
This is the relevant parts of the aspx (I think):
<ItemTemplate>
<!-- nested repeater data -->
<tr>
<td width="50%"><div class="memberName"><%# DataBinder.Eval(Container.DataItem, "userEmail" )%></div></td>
<div style="display: none;"></div>
<% if (CheckIfUserIsAdmin(email, currentRenderedGroup))
{ %>
<%if (CheckIfUserIsAdmin(currentRenderedUser, currentRenderedGroup))
{ //Checks so that the user being rendered isn't already admin%>
<td><div style="margin-right:30px;" class="makeAdminButton"></div></td>
<% }
else
{%>
<td><div style="margin-right:30px;" class="makeAdminButton"><asp:ImageButton ID="ImageButtonMakeUserAdmin" CommandName="MakeAdmin" BorderWidth="0" ImageUrl="~/gfx/doAdmin.png" CommandArgument='<%#Convert.ToString(DataBinder.Eval(Container.DataItem, "userEmail" )) + " " + Convert.ToString(DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "groupId"))%>' runat="server" AlternateText="Make admin" /></div></td>
<td><div class="removeUserButton"><asp:ImageButton ID="ImageButtonRemoveUserFromGroup" CommandName="RemoveUserFromGroup" BorderWidth="0" ImageUrl="~/gfx/can.png" CommandArgument='<%# Convert.ToString(DataBinder.Eval(Container.DataItem, "userEmail" )) + " " + Convert.ToString(DataBinder.Eval(((RepeaterItem)Conta开发者_C百科iner.Parent.Parent).DataItem, "groupId"))%>' runat="server" AlternateText="Delete" /></div></td>
<% } %>
<%}%>
</tr>
</ItemTemplate>
And this is the part of the back-end that evaluates the current user:
protected void NesterRepeater_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item)
{
DataRowView dataRowView = (DataRowView)e.Item.DataItem;
string memberInGroup = Convert.ToString(dataRowView["userEmail"]);
currentRenderedUser = memberInGroup;
}
}
How can I solve this in a better (or atleast, working) way? Thanks
The problem is that the event ItemDataBound is executed first so you probably always get the value of the last record from the Repeater's DataSource if I'm not mistaken.
It is better to do it this way:
<ItemTemplate>
<tr>
<td width="50%"><div class="memberName"><%# DataBinder.Eval(Container.DataItem, "userEmail" )%></div></td>
<div style="display: none;"></div>
<asp:PlaceHolder ID="phNoAdmin" runat="server" Visible="false">
<td><div style="margin-right:30px;" class="makeAdminButton"></div></td>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phAdmin" runat="server" Visible="false">
<td><div style="margin-right:30px;" class="makeAdminButton"><asp:ImageButton ID="ImageButtonMakeUserAdmin" CommandName="MakeAdmin" BorderWidth="0" ImageUrl="~/gfx/doAdmin.png" CommandArgument='<%#Convert.ToString(DataBinder.Eval(Container.DataItem, "userEmail" )) + " " + Convert.ToString(DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "groupId"))%>' runat="server" AlternateText="Make admin" /></div></td>
<td><div class="removeUserButton"><asp:ImageButton ID="ImageButtonRemoveUserFromGroup" CommandName="RemoveUserFromGroup" BorderWidth="0" ImageUrl="~/gfx/can.png" CommandArgument='<%# Convert.ToString(DataBinder.Eval(Container.DataItem, "userEmail" )) + " " + Convert.ToString(DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "groupId"))%>' runat="server" AlternateText="Delete" /></div></td>
</asp:PlaceHolder>
</tr>
</ItemTemplate>
And the code behind:
protected void NesterRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
DataRowView dataRowView = (DataRowView)e.Item.DataItem;
PlaceHolder phNoAdmin = e.Item.FindControl("phNoAdmin") as PlaceHolder;
PlaceHolder phAdmin = e.Item.FindControl("phAdmin") as PlaceHolder;
string memberInGroup = Convert.ToString(dataRowView["userEmail"]);
currentRenderedUser = memberInGroup;
if (CheckIfUserIsAdmin(email, currentRenderedGroup))
{
if (CheckIfUserIsAdmin(currentRenderedUser, currentRenderedGroup))
{
phNoAdmin.Visible = true;
phAdmin.Visible = false;
}
else
{
phNoAdmin.Visible = false;
phAdmin.Visible = true;
}
}
}
}
It would be better if you bind the data inside the 2 PlaceHolder control I wrote also in code behind for maintainability reason.
HTH
精彩评论