Results 1 to 2 of 2

Thread: Value from usercontrol

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question Value from usercontrol

    I have this code:
    [code]

    Protected Sub Komponenter1_ClickUserControl(ByVal sender As Object, ByVal e As System.EventArgs) Handles Komponenter1.ClickUserControl
    Dim userC As UserControl = CType(sender, UserControl)
    If Not userC Is Nothing Then
    Dim userC2 As UserControl = CType(sender, UserControl)
    Dim txt1 As String
    Dim i As Integer

    txt1 = userC.ID

    If txt1.Length = 12 Then
    i = 1
    Else
    i = 2
    End If

    txt1 = CInt(txt1.Substring(11, i))

    userC2.ID = "Komponenter" + CStr(txt1)
    userC2.Visible = True
    end sub
    [code]
    which works fine. But if I change the code in the two last line to
    Code:
    userC2.ID = "Komponenter" + CStr(txt1+1)
    userC2.Visible = True
    then I receive an error:
    Code:
    Multiple controls with the same ID 'Komponenter2' were found. FindControl requires that controls have unique IDs. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.Web.HttpException: Multiple controls with the same ID 'Komponenter2' were found. FindControl requires that controls have unique IDs.
    
    Source Error: 
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
    
    Stack Trace: 
    
    [HttpException (0x80004005): Multiple controls with the same ID 'Komponenter2' were found. FindControl requires that controls have unique IDs.]
       System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +134
       System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +203
       System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +164
       System.Web.UI.Control.FindControl(String id) +9
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +74
       System.Web.UI.Page.ProcessRequestMain() +1277
    
     
    
    
    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573
    Why can't I do that?

    The code that I write is needed for an app with a lot of order lines where the next order line becomes visible if the current order line is clicked.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Wow...

    Well, you declare txt1 as a string, then assign a numeric value to it using CINT(somestring).

    When you changed the second to last line, now you're saying CSTR(txt1+1), which is really funky, because txt1 is a string value.

    Logically, how do you add the numeric value 1 to a string that represents a number "100". It certainly is not txt1 + 1, I don't care if it worked by accident for you, which is what it did, perhaps because the VB compiler is so forgiving, but as you found out, the underlying .Net framework is not.

    Let's first get this code under control:
    VB Code:
    1. Dim userC As UserControl = CType(sender, UserControl)
    2.         If Not userC Is Nothing Then
    3.             Dim userC2 As UserControl = CType(sender, UserControl)
    4.             Dim txt1 As String
    5.             Dim i As Integer = 1
    6.             [b]Dim newID As Integer[/b]
    7.  
    8.             txt1 = userC.ID
    9.  
    10.             If Not txt1.Length = 12 Then i =2
    11.  
    12.             [b]newID = Integer.Parse(txt1.Substring(11, i))
    13.             newID += 1
    14.  
    15.             'for the change that you wanted, txt1 + 1 that caused your error
    16.             userC2.ID = String.Concat("Komponenter", newID.ToString)[/b]
    17.             userC2.Visible = True
    Last edited by nemaroller; Apr 16th, 2004 at 10:41 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width