Solved

iframe and parent page refresh

Posted on 2008-06-13
14
338 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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

13 Experts available now in Live!

Get 1:1 Help Now