jscript call problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajvan
    New Member
    • Mar 2008
    • 2

    jscript call problem

    Hi everybody,
    I have one problem, and I hope some of you guys can help me with this.
    I`m working in C# (Visual Studio 2005)...writing simple web site .
    I place grid on Default.aspx and put one textbox in it.
    On the other side, on Default.aspx.cs page I write simple method which dynamicly generate Radio Button on page..(code bellow):
    [code=cpp]
    protected void ActivateScript( )
    {
    Response.Write( "<tr><td> <input type=\"radio\" value=\"1\" name=\"RadioBtn 1\" id=\"radiobtn_\ " onClick=\"rbCha nge('TextBox1', true)\"> </td></tr>");
    }[/code]

    On Default.aspx part...I added jscript with function I want to call from aspx.cs file:
    [code=javascript]
    <script language="javas cript" type="text/javascript">
    function rbChange(tbId, state){
    var textBox = document.getEle mentById(tbId);
    tb.disabled = state;
    }
    </script>[/code]

    Jscript should set the textbox in read only mode when user click on RadioBtn...howe ver..jscript function does not work at all...I don`t know what`s going on..
    Of course..from Default.aspx page I call <%ActivateScrip t();%>
    someone have some idea?
    Last edited by Frinavale; Mar 18 '08, 06:04 PM. Reason: added [code] tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by Ajvan
    Hi everybody,
    I have one problem, and I hope some of you guys can help me with this.
    I`m working in C# (Visual Studio 2005)...writing simple web site .
    I place grid on Default.aspx and put one textbox in it.
    On the other side, on Default.aspx.cs page I write simple method which dynamicly generate Radio Button on page..(code bellow):
    [code=cpp]
    protected void ActivateScript( )
    {
    Response.Write( "<tr><td> <input type=\"radio\" value=\"1\" name=\"RadioBtn 1\" id=\"radiobtn_\ " onClick=\"rbCha nge('TextBox1', true)\"> </td></tr>");
    }[/code]

    On Default.aspx part...I added jscript with function I want to call from aspx.cs file:
    [code=javascript]
    <script language="javas cript" type="text/javascript">
    function rbChange(tbId, state){
    var textBox = document.getEle mentById(tbId);
    tb.disabled = state;
    }
    </script>[/code]

    Jscript should set the textbox in read only mode when user click on RadioBtn...howe ver..jscript function does not work at all...I don`t know what`s going on..
    Of course..from Default.aspx page I call <%ActivateScrip t();%>
    someone have some idea?
    First of all you shouldn't use Response.Write to dynamically generate your Radio Buttons.

    Instead you should use a RadioButtonList .
    Drag this object on to the page and give it a name....then in your Page_Load function you should dynamically add values to it...

    Eg
    [code=cpp]
    foreach (String radioButtonName in radioButtonName sList)
    { myRadioButtonLi st.Items.Add(Ne w ListItem(radioB uttonName));}
    [/code]

    Then you should add the JavaScript call to each of the items in the Radio Button list:
    [code=cpp]
    foreach(ListIte m lst in myRadioButtonLi st.Items)
    { lst.Attributes. Add("onclick"," JavaScript:rbCh ange('"+TextBox 1.ClientID+"',t rue);");}
    [/code]

    This will the JavaScript for the "onclick" event to each radio button in the list.
    Please note that I'm passing the Textbox1's ClientID. The reason for this is because sometimes your Textbox (control) names are changed to something other than what you code upon rendering....th e real ID for the text box is the ClientID.

    eg:
    You may declare a text box and give it the ID: "MyTextBox" but when it's displayed in the browser the ID could be changed to: "ctl00_MyTextBo x" so that .Net is able to tell what textbox belongs to what control if there is more than one control on the page with a textbox named MyTextBox.



    -Frinny

    Comment

    • Ajvan
      New Member
      • Mar 2008
      • 2

      #3
      first...thanks for your reply...
      second...I don`t get it what do you mean with your code...You just tell me not to use Response.Write. .but didn`t tell me why not??
      Perhaps I should explain problem better:
      I read some data from DB. Number of Items on page depends of number of items taken from DB..
      so, if I have, let`s say 3 records in DB I have following situation on page:

      RadioBtn1 RadioBtn1 (mutually exclusive) TextBox1
      RadioBtn2 RadioBtn2 (mutually exclusive) TextBox2
      RadioBtn3 RadioBtn3 (mutually exclusive) TextBox3

      When user clicks on first RadioBtn (first record) => TextBox goes to Read only
      When user clicks on second RadioBtn (first record) => TextBox goes to Read/write

      Also, this Items ..as you can see in the first post, should be positioned in Table...
      That`s the reason I use Response.Write. ..perhaps it can be do it in other way, but so far I used this one, and everything works fine...

      RadioBtn-s in the same row have same name but different Value, so I can distinct which RadioBtn is checked (RadioBtn definitions in the first post)

      -----------------------------------------------------------------------------
      | O RadioBtn1 | O RadioBtn1 | TextBox1 |
      -----------------------------------------------------------------------------
      | O RadioBtn2 | O RadioBtn2 | TextBox2 |
      -----------------------------------------------------------------------------
      | O RadioBtn3 | O RadioBtn3 | TextBox3 |
      -----------------------------------------------------------------------------

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by Ajvan
        first...thanks for your reply...
        second...I don`t get it what do you mean with your code...You just tell me not to use Response.Write. .but didn`t tell me why not??
        Perhaps I should explain problem better:
        I read some data from DB. Number of Items on page depends of number of items taken from DB..
        so, if I have, let`s say 3 records in DB I have following situation on page:

        RadioBtn1 RadioBtn1 (mutually exclusive) TextBox1
        RadioBtn2 RadioBtn2 (mutually exclusive) TextBox2
        RadioBtn3 RadioBtn3 (mutually exclusive) TextBox3

        When user clicks on first RadioBtn (first record) => TextBox goes to Read only
        When user clicks on second RadioBtn (first record) => TextBox goes to Read/write

        Also, this Items ..as you can see in the first post, should be positioned in Table...
        That`s the reason I use Response.Write. ..perhaps it can be do it in other way, but so far I used this one, and everything works fine...

        RadioBtn-s in the same row have same name but different Value, so I can distinct which RadioBtn is checked (RadioBtn definitions in the first post)

        -----------------------------------------------------------------------------
        | O RadioBtn1 | O RadioBtn1 | TextBox1 |
        -----------------------------------------------------------------------------
        | O RadioBtn2 | O RadioBtn2 | TextBox2 |
        -----------------------------------------------------------------------------
        | O RadioBtn3 | O RadioBtn3 | TextBox3 |
        -----------------------------------------------------------------------------
        The reason you shouldn't use Response.Write is because this will insert your data anywhere in the output stream. This means that your data could be inserted before the <body> tag...even before the <head> or <html> tags. It can appear anywhere and can cause problems.

        If I were you, I'd use a Repeater Control to create my table...and adding the JavaScript calls in with the repeater.

        -Frinny

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          You could also create the a table and add it to a Panel:
          Code:
          Private Sub CreateTable()
            Dim dr As Data.DataRow
            Dim dt As New Data.DataTable 'the table that will hold the card holders
          'add columns to the table so that data can be added to that column
            dt.Columns.Add(New Data.DataColumn("checkbox1", GetType(CheckBox)))
            dt.Columns.Add(New Data.DataColumn("checkbox2", GetType(CheckBox)))
            dt.Columns.Add(New Data.DataColumn("textbox1", GetType(TextBox)))
          
          'do database calls.....then loop through database and create a new row for each record and add the content to the table...
          
          For Each s As Object in MyDatabaseResults
              dr= dt.NewRow
              Dim check1 As New CheckBox()
              check1.Attributes.Add("onclick", "JavaScript:.....")
              check1.Checked = ....
              dr("checkbox1")= check1
              Dim check2 As New CheckBox()
              check2.Attributes.Add("onclick", "JavaScript:.....")
              check2.Checked = ....
              dr("checkbox2")=check2
              Dim text1 As New TextBox
              text1.Text=....
              dr("TextBox")=text1
              dt.Rows.Add(dr)
          Next
          [code=vbnet]
          myPanel.Control s.Add(myDynamic allyGeneratedTa ble)
          [/code]

          Comment

          Working...