Ok folks. I just found a cool Button property called Button.PostBackUrl . It works fine when you set this property and click on the button -- it takes you the page specified in the PostBackUrl property. At first glance, it looked very promissing for cross-page coding.

Here's my problem... I have a GridView in my web form. I've added a column of ButtonField controls to the grid in hopes that I can pass control to a different page by setting the PostBackUrl property, alas the ButtonField control doesn't have this property.

So I went on to put a Button in my code and set it's PostBackUrl to my target page. It properly gets sent to the page if I click on the button, but when I programmitically invoke it's Button_Click event handler, the code doesn't route me to the page specified in the PostBackUrl property. The idea being that I can fire the Button_Click event from the GridView 's ButttonField event handler. Bottom line, the code doesn't get routed to the PostBackUrl page when programmitically invoking the Button_Click event.

Can anyone help:?:

I just ran a test to see if my timing is correct. I believe I've got it right, but I'm not sent to the page specified in the PostbackUrl. (I can get there if I click on the button, but if I fire the Click event via code, It doesn't get me there.)

Here's some code... Clicking on a radio button should set the PostbackUrl property and fire the Button1_Click event. But...

default.aspx

<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="PassParameters._Default" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<hr />
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Value="http://www.daniweb.com/techtalkforums/post314320.html">Daniweb</asp:ListItem>
<asp:ListItem Value="~/test.aspx">Uke</asp:ListItem>
</asp:RadioButtonList>
</div>
</form>
</body>
</html>

default.aspx.cs

using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace PassParameters {
public partial class _Default:System.Web.UI.Page {
 protected void Page_Load(object sender, EventArgs e) {
    }
 protected void RadioButtonList1_SelectedIndexChanged(object sender
    , EventArgs e) {
      // Set the destination.
      Button1.PostBackUrl = RadioButtonList1.SelectedValue;
 
      //This should send me to a different page right?
      Button1_Click((object)Button1,e);
    }
 protected void Button1_Click(object sender, EventArgs e) {
    }
  }
}

Yes, and for me, it does.
You're inheriting code. Did you mean to do that?

Inherits="PassParameters._Default" %>

You had a couple other errors in there I fixed without thinking about it, too.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5"  %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<hr />
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Value="http://www.daniweb.com/techtalkforums/post314320.html">Daniweb</asp:ListItem>
<asp:ListItem Value="~/test.aspx">Uke</asp:ListItem>
</asp:RadioButtonList>
</div>
</form>
</body>
</html>

Inherits="PassParameters._Default"

PassParameters is my namespace. I actually have a lot more code, but I stripped out everything and left only the relevant stuff. Sorry if this caused any confusion - I should have made my sample code more clear.

It also looks like my code didn't paste correctly. Are the errors you are refering to in the DOCTYPE, html and head tags?

Did I err anywhere else? Can you post your code in default5.aspx.cs?

Here's what I had.
Note that posting to the daniweb address actually causes an error due to them not accepting POST from outside their domain. However, this still indicates success at postbackurl.
Also, I choose an option before clicking that button.
This is .NET 2.0, which I assume is what you're using, because IIRC 1.1 doesn't support this sort of thing.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5"  %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<hr />
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Value="http://www.daniweb.com/techtalkforums/post314320.html">Daniweb</asp:ListItem>
<asp:ListItem Value="~/test.aspx">Uke</asp:ListItem>
</asp:RadioButtonList>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Set the destination.
        Button1.PostBackUrl = RadioButtonList1.SelectedValue;

        //This should send me to a different page right?
        Button1_Click((object)Button1, e);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    }
}

Hmmm. There's a little bit of confusion here. I'm trying to get my page to redirect by firing the Button1_Click event handler from within the radio button's event handler. My goal is to click once and get redirected to my destination. (Not make the radio button selection and then having to click on the button.)In past projects, I've used Server.Transfer("someurl.aspx",true); to do this, but I was trying to get this done by using the PostbackUrl property instead.

In my real code, I'm populating a GridView control which has a ButtonField column. If the list is long then the user will have to do a lot of scrolling in order to select the correct record and then a lot of scrolling to click on the button.

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Set the destination.
        Button1.PostBackUrl = RadioButtonList1.SelectedValue;
 
        //This should send me to a different page right?
        Button1_Click((object)Button1, e);
    }

Okay, no, that won't work like that.
Behind the scenes, all post back url does is change the onclick on the button CLIENT SIDE.
So changing it then trying to call onclick, before the code is written to the browser, won't work.
Here is view source from the browser.

onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Button1&quot;, &quot;&quot;, false, &quot;&quot;, &quot;http://www.daniweb.com/techtalkforums/post314320.html&quot;, false, false))" id="Button1" />

So, to solve your problem, you can still use transfer, or you can register client side script block to auto-call the button click event when the page loads.
Transfer is, in my opinion, the better option.

A-ha! So it just creates a client side event handler in the background. That's good to know; as you suggest, I'll stick the Server.Transfer method.

Thanks for your help, nikkiH! Have a nice day "near Chicago".

Even I'm not an expert in this PostBackURL property. But in your case you could try

Response.Redirect( "[I]your form name[/I]");

Anyone who knows the different between PostBackURL and Response.Redirect???

Greetings c# seeker,

There is a difference. Using Response.Redirect("url"); will get you to the next page, but it doesn't retain any form variables. PostbackUrl does retain form variables; you access them by using the PreviousPage property.

Note: Server.Transfer("url",true); also retains form variables. This is what I ended up using.

Here's an example of using the PreviousPage property. (Not available when using Response.Redirect(url); .

string test;
if (Page.PreviousPage != null) {
    HiddenField tmpHiddenField = (HiddenField)Page.PreviousPage.FindControl("HiddenField1");
    if (tmpHiddenField != null) {
        test = tmpHiddenField.Value;
    }
}
This question has already been answered. Start a new discussion instead.