开发者

Cant assign values to variable in a user control on ajax postback... or something like that!

I have a simple user control which toggles between the visibility of a few images depending on the UserId. When the page is loaded for the first time, the userID is being assigned correctly (I'm debugging). However, there are two other buttons on the page for filtering the type of users, they are perform their functionality via Ajax (not the toolkit, but the normal javascript ajax)

When one of these buttons are clicked, the user control cant get the userid, it always comes up as Zero. Oh, btw, the usercontrol is in a DataRepeater, so it occurs for each of the user.. all the time.. or thats the Idea. I have several other pages like this, (none of them have ajax) and the user control has no problems whatsoever.

My User control code:

public partial class ucAuthorityBadges : BaseUserControl {

    protected void Page_Load(object sender, EventArgs e)
    {
            if (UserId > 0 && UOBJ.GroupId &开发者_开发知识库gt; 0)
            {
                DataSet DS = new DataSet();
                DS = huDataAccessLayer.dbTools.GetUserAuthority(UserId);

                if (!utils.isDataSetEmpty(DS))
                {
                    imgTeamBadge.Visible = Convert.ToBoolean(DS.Tables[0].Rows[0]["isTeam"]);
                    imgDoctorBadge.Visible = Convert.ToBoolean(DS.Tables[0].Rows[0]["isDoctor"]);
                    imgAdminBadge.Visible = Convert.ToBoolean(DS.Tables[0].Rows[0]["isAdmin"]);
                    imgVolunteerBadge.Visible = Convert.ToBoolean(DS.Tables[0].Rows[0]["isVolunteer"]);
                    imgBloggerBadge.Visible = Convert.ToBoolean(DS.Tables[0].Rows[0]["isBlogger"]);
                }
            }
      }
}

And the Data Repeater is something like this:

<div class="profileContainer">
<asp:HyperLink Enabled='<%# Eval("isActive") %>' ID="profileName_link" runat="server" Text='<%# Eval("Username") %>' NavigateUrl='<%# "/Profile/" + Eval("Username") %>'></asp:HyperLink>
<img id="friend_image" runat="server" src="/css/images/icons/friend.gif" alt="friend" title="Friend" visible='<%# Convert.ToBoolean(Eval("isFriend")) %>' style="vertical-align:middle" /> 
<ucAB:AuthorityBadges ID="ucAuthBadge" runat="server" UserId='<%# Convert.ToInt32(Eval("UserId")) %>' />
<%# Eval("UserId") %>
<asp:Label ID="status_label1" Text=" · Friend Request Pending" style="font-size:small;" Visible='<%# Convert.ToBoolean(Eval("isFriendReqPending")) %>' runat="server" Font-Bold="false"></asp:Label>
 </div>

Here, Immediately after the userControl line, I am printing the userid just to see if its valid (by <%# Eval("UserId") %> and the user id's are valid too. I dont understand why the User Control cant pick it up on ajax postback?

Any hint at this would be really great,

Thanks so much!


I think the problem here is that your execution code actually lies in the Page_Load event, which occurs before the repeater is DataBound. A solution to that would be to place your code that is currently in Page_Load into another public method, and then on ItemDataBound event you need to find your control and manually call the public method with the parameters you need. This way you will populate all the fields you need once the control has already initialized.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜