 |
| ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 23rd, 2011, 04:30 AM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
IsPostBack in Master Detail
My question is as follows. On the first page (master page) I have a dropdownlist and a gridview. The user chooses a value and the results are shown in the gridview. One field in the gridview is a link to a details page. When you click the link, you see the details. Nothing unusual so far.
Next I want to go back to the masterpage with a button or something and I want to keep the original value chosen by the user. I have tried everything (mainly IsPostback code), but nothing works. The masterpage shows the default value in stead of the chosen value.
Is there a solution for this problem? In the old days with classic asp this wasnot a problem at all. If you want more details let me know.
Thanks in advance for helping me out.
Robert
|
|

March 23rd, 2011, 06:03 AM
|
|
Authorized User
|
|
Join Date: Jan 2011
Posts: 86
Thanks: 1
Thanked 12 Times in 12 Posts
|
|
Hi,
is enableviewstate for your dropdownlist on ? It should be to remember the selected value.
is your dropdownlist populated by data binding ? Then it should help to exclude your databinding on postback or bind your data already in the Page_Init
|
|

March 23rd, 2011, 07:03 AM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
IsPostBack
Hi,
The enableviewstate of the ddl is true, so that's OK.
In the Page_Load event I have the following code:
Code:
If Not Page.IsPostBack Then
DropDownList1.DataBind()
End If
On your advice I copied this to the Page_Init event.
My ddl is populated by a database-query. The code:
Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connWielerDatabase %>"
SelectCommand="SELECT [Koersid], [Koersnaam] FROM [Koers] WHERE ([Koersid] < @Koersid) ORDER BY [Koersnaam]">
<SelectParameters>
<asp:Parameter DefaultValue="30" Name="Koersid" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<span id="keuzevak">Kies een koers:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="Koersnaam"
DataValueField="Koersid">
</asp:DropDownList></span>
.
All the things you said didnot make any difference. The ddl jumps back to the defaultvalue.
Grz,
Robert
|
|

March 23rd, 2011, 07:07 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
When you click the link, you see the details. Nothing unusual so far.
|
and
Quote:
|
Next I want to go back to the masterpage with a button or something and I want to keep the original value chosen by the user.
|
How are you linking to the details page and how do you send the user back to the previous page? And what does your button look like?
My guess is that you link with a normal href which results in a *get* request to the new page. Then IsPostBack won't be true for the initial page anymore, and the DropDown loses its state.
Can you show us the relevant bits of your code?
Imar
|
|

March 23rd, 2011, 08:09 AM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Hello Imar,
My answers to your questions:
Code linking to details page
Code:
<asp:HyperLinkField DataNavigateUrlFields="Koersid,Rennerid"
DataNavigateUrlFormatString="RanglijstWinnaarsDetails.aspx?Koersid={0}&Rennerid={1}"
HeaderText="Coureur" NavigateUrl="RanglijstWinnaarsDetails.aspx"
Text="Details....." DataTextField="Coureur" />
<asp:BoundField DataField="Totaal" HeaderText="Totaal" SortExpression="Totaal">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
Then code of the button back to masterpage:
Code:
<p id="plink" align="center">
<asp:LinkButton ID="LinkButton1" runat="server"
PostBackUrl="~/Klassiekers/KlassiekersKoersen/KlassiekersKoersRanglijstWinnaars.aspx"
BackColor="white" BorderStyle="Solid" Font-Bold="True" Font-Size="12px"
ForeColor="#284775" BorderWidth="1px" CssClass="label">Ga Terug Naar Master</asp:LinkButton>
</p>
The second chunk of code answer also the question how the button looks like.
Is this OK or do you need more code?
You could be right about the IsPostback status, but the next question would then be what's the alternative?
Grz,
Robert
|
|

March 23rd, 2011, 11:55 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Robert,
You have a few alternatives
1. Pass the ID in the query string and use it to go back. E.g.:
<a href="somepage.aspx?CategoryId=123">Ga terug</a>
Then in the master page you can use Request.QueryString.Get("CategoryId") and if it's not null, use the value to find and select the correct item in the drop down.
2. If you only visit one page (or at least a defined number of pages), you could do it with JavaScript:
<button onclick="history.go(-1);" value="Ga terug" />
FInally, you could maintain the state of the drop down in a session variable, a cookie or what have you.
How would you have solved this in classic ASP? With option 1?
Imar
|
|

March 23rd, 2011, 02:24 PM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
In Classic ASP (when things were simple and understandable) I used a submit button in a form and I used option 1 (?Koersid=<%=(objRS.Fields.Item("Koersid").Value)% > at the detailpage and Request.Querystring("Koersid") at the masterpage. I have not the faintest idea how to program that in ASP.NET.
On the detailpage: what comes after the ?Koersid=......... (binding to a column of the select statement?).
On the masterpage I tried this before the dropdownlist:
Code:
<%
If Request.QueryString.Get("Koersid") <> "" Then
DropDownList1.SelectedValue = Request.QueryString.Get("Koersid")
End If
%>
The effect is that the value in the ddl is OK, but the gridview keeps/returns to his or her default value.
Your suggestion for Javascript I translated as follows on the detailpage:
Code:
<input id="Button1" onclick="history.go(-1);" type="button" value="Ga terug" runat="server"/>
This has the same effect as the former option: the ddl is OK, but the gridview falls back to the default.
I am afraid that we have still no solution for this problem.
Any ideas?
Grz
Robert
|
|

March 23rd, 2011, 03:33 PM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Hello Imar,
I have got some second thoughts. When I do the following:
masterpage adding GridView1.DataBind() to the Request etc. code
detailpage adding PostBackURL = "filepath?Koersid=7"
the code works OK for ddl à nd Gridview.
Then I tried the next code:
detailpage PostBackURL = "filepath?Koersid=<%# Eval=("Koersid") %>"
I am getting a server error about a wrong serverlabel.
So the question remains how to fill in the PostBackURL.
I hope that this will brings things a bit further.
Grz,
Robert
|
|

March 23rd, 2011, 05:17 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
In Classic ASP (when things were simple and understandable)
|
I know where you're coming from.... ;-) .NET makes the hard things simple, and the simple things hard... ;-)
Rather than setting the SelectedValue directly, you could / should use ddl.Items.FindByValue and if that returns a non null value, you can set its Selected property. That way you protect yourself against old values in the query string that no longer exist.
Anyway, PostBackUrl serves a different purpose than what you're using them for here. You don't really need them.
If KoersId is in the query string, you can assign, for example, that value to a link the classic ASP way:
Code:
<a href="SomePage.aspx?KoersId=<%=Request.QueryString.Get("KoersId") %>">Ga terug</a>
Alternatively, you can use a Hyperlink or a LinkButton and set the ID through code. First add the control to the page:
Code:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="SomePage.aspx?KoersId={0}">Ga terug</asp:HyperLink>
and then assign it a value in Code Behind:
Code:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
HyperLink1.NavigateUrl = String.Format(HyperLink1.NavigateUrl, Request.QueryString.Get("KoersId"))
End Sub
Many ways to Rome, in ASP.NET. The trick is finding which one is the best....
Hope this helps.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

March 24th, 2011, 05:20 AM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Hello Imar,
Thanks a lot for your help: option 1 Ã nd option 2 both worked out properly. I also tried out your FindByValue suggestion as follows:
Code:
Dim lstItem As ListItem
lstItem = DropDownList1.Items.FindByValue(Request.QueryString.Get("Koersid"))
If Not lstItem Is Nothing Then
DropDownList1.SelectedValue = Nothing
lstItem.Selected = True
End If
Works OK too. My first code does the same trick I think, because Null-values or old values are not possible:
Code:
If Request.QueryString.Get("Koersid") <> "" Then
DropDownList1.SelectedValue = Request.QueryString.Get("Koersid")
GridView1.DataBind()
End If
The user must select a hyperlink to reach the details page (you cannot enter that page directly via menus), so koersid has always a value and it is always the right value because the most recent value is taken.
Again: thanks a lot for the many ways to Rome. I must get used to that....
Grz
Robert
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| master-detail |
hhpatek |
ADO.NET |
0 |
April 3rd, 2008 02:53 PM |
| Master Detail |
prasanta2expert |
Access VBA |
1 |
October 1st, 2007 06:37 AM |
| master/detail |
beeyule |
Dreamweaver (all versions) |
1 |
January 18th, 2005 02:59 AM |
| ADO Master/detail -Help! |
SaharaWizard |
VB Databases Basics |
2 |
July 24th, 2004 08:56 PM |
| detail-master pages |
katie456 |
Classic ASP Databases |
1 |
June 13th, 2003 04:16 AM |
|
 |