Repeater SubControl Dropdownlist not saving SelectedValue

B

Ben Dewey

Hey everyone,

I have a wierd issue i can't seem to find out whats going on. I have a
Control for a Shopping Cart Merchant Page called OrderStatus.ascx Inside
that control there is a Repeater with the Products that were ordered on the
ItemCreated event of the repeater i set a property of an options control
within the repeater. The Options control sets a DropDownList and then sets
a SelectedValue.

The Selected Value prop is correct after set, when you get back to the
itemCreated sub it is set there as well, But when it gets out of the
DataBind function and back to the PageLoad sub this value is cleared. Any
idea why anyone?

Here is the code:
---------------------------------------

--
OrderStatus.aspx
--
<asp:repeater id="ProductsRepeater" runat="server">
<itemtemplate>
<tr bgcolor="#ffffff">
<td class="adminBlack" valign="top">
<input type="hidden" runat="server" id="ProductID"
value='<%#DataBinder.Eval(Container.DataItem, "ProductID")%>'>
<asp:textbox runat="server" id="txtQty" cssclass="formText"
text='<%#DataBinder.Eval(Container.DataItem, "Quantity")%>' width="30px">
</asp:textbox><br>
<asp:button runat="server" id="updateQty"
commandargument='<%#DataBinder.Eval(Container.DataItem, "ProductID")%>'
commandname="updateQty" text="Update" cssclass="submitButton">
</asp:button>
</td>
<td class="adminBlack"><a
href='http://www.mrchocolate.com/ecom/site/product.cfm?id=<%#DataBinder.Eval(Container.DataItem,
"ProductID")%>'><%#DataBinder.Eval(Container.DataItem, "Name")%></a>
<uc1:productoptionscontrol id="ProductOptionsControl1"
runat="server"></uc1:productoptionscontrol>
</td>
<td class="adminBlack" align="right"
valign="top"><%#String.Format("{0:c}", DataBinder.Eval(Container.DataItem,
"ItemPrice"))%></td>
<td class="adminBlack" align="right"
valign="top"><%#String.Format("{0:c}", DataBinder.Eval(Container.DataItem,
"ItemTotal"))%></td>
</tr>
</itemtemplate>
</asp:repeater>


--
OrderStatus.aspx.vb
--
--
Sub Page_Load:
--
Me.ProductsRepeater.DataSource = shipAddress.OrderItems
Me.ProductsRepeater.DataBind()
'SelectedValue is reset back to 29 here ??? WHY ???

--
Sub ProductsRepeater_ItemCreated:
--
ProductOptionsControl1 = e.Item.FindControl("ProductOptionsControl1")

If Not IsNothing(Me.ProductOptionsControl1.OrderItem) Then
Dim item As COrderItem = e.Item.DataItem

ProductOptionsControl1.OrderItem = item
'SelectedValue is valid at 27
End If

--
ProductOptionsControl.ascx.vb:
--
Private m_orderItem As COrderItem
Friend Property OrderItem() As COrderItem
Get
If IsNothing(m_orderItem) Then
m_orderItem = CType(Me.ViewState.Item("m_orderItem"), COrderItem)
End If
Return m_orderItem
End Get
Set(ByVal Value As COrderItem)
m_orderItem = Value
Me.ViewState.Add("m_orderItem", m_orderItem)

If IsNothing(m_orderItem) Then
m_orderItem.LoadOrderAttributes()

If Me.m_orderItem.Attributes.Count = 1 Then
Dim objProducts As New CStoreProducts
Dim arAttribs As ArrayList =
objProducts.GetProductAttributes(m_orderItem.ProductID)

Me.lblAttributeName.Text = CType(arAttribs(0), CAttribute).Name

Me.ddlAttributes.DataSource = CType(arAttribs(0),
CAttribute).AttributeDetails
Me.ddlAttributes.DataTextField = "Name_Price_Info"
Me.ddlAttributes.DataValueField = "UID"
Me.ddlAttributes.DataBind()

'Example DDL Values are 29,27,28
'Selected Value is set to 27
Me.ddlAttributes.SelectedValue = CType(CType(m_orderItem.Attributes(0),
CAttribute).AttributeDetails(0), CAttributeDetail).UID.ToString

'SelectedValue is valid at 27

Else
Me.Visible = False
End If
End If
End Set
End Property

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
 
B

Ben Dewey

It might be an issue with the ViewState. It doesn't seem to be saving. The
Controls EnableViewState=true, and also the Instance EnableViewState=true

Anyone?
 
I

intrader

Hey everyone,

I have a wierd issue i can't seem to find out whats going on. I have a
Control for a Shopping Cart Merchant Page called OrderStatus.ascx Inside
that control there is a Repeater with the Products that were ordered on the
ItemCreated event of the repeater i set a property of an options control
within the repeater. The Options control sets a DropDownList and then sets
a SelectedValue.

The Selected Value prop is correct after set, when you get back to the
itemCreated sub it is set there as well, But when it gets out of the
DataBind function and back to the PageLoad sub this value is cleared. Any
idea why anyone?

Here is the code:
---------------------------------------

--
OrderStatus.aspx
--
<asp:repeater id="ProductsRepeater" runat="server">
<itemtemplate>
<tr bgcolor="#ffffff">
<td class="adminBlack" valign="top">
<input type="hidden" runat="server" id="ProductID"
value='<%#DataBinder.Eval(Container.DataItem, "ProductID")%>'>
<asp:textbox runat="server" id="txtQty" cssclass="formText"
text='<%#DataBinder.Eval(Container.DataItem, "Quantity")%>' width="30px">
</asp:textbox><br>
<asp:button runat="server" id="updateQty"
commandargument='<%#DataBinder.Eval(Container.DataItem, "ProductID")%>'
commandname="updateQty" text="Update" cssclass="submitButton">
</asp:button>
</td>
<td class="adminBlack"><a
href='http://www.mrchocolate.com/ecom/site/product.cfm?id=<%#DataBinder.Eval(Container.DataItem,
"ProductID")%>'><%#DataBinder.Eval(Container.DataItem, "Name")%></a>
<uc1:productoptionscontrol id="ProductOptionsControl1"
runat="server"></uc1:productoptionscontrol>
</td>
<td class="adminBlack" align="right"
valign="top"><%#String.Format("{0:c}", DataBinder.Eval(Container.DataItem,
"ItemPrice"))%></td>
<td class="adminBlack" align="right"
valign="top"><%#String.Format("{0:c}", DataBinder.Eval(Container.DataItem,
"ItemTotal"))%></td>
</tr>
</itemtemplate>
</asp:repeater>


--
OrderStatus.aspx.vb
--
--
Sub Page_Load:
--
Me.ProductsRepeater.DataSource = shipAddress.OrderItems
Me.ProductsRepeater.DataBind()
'SelectedValue is reset back to 29 here ??? WHY ???

--
Sub ProductsRepeater_ItemCreated:
--
ProductOptionsControl1 = e.Item.FindControl("ProductOptionsControl1")

If Not IsNothing(Me.ProductOptionsControl1.OrderItem) Then
Dim item As COrderItem = e.Item.DataItem

ProductOptionsControl1.OrderItem = item
'SelectedValue is valid at 27
End If

--
ProductOptionsControl.ascx.vb:
--
Private m_orderItem As COrderItem
Friend Property OrderItem() As COrderItem
Get
If IsNothing(m_orderItem) Then
m_orderItem = CType(Me.ViewState.Item("m_orderItem"), COrderItem)
End If
Return m_orderItem
End Get
Set(ByVal Value As COrderItem)
m_orderItem = Value
Me.ViewState.Add("m_orderItem", m_orderItem)

If IsNothing(m_orderItem) Then
m_orderItem.LoadOrderAttributes()

If Me.m_orderItem.Attributes.Count = 1 Then
Dim objProducts As New CStoreProducts
Dim arAttribs As ArrayList =
objProducts.GetProductAttributes(m_orderItem.ProductID)

Me.lblAttributeName.Text = CType(arAttribs(0), CAttribute).Name

Me.ddlAttributes.DataSource = CType(arAttribs(0),
CAttribute).AttributeDetails
Me.ddlAttributes.DataTextField = "Name_Price_Info"
Me.ddlAttributes.DataValueField = "UID"
Me.ddlAttributes.DataBind()

'Example DDL Values are 29,27,28
'Selected Value is set to 27
Me.ddlAttributes.SelectedValue = CType(CType(m_orderItem.Attributes(0),
CAttribute).AttributeDetails(0), CAttributeDetail).UID.ToString

'SelectedValue is valid at 27

Else
Me.Visible = False
End If
End If
End Set
End Property

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Perhaps the problem is the postback behavior. you are rerendering
everything, everytime. Use the IsPostBack to control when you are
rebuilding the controls.
 
B

Ben Dewey

Well I figured out what the problem was. I was using the IsPostBack to
check and I wasn't rebinding the Repeater. The problem was that I was using
the OnItemCreated event for the repeater. I changed it to the
OnItemDataBound Event.

When I used the OnItemCreated event it Rebound the Nested Repeater on
everypost back

Essentially if you use nested repeaters and you are having an issue saving
the state. It probably a rebinding issue. Make sure you are checking the
IsPostBack and only Binding Once. Also make sure you are binding your
nested repeaters/controls on the OnDataBound Event, not the OnItemCreated
Event.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top