By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

jscript call problem

P: 2
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):
Expand|Select|Wrap|Line Numbers
  1. protected void ActivateScript()
  2.     {
  3. Response.Write("<tr><td> <input type=\"radio\" value=\"1\" name=\"RadioBtn1\" id=\"radiobtn_\" onClick=\"rbChange('TextBox1', true)\"> </td></tr>");
  4. }
On Default.aspx part...I added jscript with function I want to call from aspx.cs file:
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.     function rbChange(tbId, state){
  3.          var textBox = document.getElementById(tbId);
  4.         tb.disabled = state; 
  5.     }
  6. </script>
Jscript should set the textbox in read only mode when user click on RadioBtn...however..jscript function does not work at all...I don`t know what`s going on..
Of course..from Default.aspx page I call <%ActivateScript();%>
someone have some idea?
Mar 18 '08 #1
Share this Question
Share on Google+
4 Replies


Frinavale
Expert Mod 5K+
P: 9,731
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):
Expand|Select|Wrap|Line Numbers
  1. protected void ActivateScript()
  2.     {
  3. Response.Write("<tr><td> <input type=\"radio\" value=\"1\" name=\"RadioBtn1\" id=\"radiobtn_\" onClick=\"rbChange('TextBox1', true)\"> </td></tr>");
  4. }
On Default.aspx part...I added jscript with function I want to call from aspx.cs file:
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.     function rbChange(tbId, state){
  3.          var textBox = document.getElementById(tbId);
  4.         tb.disabled = state; 
  5.     }
  6. </script>
Jscript should set the textbox in read only mode when user click on RadioBtn...however..jscript function does not work at all...I don`t know what`s going on..
Of course..from Default.aspx page I call <%ActivateScript();%>
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
Expand|Select|Wrap|Line Numbers
  1. foreach (String radioButtonName in radioButtonNamesList)
  2. {     myRadioButtonList.Items.Add(New ListItem(radioButtonName));}
  3.  
Then you should add the JavaScript call to each of the items in the Radio Button list:
Expand|Select|Wrap|Line Numbers
  1. foreach(ListItem lst in myRadioButtonList.Items)
  2. {    lst.Attributes.Add("onclick","javascript:rbChange('"+TextBox1.ClientID+"',true);");}
  3.  
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....the 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_MyTextBox" 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
Mar 18 '08 #2

P: 2
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 |
-----------------------------------------------------------------------------
Mar 18 '08 #3

Frinavale
Expert Mod 5K+
P: 9,731
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
Mar 18 '08 #4

Frinavale
Expert Mod 5K+
P: 9,731
You could also create the a table and add it to a Panel:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CreateTable()
  2.   Dim dr As Data.DataRow
  3.   Dim dt As New Data.DataTable 'the table that will hold the card holders
  4. 'add columns to the table so that data can be added to that column
  5.   dt.Columns.Add(New Data.DataColumn("checkbox1", GetType(CheckBox)))
  6.   dt.Columns.Add(New Data.DataColumn("checkbox2", GetType(CheckBox)))
  7.   dt.Columns.Add(New Data.DataColumn("textbox1", GetType(TextBox)))
  8.  
  9. 'do database calls.....then loop through database and create a new row for each record and add the content to the table...
  10.  
  11. For Each s As Object in MyDatabaseResults
  12.     dr= dt.NewRow
  13.     Dim check1 As New CheckBox()
  14.     check1.Attributes.Add("onclick", "javascript:.....")
  15.     check1.Checked = ....
  16.     dr("checkbox1")= check1
  17.     Dim check2 As New CheckBox()
  18.     check2.Attributes.Add("onclick", "javascript:.....")
  19.     check2.Checked = ....
  20.     dr("checkbox2")=check2
  21.     Dim text1 As New TextBox
  22.     text1.Text=....
  23.     dr("TextBox")=text1
  24.     dt.Rows.Add(dr)
  25. Next
  26.  
Expand|Select|Wrap|Line Numbers
  1. myPanel.Controls.Add(myDynamicallyGeneratedTable)
  2.  
Mar 18 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.