Custom controls on one page, populating the controls issue
This question continues from the two previous questions I have posted:
1) How to get variables from .ascx in another .ascx?
2) C# asp.net textbox.text not setting?
My third question in relation is when I create a session and parse information through from ParentControl to ChildControl and then try to parse different information to a different instance of the ChildControl it copies the values from the second child control into the first child control.
Update: showing .ascx page which I missed out might be relevant.
ParentControl.ascx
<asp:PlaceHolder runat="server" id="ShowHideControls" Visible="false">
<asp:PlaceHolder runat="server" id="instanceOne" Visible="false">
<uc1:ChildControl ID="controlOne" runat="server" />
</asp:PlaceHolder>
<asp:PlaceHolder runat="server" id="instanceTwo" Visible="false">
<uc1:Chil开发者_运维问答dControl ID="controlTwo" runat="server" />
</asp:PlaceHolder>
</asp:PlaceHolder>
ParentControl.ascx.cs
public void CreateSessionsAddressString(User user)
{
if (user == user)
Session["user"] = user;
}
instanceOne ChildControl.ascx.cs
public void SetUserDetails()
{
User user = Session["user"] as User;
string fullAddress = User.fullAddress;
}
Now if I have a second instance of the same user control the following code is reused.
instanceTwo ChildControl.ascx
public void SetUserDetails()
{
User user = Session["user"] as User;
string fullAddress = user.fullAddress;
}
On the instanceTwo when a set method is invoked to update information, instanceOne seems to populate itself with data from the instanceTwo. how can I prevent this from happening?
Update: Added further information to help solve issue below.
So in our solution we have a user who has an account, each account can have one additional user. I perform a check to see if user1 and user2 are not null.
If user1 is true and user2 is null then I don't have an issue. It's when user1 & user2 is true I get an issue with getting the information because when I call the method to get user information it seems to overwrite the user1 information in the user control.
I need to as some users have suggested wrap the code so it only affects itself and not other controls on the same page. I am going to go through some of the suggestions today and try get back to your comments asap.
Update I referred back to an earlier question I asked
1) How to get variables from .ascx in another .ascx?
I guess late Friday evening aren't my best of times, I basically created a public method in the child control, and in the parent control I can call the public method for each instance so I can just type:
instanceOne.MyMethod();
instanceTwo.MyMethod();
This seems to be a good work around, and helps me avoid using sessions is well. I am going to request the question be removed because my solution is already in the a question before.
I think you're running into this problem because you're pulling the user object from session, and the user instance you're creating still points to the object reference stored in session.
Try something like this:
string fullAddress = String.Copy(User.FullAddress);
And you could try something like this too:
private string fullAddress;
public string FullAddress
{
get { return fullAddress; }
set { fullAddress = String.Copy(value); }
}
public void SetUserDetails()
{
User user = Session["user"] as User;
this.FullAddress = user.fullAddress;
this.FullAddress = "Hello world!";
}
On the instanceTwo when a set method is invoked to update information, instanceOne seems to populate itself with data from the instanceTwo.
If your set operation updates the User
instance in Session, then on postback you will see that update in both controls if your code calls childControl.SetUserDetails()
. Try wrapping your calls to SetUserDetails()
in a if(!Page.IsPostBack)
block, or create two separate instances in Session that you can update. If you only set the values on the child controls when the page is initially loaded, then you should see different values on postback.
Also, please note that I'm just trying to answer the question as asked. There is probably a better design to be used here but I don't really understand what you're trying to accomplish.
精彩评论