Forum Moderators: open

Message Too Old, No Replies

Problem with label in repeater

         

andrewsmd

3:56 pm on Oct 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am havning trouble setting the text of a lable within a repeater control. What I need to do is update two lables when the user clicks an add button within a repeater. Here is my aspx the two labels are price and partNumber

<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table cellspacing="5">
</HeaderTemplate>
<ItemTemplate>
<tr valign="middle">
<td width="80px">

<asp:Button ID="AddButton" runat="server" Text="Add" CommandName="Add" CausesValidation="True" ValidationGroup ="bulkValidation" />
<asp:Button ID="RemoveBtn" runat="server" Text="Delete" CommandName="Remove" CausesValidation="false"
Visible="false" />
</td>
<td width="175px">
<telerik:RadComboBox ID="RadSearchComboBox" runat="server" Width="150px" Height="175px" ValidationGroup ="bulkValidation"
AllowCustomText="True" ShowToggleImage="False" OnSelectedIndexChanged="itemChanged"
ShowMoreResultsBox="true" EnableLoadOnDemand="True" Skin="Telerik" MarkFirstMatch="True"
OnItemsRequested="loadSearch" EnableVirtualScrolling="true" EmptyMessage="Enter Part Number"
ErrorMessage="Value not Found" AutoPostBack="False" OnClientItemsRequested="OnClientItemsRequested">
<CollapseAnimation Duration="100" Type="OutQuint" />
</telerik:RadComboBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ValidationGroup ="bulkValidation"
ControlToValidate="RadSearchComboBox" />
</td>
<td width="45px">
<telerik:RadNumericTextBox ID="QtyTextBox" runat="server" MaxLength="3" MaxValue="999" ValidationGroup ="bulkValidation"
MinValue="1" NumberFormat-DecimalDigits="0" ShowSpinButtons="false" Type="Number"
Width="20px" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ValidationGroup ="bulkValidation"
ControlToValidate="QtyTextBox" />
</td>
<td class="H2Internal">
<asp:Label CssClass ="H2Internal" id="partName" runat="server" Text=""></asp:Label>
<asp:Label CssClass ="H2Internal" id="price" runat="server" Text=""></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

Here is the vb

Protected Sub rptrCommand(ByVal sender As Object, ByVal e As RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
Dim index As Integer
index = e.Item.ItemIndex
If e.CommandName = "Remove" Then
bulkItems.RemoveAt(index)
Me.Repeater1.DataSource = bulkItems
Me.Repeater1.DataBind()

ElseIf e.CommandName = "Add" Then
Page.Validate()
If Page.IsValid() Then
Me.bulkItems.Add(New bulkOrderItem())
Me.Repeater1.DataSource = bulkItems

'this is a static label outside of the repeater
'it sets to test when the user clicks to add
msgLabel.Text = "test"

'the two labels
Dim tempPartLabel As New Label
tempPartLabel = Me.Repeater1.Items(index).FindControl("partName")
Dim tempPriceLabel As New Label
tempPriceLabel = Me.Repeater1.Items(index).FindControl("price")

Me.Repeater1.DataBind()
Me.Repeater1.Items(index).FindControl("AddButton").Visible = False
Me.Repeater1.Items(index).FindControl("RemoveBtn").Visible = True

'right now I am just trying to set them to static data
'however this will not work.
tempPartLabel.Text = "testing"
tempPriceLabel.Text = "price testing"

End If
Panel1.Update()
End If
End Sub

Does anyone know what I am doing wrong? Thanks,

marcel

8:56 pm on Oct 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried setting a breakpoint at tempPartLabel.Text = "testing" to see if tempPartLabel has actually been found and follow it's progress through the panel1.Update()

marcel

7:31 am on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried a quick test with the following code and it works fine here:

If e.CommandName = "Add" Then
Dim myLabel As Label = e.Item.FindControl("Label1")
If Not myLabel Is Nothing Then myLabel.Text = "testing123"
End If

Are you repopulating the repeater on Page_Load or in another event?

andrewsmd

7:54 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I got it, I had to do this because I needed to run it on a telerik control.
VB
Protected Sub itemChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs)
'the instances of the labels within the repeater
Dim priceLabel As Label = TryCast((TryCast(o, Telerik.Web.UI.RadComboBox)).NamingContainer.FindControl("price"), Label)
Dim partLabel As Label = TryCast((TryCast(o, Telerik.Web.UI.RadComboBox)).NamingContainer.FindControl("partName"), Label)

Dim connString As String = System.Configuration.ConfigurationManager.ConnectionStrings("orthmanConnectionString").ToString
Dim myConnection As New Data.SqlClient.SqlConnection(connString)
Dim strSQL = "getPartInfo"
Dim selectCommand As New Data.SqlClient.SqlCommand(strSQL, myConnection)
Dim userReader As SqlDataReader
Try
selectCommand.CommandType = CommandType.StoredProcedure
selectCommand.Parameters.AddWithValue("@partId", e.Text)
myConnection.Open()
userReader = selectCommand.ExecuteReader()
userReader.Read()

If Not (IsDBNull(userReader(0))) Then
partLabel.Text = "Part Name: <span class='MainText'>" & userReader(0) & "</span>"
Else
partLabel.Text = "Part Name: <span class='MainText'>No information</span>"
End If

If Not (IsDBNull(userReader(1))) Then
priceLabel.Text = "Price: <span class='MainText'>" & FormatCurrency(userReader(1)) & "</span>"
Else
priceLabel.Text = "Price: <span class='MainText'>No information</span>"
End If

userReader.Close()
Catch ex As Exception
Me.msgLabel.Text = ex.ToString
'Me.msgLabel.Text = "Oops, there was an error in retrieving the part information. " & _
' "Try reloading the page (ctrl+f5) and entering the information again. Please contact the system " & _
' "administrator if the problem persists"
Finally
myConnection.Close()
End Try

End Sub

Aspx

<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table cellspacing="5">
</HeaderTemplate>
<ItemTemplate>
<tr valign="middle">
<td width="80px">

<asp:Button ID="AddButton" runat="server" Text="Add" CommandName="Add" CausesValidation="True" ValidationGroup ="bulkValidation" />
<asp:Button ID="RemoveBtn" runat="server" Text="Delete" CommandName="Remove" CausesValidation="false"
Visible="false" />
</td>
<td width="175px">
<telerik:RadComboBox ID="RadSearchComboBox" runat="server" Width="150px" Height="175px" ValidationGroup ="bulkValidation"
onselectedindexchanged="itemChanged"
AllowCustomText="True" ShowToggleImage="False"
ShowMoreResultsBox="true" EnableLoadOnDemand="True" Skin="Telerik" MarkFirstMatch="True"
OnItemsRequested="loadSearch" EnableVirtualScrolling="true" EmptyMessage="Enter Part Number"
ErrorMessage="Value not Found" AutoPostBack="True" OnClientItemsRequested="OnClientItemsRequested">
<CollapseAnimation Duration="100" Type="OutQuint" />
</telerik:RadComboBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ValidationGroup ="bulkValidation"
ControlToValidate="RadSearchComboBox" />
</td>
<td width="45px">
<telerik:RadNumericTextBox ID="QtyTextBox" runat="server" MaxLength="3" MaxValue="999" ValidationGroup ="bulkValidation"
MinValue="1" NumberFormat-DecimalDigits="0" ShowSpinButtons="false" Type="Number"
Width="20px" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ValidationGroup ="bulkValidation"
ControlToValidate="QtyTextBox" />
</td>
<td class="H2Internal">
<asp:Label CssClass ="H2Internal" id="partName" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;
<asp:Label CssClass ="H2Internal" id="price" runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

I hope that helps someone else because it took me forever to figure that out.