BPruvost
BPruvost

Reputation: 503

Submit a form from code behind

I'm having trouble implementing a functionality on my c#/asp.net app.

I have a form with a RadioButtonList and a submit button. The RadioButtonList is generated on Page_Load() from a list of objects I retrieve from the database. I would like to automatically submit the form if there is only 1 object in the list.

I have access to my Form object, to the submit button etc... but I can't seem to find a solution (in the end I'm looking for a kind of form.Submit() ) method.

Does anyone have an idea of how I could do this ?

Thanks in advance !

EDIT >> Here is the code :

.aspx : http://pastebin.com/0E6T7dqH

.aspx.cs : http://pastebin.com/54payZJP

EDIT2 >>>

As it seems there is no way to do what I wanted to do at first, I ended up using a session variable with a response.redirect()

Source : http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspx

Upvotes: 4

Views: 14077

Answers (3)

SouthShoreAK
SouthShoreAK

Reputation: 4296

I actually had to do something similar, once. Here is a way you can do it.

Asp.Net buttons have a property called PostBackUrl, and it does exactly what you would expect - it controls where the form will post if you click the button.

You can also use the RegisterStartupScript function to render javascript on the page.

Now, with these two pieces, you can achieve your goal.

if(!IsPostBack)
{
    if(results == 1)
    {
        button.PostBackUrl = "next page's url"
        //Register script to click the button using RegisterStartupScript
    }
}

Now, having shown you this, I will warn you it may not make for the best user experience. When I did it, it was for a very specific case that had no other solution. The page will actually post back to the user, and they will see the page for a moment before the javascript to click the button takes effect. Additionally, when you set a button's PostBackUrl, that means that when it is clicked, your entire form will be posted to the page specified. The code behind for the current page will not fire at all, so if you have any validation, it won't run.

There's nothing wrong with letting the user click the button to submit the form even if they only have one choice. In my experience, users like to feel like they are in control on the system; they don't like it when pages just do things without their input.

Also, there is not really anything wrong with putting the information the next page needs into the session or even a database table, and using Response.Redirect. It's a fairly common practice and works reliably in most scenarios.

Upvotes: 2

Ivo
Ivo

Reputation: 8352

Post happens in the client side. As in Page_Load you are currently executing in the server side, just call the code you want to execute on post.

Edit: For actually going to another aspx

public void Page_Load(object sender, EventArgs e) {
    if(!IsPostback && OnlyOneItem) {
        Server.Transfer("TheOtherPage.aspx");
    }
}

Server.Transfer will maintain the entire request, so your post data will be available.

http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transfer.aspx

Upvotes: 3

huMpty duMpty
huMpty duMpty

Reputation: 14460

Try something like this

In your Page_Load

if(!IsPostBack)
{
      if(check for only one object)
      {
         //your submit code
      }
}

Upvotes: 2

Related Questions