Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

iframe and parent page refresh

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
digitalZo
Asked:
digitalZo
  • 9
  • 4
1 Solution
 
neeraj523Commented:
I m not 100% sure.. But you can try filling the first drop down box on page load.. without checking isPostback condition

neeraj523
0
 
digitalZoAuthor Commented:
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
 
codeclayCommented:
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
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!

 
digitalZoAuthor Commented:
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
 
codeclayCommented:
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
 
digitalZoAuthor Commented:
<<<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
 
codeclayCommented:
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
 
digitalZoAuthor Commented:
can u show me how to do it?
0
 
digitalZoAuthor Commented:
?
0
 
codeclayCommented:
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
 
digitalZoAuthor Commented:
It'd have been nice if you had explained how to make Custom Page Class. :)
0
 
digitalZoAuthor Commented:
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
 
digitalZoAuthor Commented:
Thanks for all your help. :)
0
 
digitalZoAuthor Commented:
oops sorry forgot to say, that if you want, you can answer in that thread. i really need a solution quickly.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now