Solved

iframe and parent page refresh

Posted on 2008-06-13
14
337 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
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)

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now