1 Replies - 5593 Views - Last Post: 13 September 2012 - 06:12 PM Rate Topic: -----

#1 November-06   User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 482
  • Joined: 04-January 11

Using javascript inside usercontrol

Posted 11 September 2012 - 02:00 AM

If I add the javascript codes inside the usercontrol, calling the js function causes error since it can't find the object. If I add the javascript in the page that will call the usercontrol, then the function is being successfully called. But now I do not know how to set textbox value inside the usercontrol.

To make it clear, the scenario is this...

I have only 1 aspx page which I call Main.aspx. I wanted it to have a navigation in which if a menu is clicked, there won't be a full page postback. Only the updatepanel for the content will be posted back. So what I did was create a page, created a layout for the header, the navigation, the breadcrumbs, the footer and of course, the content.

The content contains an updatepanel which will be the only one to change in case a menu in the navigation is clicked.

So, my pages are created using usercontrols instead of webforms. The usercontrol is being added to the page after menu click. So here, my usercontrol (which acts as my page) is called by the mainpage (Main.aspx).

Now, I have another usercontrol. This time, it does not act as a page but as an object. It contains a repeater control to display a table for reports. I have different reports that will be displayed with only one format so I decided to use a usercontrol to display the different reports without needing to type the codes over and over. So this usercontrol (the object-like control) is being called inside my other control (which is my page) which is being called by my mainpage. No problem with that. It is being successfully displayed. But my problem is this...

The table inside the object usercontrol has this functionality...

col1 col2 col3 col4 col5
val1.1 val1.2 val1.3 val1.4 val1.5
val2.1 val2.2 val2.3 val2.4 val2.5
val3.1 val3.2 val3.3 val3.4 val3.5

Say, col3 and col4 are clickable columns with col1 as the primary key.

If I click val1.3, I need to open a popup while passing val1.1 (the primary key) and col3 (column name) as parameters. Then it will read some data and display something in the popup.

I did something similar to this before but since I was not using a usercontrol back then, it was easier. Since this grid that I created will be used many times, I prefer it to be created in a usercontrol but if you guys have better suggestions, I would really be glad to entertain them.

Now back to the similar project that I said I did, I was able to do it using this approach:
First, I created textbox controls to handle parameters but I hide them using style="display:none;"
Second, I wrote a javascript function that accepts parameters and set the textbox values with the parameters then postback the updatepanel containing the textboxes.
Third, in the databound event of the repeater, I added attribute to the control "onclick" to call the javascript function I created in step 2.
Fourth, in the textchanged event, I called the popup and used the textboxes' values as reference.

This is the workaround I created before but I am a newbie to javascript so I do not have an idea of the best practice in this situation.

So again, back to my problem. I couldn't apply the same method to my scenario now with the usercontrol since the javascript cannot be placed in the usercontrol.

I hope you can teach me of a way in which I can set the value of a textbox inside a usercontrol with a js function inside the page that will call the usercontrol. It would also be nice if you can suggest of a better approach to this kind of problem.

Is This A Good Question/Topic? 1
  • +

Replies To: Using javascript inside usercontrol

#2 November-06   User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 482
  • Joined: 04-January 11

Re: Using javascript inside usercontrol

Posted 13 September 2012 - 06:12 PM

I decided not to use the usercontrol inside a usercontrol which is inside a web form for now but I would be glad to learn how to do that in the future.

I had to resort to a web form with a usercontrol and everything works fine. Only I did not need the textboxes and the modal popup extender anymore.

I do not really know much of javascript but I read a lot of articles for calling WebMethods from javascript and passing objects to WebMethod so I can call server-side methods from client-side. As for the popup, I used the window control from the obout tools. It works faster than in my previous project where I depended on textbox postback to show the modal popup extender since what I did now is more on the client-side.

To those who are interested to learn obout tools, just go to their site http://www.obout.com/
They have a lot of tools that might be useful to you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1