Solved

iframe and parent page refresh

Posted on 2008-06-13
14
344 Views
Last Modified: 2010-04-21
Please go through this thread:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23475072.html

I've used this code on submit button:
If Not (Page.IsStartupScriptRegistered("Test")) Then
          Dim temp As String
            temp = "<script language='Javascript'>"
            temp += " alert(""Successful;"");"
            temp += " var newElm = document.createElement('option');"
            temp += "  newElm.text ='" + txtcompany.Text
            temp += "';  newElm.value ='" + txtcompany.Text
            temp += "';  parent.document.getElementById('ctl00_ContentPlaceHolder1_ddl_ri_vname').options.add(newElm);"
            temp += " parent.document.getElementById('mydiv').style.display='none';"
            temp += "</script>"
            Page.RegisterStartupScript("Test", temp)
        End If


Now the only problem is that the data shows in the dropdown. I go to the next dropdown whose AutoPostBack is set to True. I choose an option from the dropdown. It postbacks to the server and the newly added data to the previous dropdown disappears although the value is in the database. How do I solve this? And why is this happening, when on postback it should be actually showing the new data?
0
Comment
Question by:digitalZo
[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
  • 9
  • 4
14 Comments
 
LVL 22

Expert Comment

by:neeraj523
ID: 21777381
I m not 100% sure.. But you can try filling the first drop down box on page load.. without checking isPostback condition

neeraj523
0
 
LVL 10

Author Comment

by:digitalZo
ID: 21777734
Well, it isnt really possible because if the dropdown which gets its data from the ifram is somewhere in the middle, and the user has to fill the details of the other dropdowns later, then i cant really force the user to fill the drodpown which is somewhere in the middle of the page, right?

that's not feasible either. i hope someone has a solution.
0
 
LVL 9

Accepted Solution

by:
codeclay earned 500 total points
ID: 21778634
Add a hidden field to your page .Then after you have added the item to the dropdownlist on client side, update the value of the hidden field also . Keep the key and value as comma(,) seperated values in the Hidden field . In the page load event in code behind ,

if (!IsPostBack)
        {
            if (!this.IsStartupScriptRegistered("Test"))
            {
                string temp;
                temp = "<script language='Javascript'>";
                temp += " alert('Successful;');";
                temp += " var newElm = document.createElement('option');";
                temp += " var state = document.getElementById('HiddenField1');";
                temp += "  state.value ='New Value,New Value'; ";
                temp += "  newElm.text ='New Value";
                temp += "';  newElm.value ='New Value";
                temp += "';  parent.document.getElementById('DropDownList1').options.add(newElm);";
                temp += "</script>";
                this.RegisterStartupScript("Test", temp);
            }

        }
        else
        {
            string strClientAddedValues = HiddenField1.Value;
            if (strClientAddedValues.Trim != String.Empty)//See if there is ant value in the hidden field
            {
                string[] str = strClientAddedValues.Split(new char[] { ',' }); // Now str contains the 1st Key Value Pair
                DropDownList1.Items.Add(new ListItem(str[0], str[1])); //Add the Key and value to the dropdown list
                HiddenField1.Value = String.Empty; //Reset the hidden field value
            }
        }

If you plan to add multiple items from the client side , Keep the values in the hidden field as Key1,Value1|Key2,Value2 etc   and in the code behind , before
string[] str = strClientAddedValues.Split(new char[] { ',' });  ,
do a string[] strAddItems = strClientAddedValues.Split(new char[] { '|' });
the iterate through all lines in strAddItems  and do these two lines
string[] str = strClientAddedValues.Split(new char[] { ',' });
                DropDownList1.Items.Add(new ListItem(str[0], str[1]));
And now out side the  loop clear the value of the hidden field .
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 10

Author Comment

by:digitalZo
ID: 21784139
i'll try your code but one question:

<<<Add a hidden field to your page .Then after you have added the item to the dropdownlist on client side, update the value of the hidden field also . >>>>

The thing is the IFrame is being called in at least 10 pages. So I'll have to create a hidden field for 10 pages and that code for all of them [and as the pages increase]?
0
 
LVL 9

Expert Comment

by:codeclay
ID: 21790983
I didnt get you 'The thing is the IFrame is being called in at least 10 pages' , add this code to the page where you have the dropdown .
0
 
LVL 10

Author Comment

by:digitalZo
ID: 21791315
<<<add this code to the page where you have the dropdown .>>>

Yep, that's what I wanted to ask. There are 10 pages where I have the dropdown, so will I have to repeat the coding everywhere? Or can I just add this code somewhere and call it when required.
0
 
LVL 9

Expert Comment

by:codeclay
ID: 21800232
Ok in that case you could create a custom page class  derived from System.Web.UI.Page and write the above code in that file . In those 10 pages where you need this functionality , dervie them from this custom page class . How ever note that we are refering to elements from the client side as  parent.document.getElementById('DropDownList1'). etc . So the expectation is that you have a drop down named 'DropDownList1' in all those 10 pages .
0
 
LVL 10

Author Comment

by:digitalZo
ID: 21811019
can u show me how to do it?
0
 
LVL 10

Author Comment

by:digitalZo
ID: 21820516
?
0
 
LVL 9

Expert Comment

by:codeclay
ID: 21820524
Im sorry I couldnt reply any sooner .I will be glad to help you out .Right now I am on a busy schedule . Please hold on .
0
 
LVL 10

Author Closing Comment

by:digitalZo
ID: 31466856
It'd have been nice if you had explained how to make Custom Page Class. :)
0
 
LVL 10

Author Comment

by:digitalZo
ID: 21820533
Sorry I already gave you the points and started a new thread here:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23498109.html
0
 
LVL 10

Author Comment

by:digitalZo
ID: 21820538
Thanks for all your help. :)
0
 
LVL 10

Author Comment

by:digitalZo
ID: 21820542
oops sorry forgot to say, that if you want, you can answer in that thread. i really need a solution quickly.
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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

734 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