"Collision" in DropDownLists from two instances of same UserContro


A

Alex Maghen

I have a UserControl which has only one constituent control, a DropDownList.

I am having what seems to be an IMPOSSIBLE situation: In the Page_Load() of
my ASPX, I am setting
MyCtl1.SomePropery = "A";
MyCtl2.SomePropery = "B";

Inside my control, I pretty much set the DropDownList (DDL.SelectedValue) to
the SomeProperty.

Now here's the crazy thing that's happening:
If I do what I showed above in Page_Load(), BOTH of my controls will have
the same "B" in their respective DropDownLists. Somehow, the property I am
setting for one of these is getting into the settings for the other. And it's
always the control that's LAST in order in the HTML of my ASPX page whose
value copies to those that came before it. How can this be happeneing?

Alex

Somehow, whichever instance of MyCtl came LAST in the HTML of the ASPX file
will
 
Ad

Advertisements

S

Steven Cheng[MSFT]

Hi Alex,

I've posted my followup in your previous thread in the aspnet newsgroup:

"Design-Time UserControl Property Setting"

As for the new problem you mentioned here, I think it depend on how to
implement the custom property, are you storing the property data into a
shared place that multiple usercontrols instances could overwrite it? For
exapmle, if you use a static varitable , then all the control instance may
share the same variable instance. Anyway, for your convenience, I'll paste
my test usercontrol(modified one according to your another thread) here,
you can have a test against it to see whether it works(based on my local
test it works well):

=======ascx=============
<asp:Label ID="lblCategories" runat="server" Text="Categories:
"></asp:Label>
<asp:DropDownList ID="lstCategories" runat="server"
OnSelectedIndexChanged="lstCategories_SelectedIndexChanged">
</asp:DropDownList>

==========code behind==========
public partial class UserControls_ddl_ListUC : System.Web.UI.UserControl
{

private int _count;

public int Count
{
get { return _count; }
set { _count = value; }
}

public string SelectedValue
{
get { return ViewState["_selectedvalue"] as string; }
set
{
ViewState["_selectedvalue"] = value;
}
}



protected void Page_Load(object sender, EventArgs e)
{


if (!IsPostBack)
{
string sql = "select TOP {0} CategoryID, CategoryName from
Categories";


SqlConnection conn = new
SqlConnection(WebConfigurationManager.ConnectionStrings["LocalNorthwindConnS
tr"].ConnectionString);

conn.Open();

SqlCommand comm = new SqlCommand(string.Format(sql, _count),
conn);
SqlDataReader reader =
comm.ExecuteReader(CommandBehavior.CloseConnection);

lstCategories.DataSource = reader;
lstCategories.DataTextField = "CategoryName";
lstCategories.DataValueField = "CategoryName";


lstCategories.DataBind();

reader.Close();


}

}

protected void Page_PreRender(object sender, EventArgs e)
{
lstCategories.SelectedValue = SelectedValue;

}

protected void lstCategories_SelectedIndexChanged(object sender,
EventArgs e)
{
SelectedValue = lstCategories.SelectedValue;
}
}
======================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Ad

Advertisements

S

Steven Cheng[MSFT]

Hi Alex,

Does my test control helps some on this issue? If you still meet any
problem or anything else we can help, please don't hesitate to post here.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top