?
Solved

Alter an iFrame source (src) from a User Control

Posted on 2004-09-16
10
Medium Priority
?
571 Views
Last Modified: 2012-08-14
Hi All

Heres the gist of it ...... basically I've got an aspx page with a user control in it. The user control generates a list of buttons from a SQL Db containing the text for the button and a URL (uses a repeater control). The page also contains an Iframe (ifrmMain), when I select / click a button in the user control on the page I want it to change the page in the iFrame to the URL for that button from the Db. I'm using VB not C# so would prefer any suggestions / examples to be in VB, however if you've got a C# one then I'll try and figure my way through it. Not done a great deal with .NET yet so try and keep it simple for me :-)

Thanks in advance

Rob
0
Comment
Question by:robert_f
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12072955
Hi,

to do that you have to use RegisterClientScriptBlock method like this:

 Dim scriptString as String = "<script language=JavaScript> your_iFrame_id.src = 'your_new_html_page';"
        scriptString += "</script>"
     
            If(Not IsClientScriptBlockRegistered("clientScript"))
        RegisterClientScriptBlock("clientScript", scriptString)
        End If
put this in your button click event and it will be executed when you press the button

Regards,
B..M
0
 

Author Comment

by:robert_f
ID: 12073284
Hi BM

Thanks for the quick response .... I've added in the code you suggested but when I click the button apart from the page refreshing nothing happens ?!? ... hope you can help.

Cheers

Rob
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12073310
I'm not quite sure ( i can not tested this right now ) but have you checked if on the second click the src of the iframe was changed ?
if the answer is "YES" - then you have to add the
your_iFrame_id.src = 'your_new_html_page';" line
on your onclick client event in your Page Load like this:

btnChangeIFrameSrc.Attributes.Add ("onclick", "your_iFrame_id.src = 'your_new_html_page'; return false;" )

this will add the client onclick event to your button on the page load and when you click the button page will not be submitted but the src of the iFrame should be changed

post if you have problems

Regards
B..M
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:robert_f
ID: 12073624
Hi BM

Tried that and I'm afraid had no joy with it. It looks like its entering the script OK because if i deliberately enter something incorrect in to the script i get a script debug error when i click the button. Ity just does not seem to actually do anything when i click it.

Cheers

Rob
0
 

Author Comment

by:robert_f
ID: 12073635
Please see an extract of my code below :

In User Control ctlCommand I have the following repeater which inserts the button based on a Db query :

                  <table id="tblCommand">
                        <asp:repeater id="rptLinks" runat="server">
                              <ItemTemplate>
                                    <TR>
                                          <td align="center" nowrap>
                                                <asp:button CausesValidation=False CssClass="cmdBtnStyle" OnClick="btnOnClick"  id="btnMainCommand" Text='<%#DataBinder.Eval(Container.DataItem, "ButtonText")%>' runat="server" >
                                                </asp:button>
                                          </td>
                                    </TR>
                              </ItemTemplate>
                        </asp:repeater></table>

This control is then "dropped" on to defaultMain.aspx, within defaultMain is an iFrame called ifrmMain. At present the code below (with ctlCommand)  runs on the onlclick event for any of the buttons pressed in the user control :

    Public Sub btnOnClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim scriptString As String = "<script language=JavaScript>ifrmMain.src = 'defaultlogin.aspx';"
        scriptString += "</script>"

        If (Not Page.IsClientScriptBlockRegistered("clientScript")) Then
            Page.RegisterClientScriptBlock("clientScript", scriptString)
        End If

    End Sub

Hope this helps

Cheers

Rob
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12073725
so , the problem is with that this is add at serverside onclick event
try to use the repeater OnItemDataBound event with my second solution - client onclick

B..M
0
 

Author Comment

by:robert_f
ID: 12073752
BM

Forgive my ignorance :-) but I'm really quite new to .NET and dont really understand what it is your asking me to do ... could you clarify maybe with a code example ?

Cheers

Rob
0
 
LVL 5

Accepted Solution

by:
ajitanand earned 2000 total points
ID: 12082225
Hello,

Here is the solution:
-----------------------------------
<body>
<form id="Form1" method="post" runat="server">

<!-- User Control Begins --><table id="tblCommand">
<script>
function showURL(url) {
      window.frames["iFrame"].location.href = url;
      return void[0];
}
</script>
<asp:repeater id="rptLinks" runat="server">
<itemtemplate>
<tr>
<td align="center" nowrap>
<input type="button" class="cmdBtnStyle" id="btnMainCommand"
      value='<%#DataBinder.Eval(Container.DataItem, "ButtonText")%>'
      onclick='<%# "showURL(""" & DataBinder.Eval(Container.DataItem, "URLText") & """)" %>'; >
</td>
</tr>
</itemtemplate>
</asp:repeater>
</table><!-- User Control Ends -->


<iframe src="defaultIFRAME_URL.aspx" name="iFrame" width="500" height="100" frameborder="1"></iframe>

</form>
</body>

------------------------------------------------
Best part is that the event executes entirely on the client side, so no round trips required!


Let me know if this helps/does not help.

warm regards,
Ajit Anand
0
 

Author Comment

by:robert_f
ID: 12084530
Ajit

Thanks for the response. I dont have my main PC with me at the moment but will give this a try over the weekend.

Thanks

Rob
0
 

Author Comment

by:robert_f
ID: 12094995
Ajit

That worked like a dream and I especially like that fact that no roud trips are involved !!!

Thanks again

Rob
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question