Solved

Using Codebehind to communicate between frames

Posted on 2004-08-04
12
1,375 Views
Last Modified: 2008-02-01
Hello,

Lets say I have a Framset that has a top, framtop.aspx and a botton, frame bottom.aspx.  In frame top, I have a button that executes a query and I want to put into frame bottom the results of that query in a data grid.  I want to do all of this in the framtop code behind.

LEts say my DatGrid is called DG4

How do I do that? What is all of the code nescessary that I have to put in my framtop to communicate with my fram bottom.


Bonus points of 50 if you can tell me how if my frambottom is initially 0% that when the button clicks the bottom fram goes to 30%.

Thanks
0
Comment
Question by:dmontgom
  • 7
  • 5
12 Comments
 
LVL 4

Expert Comment

by:Kittrick
Comment Utility
You want to use the server.transfer method in asp.net to send the info to other pages.

An example can be found at:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20859069.html?query=asp.net+server.transfer+method&clearTAFilter=true

Hope I helped!
Kittrick
0
 
LVL 4

Expert Comment

by:Kittrick
Comment Utility
I'm going to take a stab at the 0 to 30% problem, and say it could be one of two things:

1) Your frame that contains the button is set to size to whatever is left over which happens to be 30%

example:

<html>

<frameset cols="50%,20%,*">

  <frame src="tryhtml_frame_a.htm">
  <frame src="tryhtml_frame_b.htm">
  <frame src="tryhtml_frame_c.htm">

</frameset>

</html>

the * denotes that it will resize to whatever is left over as a remainder...which in this case, would be 30%


2) The frame is actually sized at 30% of the screen (substitute the * above with a literal 30%).

Those are the only two things I could think of.

Kittrick


0
 

Author Comment

by:dmontgom
Comment Utility
Thanks.  I am looking for exact code.  THis is what i tried with the link but it does on work

In the top portion of the frame when I click the submit button I have

        Dim objConn As New OleDbConnection("Provider=""xxx"";password=xxx;User ID=xxx;Data Source=xxx")
        Dim objCmd As New OleDbDataAdapter("Select * from eval_ad_final", objConn)
        Dim ds As DataSet = New DataSet
        objCmd.Fill(ds, "eval_ad_final")
        objConn.Close()

        Server.Transfer("outcome_results.aspx", True)


In the bottom portion of my frame I have

        Dim ds
        Dim temp

        Response.Redirect("outcome_results.aspx?ds=ds")
        temp = Request.QueryString("ds")
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()


My question is 1) where do I put the code in the bottom portion of the frame.  I it on page load?  What is the subroutine it should be in that when the button is executed in the top portion of the frame, the data source created in the top portion of the frame is then automatically bound to the DataGrid in the bottom portion of the frame.  Also, the page did redirect but it redirected in the top portion of the frame.

What should the exact code be?

Thanks





0
 

Author Comment

by:dmontgom
Comment Utility
Actually this is my code in the top frame

Response.Redirect("outcome_results.aspx?myvariable=ds")

To grab the data source (ds) in the botton frame I use

 DataGrid1.DataSource = Request.QueryString("myvariable")

It does not want to work.  I just want the bottom frame to load the data generated in the first frame.  However, when I use Response.Redirect("outcome_results.aspx?myvariable=ds") the top frame is replaced by the page that I want to be the bottom frame

 
0
 
LVL 4

Expert Comment

by:Kittrick
Comment Utility
I think I know what your problem is. I did the same thing you want to do, EXCEPT I had two completely seperate pages....one to send the data and one to catch it from the first page. When you are trying to get your data, it is updating the same frame because it is the same page. Create a second aspx page and name it whatever you want.  Here's your code to illustrate:


'initiating the OleDB data connection

Dim objConn As New OleDbConnection("Provider=""xxx"";password=xxx;User ID=xxx;Data Source=xxx")
        Dim objCmd As New OleDbDataAdapter("Select * from eval_ad_final", objConn)
        Dim ds As DataSet = New DataSet
        objCmd.Fill(ds, "eval_ad_final")
        objConn.Close()


'transferring data to the new page and saving state of current page in transit
        Server.Transfer("mynewpage.aspx", True)


'mynewpage is called in the bottom frame and receives the data

        Dim ds
        Dim temp

        Request.Form("ds")
        temp = Request.QueryString("ds")
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()



Here's an interesting excerpt from an article I found:

-----------------------------------------------------------------------------------------
Server.Transfer Vs. Response.Redirect
http://www.developer.com/net/asp/print.php/3299641
The Server.Transfer method also has a second parameter—"preserveForm". If you set this to True, using a statement such as Server.Transfer("WebForm2.aspx", True), the existing query string and any form variables will still be available to the page you are transferring to.

For example, if your WebForm1.aspx has a TextBox control called TextBox1 and you transferred to WebForm2.aspx with the preserveForm parameter set to True, you'd be able to retrieve the value of the original page TextBox control by referencing Request.Form("TextBox1").

This technique is great for wizard-style input forms split over multiple pages. But there's another thing you'll want to watch out for when using the preserveForm parameter. ASP.NET has a bug whereby, in certain situations, an error will occur when attempting to transfer the form and query string values. You'll find this documented at http://support.microsoft.com/default.aspx?id=kb;en-us;Q316920.

--------------------------------------------------------------------------------------

Hopefully this works, but I think honestly what you are trying to achieve is more difficult than it should be.

Just curious, is there a specific reason that you want to do this in frames and not in <asp:panel> 's?? Panels seperate things on a page without having to transfer things between pages. You could put your datagrid in a seperate panel than your query fields and even toggle visibility.  

I will write back tomorrow with an easier specific solution you may be interested in.

Kittrick
0
 

Author Comment

by:dmontgom
Comment Utility
Hi

Thanks for the help.  Will try and be more specific

In my top frame, I have the following that is the last line of code in a button_onclick command
Server.Transfer("outcome_results.aspx", True)  


In the page load of the bottom frame on the page load event in the vb code behind I have

        Dim ds
        Dim temp

        Request.Form("ds")
        temp = Request.QueryString("ds")
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()


In the Request.form("ds") line the ds has the squigly line under it like the vb code does not know what ds is?  is the Request.Form("ds") line correct?  BTW, I am importing on the second frame the code from the first page by using <%@ Reference Page="outcome.aspx" %> on the second page.

Also, yeah, this is a true pain. I really can't believe it is this difficult. Changing little stuff gets the form to pop but in a full page window.  I get know error but the again, i still do not get the data.

As far as what I want to do?  THis is the issue.  I have three grid controls side by side and the user checks options in each of the three grids and clicks a button.  THe button the executes a query and then returns the data to DataGrid.  TO make it look nice, I wanted the data grid to return to a bottom portion of the frame so the user can still see the options used above because users can change the above options on the fly and see updated changes below.  THe other issue as that the number of records for the three above data bound grids above can change thus making the number of rows variable.   I do not want to allow paging on the data grid.

If I can't get the frames to communicate properly then I will try grids.  Any examples on how to add e.g. two grids to a panel and then have a user buttone to switch between grids using a button or check box?

Thanks for the help





0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 4

Accepted Solution

by:
Kittrick earned 160 total points
Comment Utility
Hi again,

I have a simplistic example that you might be interested in. What it does:

1) it searches from a textbox in one panel and sends the data to a datagrid in another panel

2) by checking a box, you can narrow the fields in your datagrid to only the fields you want to see

3) if you clicked on the "Details" button, you get the info ready to update in two textboxes in  a third panel.

To use this code:

1) Create a folder from the root of your webserver and call it db.

2) Create a MS access database and call it "database" (no quotes)

3) Create two fields and set call them "field1" and "field2" (no quotes) and set them to type text

4) Create a third field, call it "id"  and  set the type to "autonumber"

5) Fill in each field with a word for maybe 2 records.

6) In your webbrowser, type in the search field for the id of  a record you entered in the database.

Here's the code:


<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="Microsoft.VisualBasic" %>

<script language="vb" runat="server">


'----------------------------START PAGE LOAD----------------

sub Page_Load(sender as Object, e as EventArgs)

If Not Page.IsPostBack
searchpanel.visible=true
showhidefieldspanel.visible=true
bindpanel.visible=false
makemoreroompanel.visible=false

End If


If page.IsPostBack = true
searchpanel.visible=true
showhidefieldspanel.visible=false
bindpanel.visible=true
makemoreroompanel.visible=true

End if

End Sub


'-----------------------------END PAGE LOAD------------------


'-----------------------------START BIND DATA-----------------

Public Sub BindData(sender as Object, e as EventArgs)

Dim oleConn As New OleDB.oleDbConnection()
oleConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("\db\database.mdb")

Dim oleComm AS NEW OleDb.oleDbCommand()

oleComm.Connection=oleConn

oleComm.CommandText="SELECT * FROM databasetable WHERE id=" + field1_search.text

dim da as New OleDb.OleDbDataAdapter()
Dim ds as New DataSet()

da.selectCommand=Olecomm



 try
    Response.Write("Hey, this thing is working!")

    da.Fill(ds,"databasetable")
    mydatagrid.datasource=ds.Tables("databasetable")
    mydatagrid.databind()

    Catch er as Exception
    Response.Write("Error: " + er.Message)

    end try



'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'The following code is what makes the fields show up/hide from view.
'
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

'Query only visible toggle. If checked, show only query, no other panels.

if queryonly_checkbox.checked then
response.write("queryonlybox is checked")
SearchPanel.Visible=false
BindPanel.Visible=false

'If Query only box is not checked, show all panels.

else
response.write("queryonlybox is NOT checked")
SearchPanel.Visible=true
BindPanel.Visible=true
end if

'If details_ckeckbox is checked...hide the details button
'You would want to do this only if you wanted to see the query for printing purposes.

if details_checkbox.checked then
response.write("detailsbox is checked")
mydatagrid.Columns(0).Visible=false

else
response.write("detailsbox is NOT checked")
mydatagrid.Columns(0).Visible=true
end if


if field1_checkbox.checked then
response.write("field1 is checked")
mydatagrid.Columns(1).Visible=false

else
response.write("field1 is NOT checked")
mydatagrid.Columns(1).Visible=true
end if


if field2_checkbox.checked then
response.write("field2 is checked")
mydatagrid.Columns(2).Visible=false

else
response.write("field2 is NOT checked")
mydatagrid.Columns(2).Visible=true

end if

oleConn.Close()

end sub


'------------------------------END BIND DATA-----------------------



'------------------------------START VIEW DETAILS-----------------

'This subfunction will post the data selected on the page in the datagrid and put it in textboxes for editing,
' by calling the array space in the database
Sub ViewDetails(sender as Object, e As DataGridCommandEventArgs)

'associating the colums with the values within by cell references based on order of fields on the page
    Dim buttonColumn as TableCell = e.Item.Cells(0)
    Dim field1Column as TableCell = e.Item.Cells(1)
    Dim field2NumColumn as TableCell = e.Item.Cells(2)
       



'declaring conversion variables and converting the cells into strings, numbers dates etc.    
   
    Dim field1ColText as String = field1Column.Text
    Dim field2NumColText as String = field2NumColumn.Text
   
'declaring an interim variable to post back to the page as a reference based on id of label
   
    field1.Text = field1ColText
    field2.text= field2NumColText
   

'-------------------------------END VIEW DETAILS---------------------

end sub


</script>


<!---This panel will search your database using a button and will search a field for a value--->

<form runat="server">

<table>


<asp:panel id="searchpanel" runat="server">


<tr>


SEARCH field1 for:
<asp:textbox id="field1_search" runat="server"/><br/>

<asp:button id="searchbutton" text="Search" runat="server" OnClick="BindData"/>


</tr>

</asp:panel>



<asp:panel id="showhidefieldspanel" runat="server">
<table border=1>

<tr>

<td colspan=20>

<center><b><u>Choose fields to hide when running the query</u></b></center>

</td>
</tr>


<tr>
<td colspan=20>
<asp:checkbox id="queryonly_checkbox" runat="server" value="queryonly"/>
View Query Results only (No edit boxes or search fields visible)

</td>
</tr>

<tr>

<td>
Details (Button)
</td>

<td>
field1
</td>

<td>
field2
</td>

</tr>

<tr>

<td>
<asp:checkbox id="Details_checkbox" runat="server" value="Details" />
</td>

<td>
<asp:checkbox id="field1_checkbox" runat="server" value="field1"/>
</td>

<td>
<asp:checkbox id="field2_checkbox" runat="server" value="field2"/>
</td>



</tr>

</table>


</asp:panel>



<asp:panel id="bindpanel" runat="server">

<tr>


<td valign=top>
<!---posts the value of the references to be posted from ViewDetails--->
      
<!-----start inline table--->

<table border=1>
<font face="arial" size="1">
<tr>
<td>
      <b>field1</b>:

</td>

<td>
<b>field2:</b>
</td>


</tr>

<tr>
<td>
      <asp:textbox id="field1" runat="server" font-size="8" maxlength=8 size=6/>
</td>
     
<td>
   <asp:textbox id="field2" runat="server" font-size="8" maxlength=2 size=1/>
</td>


</tr>
</font>      
 
 <asp:button id="button1" text="Update" runat="server" /> <asp:button id="button3" text="Add" runat="server" /><br/>


</table>

<!------end inline table--->

</td>

</tr>

</asp:panel>

<asp:panel id="makemoreroompanel" runat="server">

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>




</asp:panel>




<tr>
<td valign=top>
         <asp:DataGrid runat="server" id="mydatagrid"
         Font-Size="7pt" AutoGenerateColumns="false"
                      OnItemCommand="ViewDetails">
        <HeaderStyle BackColor="Black" ForeColor="White" Font-Bold="True" HorizontalAlign="Center" />
        <AlternatingItemStyle BackColor="lightGray" />


             
        <Columns>



  <asp:ButtonColumn Text="Details" HeaderText="Details" ButtonType="PushButton" commandname="Details"/>
                        <asp:BoundColumn DataField="field1" HeaderText="field1" visible="true"/>
                <asp:BoundColumn DataField="field2" HeaderText="field2" visible="true"/>
               
        </Columns>
      </asp:datagrid>

</td>




</tr>



</form>


Hope this helps!
Kittrick       

 
0
 

Author Comment

by:dmontgom
Comment Utility
My last question I hope

Got the datagrid to work dynamically.

Rather than reloading the query on every postback, is it possible to create a datatable that lasts throught the sesssion without having to recreate the table?  This posback is really starting to work against me becuase now every time I postback, I loose the checkboxes in the data drid that people check and I have know if keeping track of it.  If I can fill a table once, and call that table between sessions, then I am happy.

But, it seems like I loose the table as well.


Dim DataGridpl As New DataTable("Placement")

I want to fill DataGridpl just once and the have it persist through postback.  Possible?  It seems not

Thanks
0
 

Author Comment

by:dmontgom
Comment Utility
Panels worked great.  Lovel them

Ignore the last comment I made.  This is the very last thing I have to do.

I hae checkboxes in the grids in my panel.  When I switch between grids I loose what the user checked.  I switch between grids in a panel using a radio list box.  What I want to do is write to a database table what the user checked before the information on what the user checked is lost.  What event fires when a change to the second grid using the radio control button is selected but I can still capture the information in datagrid 1.

I tried radiobuttonlist_onselectionchanged but did not work

ANy ideas?
0
 

Author Comment

by:dmontgom
Comment Utility
I now hate the data grid control.  WOrthless whwn dealing with checkboxes and postbacks.  I can't get the damn checkboxes back.  I get everything else back but that is just because I rebind everytime.  No real solution there.

Thanks for the help
0
 
LVL 4

Expert Comment

by:Kittrick
Comment Utility
Sorry for writing back so late. Yes, datagrids are a serious pain because the .NET framework assumes a lot of control and gives you very little to customize. For what you want to do, you might want to look into the <datalist> with a <repeater>, but it requires you to explicitly write each and every behaviour that is built into a datagrid.  You can, however, mix JavaScript and Asp.net together to get pop-up windows or something that may not be affected by the postback. This is the biggest problem with ASP.NET in that EVERY OBJECT has to be tied to the server on postback in order for it to work at all.


Kittrick
0
 

Author Comment

by:dmontgom
Comment Utility
Total Pain but I got everything to work now by using multipanels.  Nesting panels in panels.  Just a pain to program around keeping track of them and what should be visable or not.

Thanks for the input on panels, big help
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mutually exclusive checkbox in a gridview 18 40
Paging GridView 7 32
How can I use this extension method? 8 35
Asp.net mvc 5 5 15
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

10 Experts available now in Live!

Get 1:1 Help Now