Submit form from code

I am having trouble implementing the functionality of my C # / asp.net application.

I have a form with a RadioButtonList and a submit button. The RadioButtonList is generated Page_Load()

from a list of objects that I retrieve from the database. I would like to automatically submit a form if there is only one object in the list.

I have access to my Form object, submit button, etc., but I can't seem to find a solution (I'm looking for a way after all form.Submit()

).

Does anyone have any idea how I can do this?

Thanks in advance!

EDIT -> Here is the code:

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

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

EDIT2 ->>

There seems to be no way to do what I wanted to do at the beginning, I ended up using a session variable with a .redirect () response

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

+3


source to share


3 answers


I really had to do something like this one day. Here's how you can do it.

Asp.Net buttons have a property PostBackUrl

and it does exactly what you expect - it controls where the form will be placed if you click the button.

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

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



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

      

Now that I have shown this to you, I will provide you with a warning that it may not provide a better user experience. When I did this, there was no other solution for the specific case. The page will actually send back to the user and they will see the page momentarily before the javascript clicks the button take effect. Also, when you set the PostBackUrl button, it means that when you click on it, your entire form will be sent to the specified page. The code for the current page won't fire at all, so if you have any validations it won't work.

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

Also, there is nothing wrong with putting the information needed by the next page into a session or even a database table and using Response.Redirect

. This is a fairly common practice and works reliably in most scenarios.

+1


source


The message happens on the client side. Just like the Page_Load you are currently executing on the server side, just call the code you want to execute in the post.

Edit: to navigate to another aspx

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

      



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

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

+2


source


Try something like this

IN Page_Load

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

      

+2


source







All Articles