0

I've got a search page which shows a gridview. Items in the gridview are hyperlinks which create url string back to the same page - index.aspx

I've got index.asp checking for the presence of query string and deals with it just fine.

The problem i've got is when the user wants to enter a new search which is a form-submit. However the post back still includes the previous url query string - so that part of my code handles the request not the search.

Looking around I discovered PostBackURL:

  <asp:Button ID="btn_Search" runat="server" Text="Search" Cssclass="form_btn" OnClick="btn_Search_Click" PostBackUrl="index.aspx" />&nbsp;

In testing this did the trick. Form is submitted to index.aspx not to say index.aspx?Var1=1&Var2=2

When I publish to the live site however, this doesn't work. I still get posted back with the url string.

The code outputted by asp for both the button and the javascript code are identical.

I'm fairly new to asp but i'm puzzled that the behaviour is different when running debug in VS2010 and running live on a webserver.

--

Ok some further investigation. I'm running Cassini but.. there are two buttons on my form - one is a search the other clear.

Both buttons generate the same sort of html:

<input type="submit" name="ctl00$MainContent$btn_Search" value="Search" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$btn_Search&quot;, &quot;&quot;, false, &quot;&quot;, &quot;Index.aspx&quot;, false, false))" id="MainContent_btn_Search" class="form_btn" />

<input type="submit" name="ctl00$MainContent$btn_Clear" value="Clear" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$btn_Clear&quot;, &quot;&quot;, false, &quot;&quot;, &quot;Index.aspx&quot;, false, false))" id="MainContent_btn_Clear" class="form_btn" />

The only difference in the above is the name, value, and the function they will call at the server - btn_clear or btn_Search.

The clear button works with the postbackurl on the live site; search button doesn't.

Also, once a page has been served, is it not up to the page then to determine where it posts data next ? So if the page says i'm going here, that's where it goes. My clear button says goto index.aspx with no url query string, and it works.

This puzzles me - if the clear button didn't work on the live site I would think it's either something to do with cassini or that in development it's one pc vs live has pc & server. Also how can this be a cassini/IIS issue if it's the browser that issues the command on where to go next ?

If I want to look at this further, I may need to use wireshark or similar to look at what actually leaves my browser window when I press search or clear.


Ok test project done. I've verified that this code works as I want it in debug. I can't test the live deploy to IIS until Monday.

page1 aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="page1.aspx.cs" Inherits="page1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False">
    <Columns>
    <asp:HyperLinkField DataNavigateUrlFields="intVal" 
            DataNavigateUrlFormatString="page1.aspx?Var={0}" DataTextField="intVal" 
            HeaderText="URL String" />
    </Columns>
</asp:GridView>
<br />
<br />
Search &nbsp;<asp:TextBox runat="server" ID="searchtext"></asp:TextBox><br />

<asp:Button ID="btn_Search" runat="server" Text="Search" OnClick="btn_Search_Click" PostBackUrl="page1.aspx" />&nbsp;
<asp:Button ID="btn_Clear" runat="server" Text="Clear" OnClick="btn_Clear_Click" PostBackUrl="page1.aspx" /><br />
Result:<br /><br />
<asp:Label runat="server" ID="result">Initial Load</asp:Label>

</form>
</body>
</html>

page 1aspx.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class page1 : System.Web.UI.Page
{

public class MyItem 
{
    public int intVal {get; set;}
    public string stringVal {get; set;}
}

List<MyItem> ListMyItem = new List<MyItem>();

protected void Page_Load(object sender, EventArgs e)
{
    BuildList();


    result.Text = "Page Load: " + Request.Url;
}

protected void BuildList()
{
    MyItem MI = new MyItem();
    MI.intVal = 1;
    MI.stringVal = "111";

    ListMyItem.Add(MI);

    MI = new MyItem();
    MI.intVal = 2;
    MI.stringVal = "222";

    ListMyItem.Add(MI);

    gridview1.DataSource = ListMyItem;
    gridview1.DataBind();

}

protected void btn_Clear_Click(object sender, EventArgs e)
{
    result.Text = "Clear, URL: " + Request.Url + " Search= " + searchtext.Text;
}
protected void btn_Search_Click(object sender, EventArgs e)
{
    result.Text = "Search, URL: " + Request.Url + " Search= " + searchtext.Text;
}
}
3
  • Can you show me more code in your default.aspx page atleast all the code in the form tag
    – JohnnBlade
    Jul 20, 2012 at 5:59
  • I'm curious to get to the bottom of this but there is a lot of unrelated code to this problem. Over this weekend i'll create a page1.aspx and page2.aspx sample with the bare code needed and see if I can replicate the issue. I'll post that in it's entirety - it should be pretty short.
    – andrew
    Jul 20, 2012 at 14:36
  • My new code works live & test. I think there must have been something else going on on the page that caused the problem. I also re-worked my original page so it didn't need url string so solved that way too.
    – andrew
    Jul 23, 2012 at 6:42

1 Answer 1

0

Its one or the other Remove OnClick="btn_Search_Click" if you dont need to do anything in your code behind or remove PostBackUrl="index.aspx" if you dont want to be redirected

you could ofcourse also use OnClick="btn_Search_Click" and then in code behind do a redirect to index.aspx

7
  • 1
    But why does it work in debug but not live ? Before I had postbackurl both debug and live didn't do as I wanted; but add in the postbackurl and debug version works ?
    – andrew
    Jul 19, 2012 at 9:35
  • Did you run it Cassini, the server built into visual studio, or on a local IIS
    – JohnnBlade
    Jul 19, 2012 at 9:38
  • It's probably Cassini - when I debug I get a new process "ASP.NET Development Server - Port 53800". Differences between IIS and Cassini then ?
    – andrew
    Jul 19, 2012 at 9:59
  • Have a look here stackoverflow.com/questions/103785/…
    – JohnnBlade
    Jul 19, 2012 at 10:17
  • I thought on this overnight and have added more comments in the question. It very well could still be cassini vs IIS but I still don't understand yet.
    – andrew
    Jul 20, 2012 at 0:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.