开发者

Multiple instances of usercontrol on one page but only the last control is being referenced

I have a UserControl that allows the user to upload 开发者_JAVA百科files and also displays them in a GridView. On the parent page, I have a jQuery tab control to which I dynamically add 2 instances of my UserControl (on different tabs). The second instance works fine, so I know the control works. However, when I try to upload a file using the first instance, the second instance is being referenced...so all property values, control names, etc are pointing that that of the second.

This is how I am loading the controls in the parent page code-behind:

protected void Page_Load(object sender, EventArgs e)
{
  MyControl ucAttachments1 = (MyControl) Page.LoadControl("~/controls/mycontrol.ascx");
  ucAttachments1.ID = "ucAttachments1";
  ucAttachments1.Directory = "/uploads/documents";
  ucAttachments1.DataChanged += new MyControl.DataChangedEventHandler(DoSomething);
  phAttachments1.Controls.Add(ucAttachments1);

  MyControl ucAttachments2 = (MyControl)Page.LoadControl("~/controls/mycontrol.ascx");
  ucAttachments2.ID = "ucAttachments2";
  ucAttachments2.Directory = "/uploads/drawings";
  ucAttachments2.DataChanged += new MyControl.DataChangedEventHandler(DoSomething);
  phAttachmetns2.Controls.Add(ucAttachments2);
}

in the html:

<div id="tabContainer">
    <div id="files">
        <asp:PlaceHolder id="phAttachments1" runat="server" />
    </div>
    <div id="drawings">
        <asp:PlaceHolder id="phAttachments2" runat="server" />
    </div>
</div>

a snippet of the user control code:

private string directory;

override protected void OnLoad(EventArgs e)
{
    PopulateAttachmentGridview();
}

protected btnUpload_Click(object sender, EventArgs e)
{
    UploadFile(directory);
}

public string Directory
{
    get { return directory; }
    set { directory = value; }
}

How can I ensure my usercontrols are being referenced correctly?


Check the actual html and javascript rendered to the client to ensure that there isn't a duplicate ID related to the controls slipping through the cracks.


I think this is the problem

MyControl ucAttachments1 = (MyControl) Page.LoadControl("~/controls/mycontrol.ascx");

MyControl ucAttachments2 = (MyControl)Page.LoadControl("~/controls/mycontrol.ascx");

you are referencing the same instance of the control to two different variables. So now you have two different references of the same instance, Now since you set the properties for "ucAttachments2" at last so whats happening is your second control properties are being set on to the instance.. thus whenever you try to access that instance(by using "ucAttachments1" or "ucAttachments2" ) you are getting the properties of second control.

TRY DOING :

MyControl ucAttachments1 = new MyControl();
ucAttachments1 = (MyControl) Page.LoadControl("~/controls/mycontrol.ascx");
 ucAttachments1.ID = "ucAttachments1";
  ucAttachments1.Directory = "/uploads/documents";
  ucAttachments1.DataChanged += new MyControl.DataChangedEventHandler(DoSomething);
  phAttachments1.Controls.Add(ucAttachments1);


MyControl ucAttachments2 = new MyControl();
ucAttachments2 = (MyControl) Page.LoadControl("~/controls/mycontrol.ascx");
ucAttachments2.ID = "ucAttachments2";
  ucAttachments2.Directory = "/uploads/drawings";
  ucAttachments2.DataChanged += new MyControl.DataChangedEventHandler(DoSomething);
  phAttachmetns2.Controls.Add(ucAttachments2);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜