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" />
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("ctl00$MainContent$btn_Search", "", false, "", "Index.aspx", 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("ctl00$MainContent$btn_Clear", "", false, "", "Index.aspx", 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 <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" />
<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;
}
}
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