Problem with asp.net and JavaScript

Problem with asp.net and JavaScript

Post by Scott Ros » Thu, 24 Oct 2002 01:56:35



I am trying to utilize the asp.net web controls for their
server side fucntionality along with some client side
javascript functionality. I am using the table web control
and building cells and controls within those cells on the
fly. On each row I need to add two of the text boxes
together and present the results into the third. I do not
want to do this posting back to the server. I want this to
happen client side when the users update either one of the
text boxes. Can someone help me out. Here is the code that
builds the table...

rivate Sub Page_Load(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not IsPostBack Then

            With Me.dlstPOrg.Items
                .Add("Corp")
                .Add("ADSC")
                .Add("ADNW")
            End With

            With Me.dlstPlant.Items
                .Add("MNW3")
                .Add("MSAN")
                .Add("MNW3")
            End With

            With Me.dlistCur.Items
                .Add("Dollars")
                .Add("Yen")
                .Add("Euro")
            End With

            With Me.dlistTerms.Items
                .Add("FOB")
            End With

            With Me.dlistUrgent.Items
                .Add("Medium")
                .Add("Low")
                .Add("High")
            End With

        End If

        Table1.BorderStyle = BorderStyle.Solid
        Table1.GridLines = GridLines.Both
        Dim r As New TableRow()
        Dim c1 As New TableHeaderCell()
        Dim c2 As New TableHeaderCell()
        Dim c3 As New TableHeaderCell()
        Dim c4 As New TableHeaderCell()
        Dim c5 As New TableHeaderCell()
        Dim c6 As New TableHeaderCell()
        Dim c7 As New TableHeaderCell()
        Dim c8 As New TableHeaderCell()
        Dim c9 As New TableHeaderCell()
        Dim c10 As New TableHeaderCell()
        Dim c11 As New TableHeaderCell()
        Dim c12 As New TableHeaderCell()
        Dim c13 As New TableHeaderCell()

        c1.Controls.Add(New LiteralControl("Item"))
        r.Cells.Add(c1)
        c2.Controls.Add(New LiteralControl("Acct Type"))
        r.Cells.Add(c2)
        c3.Controls.Add(New LiteralControl("Cost Center"))
        r.Cells.Add(c3)
        c4.Controls.Add(New LiteralControl("G/L Account"))
        r.Cells.Add(c4)
        c5.Controls.Add(New LiteralControl("Asset /
Project"))
        r.Cells.Add(c5)
        c6.Controls.Add(New LiteralControl("Description"))
        r.Cells.Add(c6)
        c7.Controls.Add(New LiteralControl("Matl Group"))
        r.Cells.Add(c7)
        c8.Controls.Add(New LiteralControl("Delivery
Date"))
        r.Cells.Add(c8)
        c9.Controls.Add(New LiteralControl("Qty"))
        r.Cells.Add(c9)
        c10.Controls.Add(New LiteralControl("UM"))
        r.Cells.Add(c10)
        c11.Controls.Add(New LiteralControl("Est."))
        r.Cells.Add(c11)
        c12.Controls.Add(New LiteralControl("Unit Cost"))
        r.Cells.Add(c12)
        c13.Controls.Add(New LiteralControl("Total Cost"))
        r.Cells.Add(c13)

        Table1.Rows.Add(r)

        Dim i As Integer
        Do While i < 7
            Dim x As New TableRow()
            Dim d1 As New TableCell()
            Dim d2 As New TableCell()
            Dim d3 As New TableCell()
            Dim d4 As New TableCell()
            Dim d5 As New TableCell()
            Dim d6 As New TableCell()
            Dim d7 As New TableCell()
            Dim d8 As New TableCell()
            Dim d9 As New TableCell()
            Dim d10 As New TableCell()
            Dim d11 As New TableCell()
            Dim d12 As New TableCell()
            Dim d13 As New TableCell()

            Dim txtItem As New TextBox()
            Dim lstAcct As New DropDownList()
            Dim txtCC As New TextBox()
            Dim txtGl As New TextBox()
            Dim txtAsset As New TextBox()
            Dim txtDesc As New TextBox()
            Dim txtMatGroup As New TextBox()
            Dim txtDDate As New TextBox()
            Dim txtQty As New TextBox()
            Dim txtUM As New TextBox()
            Dim lstEst As New DropDownList()
            Dim txtUC As New TextBox()
            Dim txtTC As New TextBox()

            With txtItem
                .Text = i + 1
                .Width = Unit.Pixel(20)
                .Enabled = False
                .BackColor = Color.LightGray
            End With
            d1.Controls.Add(txtItem)
            x.Cells.Add(d1)
            txtItem.ID = "item" & i

            With lstAcct.Items
                .Add("Capital")
                .Add("Balance Sheet")
                .Add("Expense")
            End With

            d2.Controls.Add(lstAcct)
            x.Cells.Add(d2)

            txtCC.Width = Unit.Pixel(50)
            d3.Controls.Add(txtCC)
            x.Cells.Add(d3)
            txtGl.Width = Unit.Pixel(50)
            d4.Controls.Add(txtGl)
            x.Cells.Add(d4)
            txtAsset.Width = Unit.Pixel(50)
            d5.Controls.Add(txtAsset)
            x.Cells.Add(d5)
            With txtDesc
                .Width = Unit.Pixel(120)
                .Height = Unit.Pixel(40)
                .TextMode = TextBoxMode.MultiLine
            End With
            txtDesc.Width = Unit.Pixel(120)
            d6.Controls.Add(txtDesc)
            x.Cells.Add(d6)
            txtMatGroup.Width = Unit.Pixel(50)
            d7.Controls.Add(txtMatGroup)
            x.Cells.Add(d7)
            txtDDate.Width = Unit.Pixel(50)
            d8.Controls.Add(txtDDate)
            x.Cells.Add(d8)
            txtQty.Width = Unit.Pixel(30)
            d9.Controls.Add(txtQty)
            x.Cells.Add(d9)
            txtUM.Width = Unit.Pixel(30)
            d10.Controls.Add(txtUM)
            x.Cells.Add(d10)
            With lstEst.Items
                .Add("Y")
                .Add("N")
            End With
            lstEst.Width = Unit.Pixel(50)
            d11.Controls.Add(lstEst)
            x.Cells.Add(d11)
            txtUC.Width = Unit.Pixel(60)
            d12.Controls.Add(txtUC)
            x.Cells.Add(d12)

            txtTC.Width = Unit.Pixel(60)
            d13.Controls.Add(txtTC)
            x.Cells.Add(d13)
            Table1.Rows.Add(x)

            i = i + 1
        Loop

    End Sub

 
 
 

Problem with asp.net and JavaScript

Post by Mike Moore [MS » Thu, 24 Oct 2002 08:21:10


Hi Scott,

QUESTION
I have a web control table. In my Page_Load (if not postback), I add rows
and cells to the table. Many of these cells contain text boxes. For two of
these text boxes, I want to use client-side code to update a third text box
(on the same row) when either of these two text boxes change.

ANSWER
For the two text boxes will trigger a change to the third text box, each
one needs code added to its "onchange" event. The code is obvious, at first:
    Text3.value = Text1.value + Text2.value;

However, each of these text boxes is repeated on each row. How can we
identify the correct text boxes so we combine the correct strings and put
the results into the correct box on the same row?

One answer is to use the code-behind to give each text box a unique ID.
Then use string manipulation to output the correct onchange event to each
text box.

----
Add a web server table control to a web form (named table1).
Add the following to the code behind.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
      If Not IsPostBack Then
         Dim i As Int32
         For i = 1 To 5
            Dim r As New TableRow()
            Dim i2 As Int32
            For i2 = 1 To 3
               Dim c As New TableCell()
               Dim t As New TextBox()
               t.ID = "txtR" & i & "C" & i2
               c.Controls.Add(t)
               r.Cells.Add(c)
               t.Text = t.ClientID
               Dim s As String = "txtR" & i & "C"
               If i2 < 3 Then
                  t.Attributes.Add("onchange", "UpdateText('" & s & "1','"
& s & "2','" & s & "3');")
               End If
            Next i2
            Table1.Rows.Add(r)
         Next i
         If Not Page.IsClientScriptBlockRegistered("UpdateText") Then
            Dim s2 As String
            s2 = "<script language=javascript>"
            s2 = s2 & "function UpdateText(a,b,c) {"
            s2 = s2 & "document.all(c).value = "
            s2 = s2 & "document.all(a).value"
            s2 = s2 & " + ' ' + " & "document.all(b).value;"
            s2 = s2 & "}"
            s2 = s2 & "</script>"
            Page.RegisterClientScriptBlock("UpdateText", s2)
         End If
      End If
   End Sub

Please repost if you have more questions on this.

Thank you, Mike Moore
Microsoft, ASP.NET

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

--------------------

>Content-Class: urn:content-classes:message
>From: "Scott Rosa" <Scott.R...@Analog.com>
>Sender: "Scott Rosa" <Scott.R...@Analog.com>
>Subject: Problem with asp.net and JavaScript
>Date: Tue, 22 Oct 2002 09:56:35 -0700
>Lines: 198
>Message-ID: <0d4301c279eb$fb18b0d0$95e62ecf@tkmsftngxs02>
>MIME-Version: 1.0
>Content-Type: text/plain;
>    charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
>Thread-Index: AcJ56/sY1iESmdukScyJu8BM+LqjSg==
>Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
>NNTP-Posting-Host: TKMSFTNGXS02 10.201.232.66
>Path: cpmsftngxa06!tkmsftngp01!tkmsftngxs02!cpmsftngxa09
>Xref: cpmsftngxa06

microsoft.public.dotnet.framework.aspnet.webcontrols:6541

- Show quoted text -

>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols

>I am trying to utilize the asp.net web controls for their
>server side fucntionality along with some client side
>javascript functionality. I am using the table web control
>and building cells and controls within those cells on the
>fly. On each row I need to add two of the text boxes
>together and present the results into the third. I do not
>want to do this posting back to the server. I want this to
>happen client side when the users update either one of the
>text boxes. Can someone help me out. Here is the code that
>builds the table...

>rivate Sub Page_Load(ByVal sender As System.Object, ByVal
>e As System.EventArgs) Handles MyBase.Load
>        'Put user code to initialize the page here
>        If Not IsPostBack Then

>            With Me.dlstPOrg.Items
>                .Add("Corp")
>                .Add("ADSC")
>                .Add("ADNW")
>            End With

>            With Me.dlstPlant.Items
>                .Add("MNW3")
>                .Add("MSAN")
>                .Add("MNW3")
>            End With

>            With Me.dlistCur.Items
>                .Add("Dollars")
>                .Add("Yen")
>                .Add("Euro")
>            End With

>            With Me.dlistTerms.Items
>                .Add("FOB")
>            End With

>            With Me.dlistUrgent.Items
>                .Add("Medium")
>                .Add("Low")
>                .Add("High")
>            End With

>        End If

>        Table1.BorderStyle = BorderStyle.Solid
>        Table1.GridLines = GridLines.Both
>        Dim r As New TableRow()
>        Dim c1 As New TableHeaderCell()
>        Dim c2 As New TableHeaderCell()
>        Dim c3 As New TableHeaderCell()
>        Dim c4 As New TableHeaderCell()
>        Dim c5 As New TableHeaderCell()
>        Dim c6 As New TableHeaderCell()
>        Dim c7 As New TableHeaderCell()
>        Dim c8 As New TableHeaderCell()
>        Dim c9 As New TableHeaderCell()
>        Dim c10 As New TableHeaderCell()
>        Dim c11 As New TableHeaderCell()
>        Dim c12 As New TableHeaderCell()
>        Dim c13 As New TableHeaderCell()

>        c1.Controls.Add(New LiteralControl("Item"))
>        r.Cells.Add(c1)
>        c2.Controls.Add(New LiteralControl("Acct Type"))
>        r.Cells.Add(c2)
>        c3.Controls.Add(New LiteralControl("Cost Center"))
>        r.Cells.Add(c3)
>        c4.Controls.Add(New LiteralControl("G/L Account"))
>        r.Cells.Add(c4)
>        c5.Controls.Add(New LiteralControl("Asset /
>Project"))
>        r.Cells.Add(c5)
>        c6.Controls.Add(New LiteralControl("Description"))
>        r.Cells.Add(c6)
>        c7.Controls.Add(New LiteralControl("Matl Group"))
>        r.Cells.Add(c7)
>        c8.Controls.Add(New LiteralControl("Delivery
>Date"))
>        r.Cells.Add(c8)
>        c9.Controls.Add(New LiteralControl("Qty"))
>        r.Cells.Add(c9)
>        c10.Controls.Add(New LiteralControl("UM"))
>        r.Cells.Add(c10)
>        c11.Controls.Add(New LiteralControl("Est."))
>        r.Cells.Add(c11)
>        c12.Controls.Add(New LiteralControl("Unit Cost"))
>        r.Cells.Add(c12)
>        c13.Controls.Add(New LiteralControl("Total Cost"))
>        r.Cells.Add(c13)

>        Table1.Rows.Add(r)

>        Dim i As Integer
>        Do While i < 7
>            Dim x As New TableRow()
>            Dim d1 As New TableCell()
>            Dim d2 As New TableCell()
>            Dim d3 As New TableCell()
>            Dim d4 As New TableCell()
>            Dim d5 As New TableCell()
>            Dim d6 As New TableCell()
>            Dim d7 As New TableCell()
>            Dim d8 As New TableCell()
>            Dim d9 As New TableCell()
>            Dim d10 As New TableCell()
>            Dim d11 As New TableCell()
>            Dim d12 As New TableCell()
>            Dim d13 As New TableCell()

>            Dim txtItem As New TextBox()
>            Dim lstAcct As New DropDownList()
>            Dim txtCC As New TextBox()
>            Dim txtGl As New TextBox()
>            Dim txtAsset As New TextBox()
>            Dim txtDesc As New TextBox()
>            Dim txtMatGroup As New TextBox()
>            Dim txtDDate As New TextBox()
>            Dim txtQty As New TextBox()
>            Dim txtUM As New TextBox()
>            Dim lstEst As New DropDownList()
>            Dim txtUC As New TextBox()
>            Dim txtTC As New TextBox()

>            With txtItem
>                .Text = i + 1
>                .Width = Unit.Pixel(20)
>                .Enabled = False
>                .BackColor = Color.LightGray
>            End With
>            d1.Controls.Add(txtItem)
>            x.Cells.Add(d1)
>            txtItem.ID = "item" & i

>            With lstAcct.Items
>                .Add("Capital")
>                .Add("Balance Sheet")
>                .Add("Expense")
>            End With

>            d2.Controls.Add(lstAcct)
>            x.Cells.Add(d2)

>            txtCC.Width = Unit.Pixel(50)
>            d3.Controls.Add(txtCC)
>            x.Cells.Add(d3)
>            txtGl.Width = Unit.Pixel(50)
>            d4.Controls.Add(txtGl)
>            x.Cells.Add(d4)
>            txtAsset.Width = Unit.Pixel(50)
>            d5.Controls.Add(txtAsset)
>            x.Cells.Add(d5)
>            With txtDesc
>                .Width = Unit.Pixel(120)
>                .Height = Unit.Pixel(40)
>                .TextMode = TextBoxMode.MultiLine
>            End With
>            txtDesc.Width = Unit.Pixel(120)
>            d6.Controls.Add(txtDesc)
>            x.Cells.Add(d6)
>            txtMatGroup.Width = Unit.Pixel(50)
>            d7.Controls.Add(txtMatGroup)
>            x.Cells.Add(d7)
>            txtDDate.Width = Unit.Pixel(50)
>            d8.Controls.Add(txtDDate)
>            x.Cells.Add(d8)
>            txtQty.Width = Unit.Pixel(30)
>            d9.Controls.Add(txtQty)
>            x.Cells.Add(d9)
>            txtUM.Width = Unit.Pixel(30)
>            d10.Controls.Add(txtUM)
>            x.Cells.Add(d10)
>            With lstEst.Items
>                .Add("Y")
>                .Add("N")
>            End With
>            lstEst.Width = Unit.Pixel(50)
>            d11.Controls.Add(lstEst)
>            x.Cells.Add(d11)
>            txtUC.Width = Unit.Pixel(60)
>            d12.Controls.Add(txtUC)
>            x.Cells.Add(d12)

>            txtTC.Width = Unit.Pixel(60)
>            d13.Controls.Add(txtTC)
>            x.Cells.Add(d13)
>            Table1.Rows.Add(x)

>            i = i + 1
>        Loop

>    End Sub


 
 
 

1. ASP.NET User Control problem with Javascript

Hi Friends
I am having some problem with ASP.NET user control and
javascript.
I have User control which is having asp textbox control in
it with access modifier as 'Public'

I have used this User cotrol in one of the asp.net web
form (.aspx). Now I want to change the content of the asp
textbox which is in my User control at client side, for
this I am trying to access the textbox control of user
control through javascript(client side). the Code
is something like this
document["Banner"].item("BannerLabel").value =
FormLabel.value.
However I am getting the javascript error as 'null object'.
More ever when I see the client side HTML source code it
did not show any control by the Id which is there in User
cntrol.
So my problem is how to access the controls defined in
asp.net User control through client side javascript.

ASP.NET Gurus and Javascript Masters please help me out.

Regards
Makarand.

2. iptables

3. Problem regarding asp.net validation controls and javascript

4. Which OS/2 drivers for Hercules Dynamite Pro VLB?

5. asp.net (vb.net) and ClientID behaviour with JavaScript.

6. "Deleting a modem" as a fix on Windows 95?

7. Migration: ASP -> ASP.NET: How write clientside javascript inside a function?

8. How to disable KEYBOARD & Mouse..??

9. HOWTO Resolve the Nested "Javascript" ---Migrate Asp -> ASP.NET

10. problem installing ASP.NET V1 after ASP.NET Beta 2

11. Javascript on-click code run from within an xmlDocument() in asp.net?

12. How to debug JavaScript in an ASP.NET project

13. ASP.net MD5 value not the same as Javascript MD5