How to submit data twice on an ASP.NET page - asp.net

I have a webpage. At the top is a search bar that is inside a <form id="form1" runat="server">.
I also want to add a form on that same page that would allow users to register their details. Problem, ASP.NET only allows one form per page.
How can I achieve my goal? Any workarounds?

You can only have one server side form on a page.
If it is an option, you can have a client side form (without runat="server") with a separate action - this POST can go to a different page, where you will have to accessRequest.Form` to retrieve the values posted.
Another option is to use separate buttons for posting with different event handlers.

You can use the simple HTML form approach but there is the problem of always post the whole page back. or use mvc:
Are multiple forms on a single page supported in asp.net 3.5?

Put the code you want to be called in different button click events. Therefore, if the search button is clicked, only the code in the search buttons click event is run. If the register button is clicked, only the code in the registers click event is run.
Here is an example:
protected void Page_Load(object sender, EventArgs e) {
// Common code
}
protected void btnSearch_Click(object sender, EventArgs e) {
// Search code
}
protected void btnRegister_Click(object sender, EventArgs e) {
// Register code
}
Double clicking the buttons in the designer will create the click events in the code behind.

As already stated in the answer by #Simon, it's easy to have multiple click handlers in your code-behind to process exactly what you need on the page - this is the easiest way to solve the "lack of multiple forms" issue.
Something very useful in this situation is the DefaultButton attribute of the <asp:Panel> control. This means that should you have multiple areas of your page with (for instance) <asp:TextBox> controls, and each of those areas has a specific <asp:Button> associated, if the focus is in one of the textboxes then pressing Return or Enter will result in the DefaultButton being clicked.
For example...
<asp:Panel runat="server" DefaultButton="btnSearch">
Search: <asp:TextBox runat="server" id="txtSearcn"/>
<asp:Button runat="server" id="btnSearch" Text="Search" OnClick="btnSearch_Click"/>
</asp:Panel>
See MSDN for more information on the DefaultButton attribute of <asp:Panel>

Related

Registering a user control always postback hen the form loaded

Hi all I have created a user control and registered it in web.config with some extension and calling it on the page where ever required. When I use that when ever the page get loaded the user control also getting loaded, i mean the post back event of the user control or the page load event occurs as per the operations made in web page.
Is there any way to avoid this in such a way that only the user control page load should enable or load when ever some operations performed on that control when loaded.
For example I have created a user control and registered on my form, this will get loaded on button click of the form, I would like to make the post back of user control only when user click on the button of the web form but not on every operations performed
Edit as per jadarnel27 answer
<%# Register Src="~/UserControl1.ascx" TagName="TimeoutControl" TagPrefix="uc1" %>
<asp:Panel ID="yourPanelControl" runat="server" Height="200px" Width="300px">
</asp:Panel>
<dx:ASPxButton ID="btn" Text="user" OnClick="btn_Click" runat="server">
</dx:ASPxButton>
protected void btn_Click(object sender, EventArgs e)
{
TimeoutControl tc = new TimeoutControl();
yourPanelControl.Controls.Add(tc);
}
But I am unable to see the postback event fires on page load of user control as per ur code
When the ASP.NET Page loads, it recursively calls the Load function of all the controls in the Page.Controls collection (see the "Life Cycle events" section of this MSDN article on The ASP.NET Page Life Cylce, specifically the "Load" event). Including the UserControl in the markup of your page will cause it to be part of this collection, so the load event will fire whenever the Page loads.
If you want to avoid this, you need to add the UserControl to the Page dynamically in response to the Button's click event. You could, for instance, include some kind of Panel in your Page, then add the UserControl to that Panel when you click the Button.
Something like this:
protected void yourButton_Click(Object sender, EventArgs e)
{
// Create your UserControl
yourUserControl uc = new yourUserControl();
// Add it to the Panel you included in your markup
yourPanelControl.Controls.Add(uc);
}
The markup for a Panel would be something like this:
<asp:Panel id="yourPanelControl" runat="server" Height="200px" Width="300px">
</asp:Panel>

Good programming practice for Postbacks

newbie to asp.net here. I am building a reporting tool and trying to handle logic on one single page so that report A is one aspx page and so and so forth.
Therefore, many controls / queries actually do not need to get executed on first visit of the page, where only parameters, such as dates, columns, are needed as input.
On clicking a button, or postback i suppose, these controls whether in charts or gridview form would generate. Back in the old dates when I did some ASP programming, I would put all the controls, codes within a if block is see if it is a postback. Is there a more efficient way in doing so in ASP.net?
Thank you all.
Assuming ASP.NET WebForms:
If you need your code to be ran when a button is clicked, you should add your code into your button's click event handler.
<asp:button id="btn" runat="server" text="Submit" onclick="btn_Click" />
protected void btn_Click(object sender, eventargs e){
//code here
}

asp.net: monitor user click

I'm trying to fire an event when a user clicks a hyperlink. But it would complain that the event is not defined:
<asp:HyperLink ID="HyperLink1" onmouseover="btnSubmit_Click" runat="server">www.google.com</asp:HyperLink>
<asp:Button
id="btnSubmit"
Text="Submit"
Runat="server" />
protected void btnSubmit_Click(object sender, EventArgs e)
{
btnSubmit.Text = "clicked a link!!!";
}
I see several problems.
You do not have any sort of click event setup on your hyperlink. You do have a "onmouseover" but based on MSDN's documentation there is no click event for that control.
You have a button defined, but no events associated with that button.
You have a function that appears to be an event handler, but the naming convention suggests that it is associated with the button that has no events.
Can you provide more detail of what you are trying to do? I assume the c# code you have posted resides in the code behind?
Update:
Try changing your code to this -
<asp:LinkButton ID="lb_Link" OnClick="btnSubmit_Click" Text="www.google.com" runat="server" />
Obviously this will not redirect you, but based on what your code does, it doesn't sound like you want a redirect...
The event you're trying to trigger is a server side event. You need to use client side code for what you want to do. Plus, there is no property known as onmouseover, you can add it as a client side event from code behind
HyperLink1.Attributes.Add("onmouseover","yourClientFunction");//this can be done in page load

Submit form to a non default page

I solved this problem in another instance by making some workaround but I want to get it clear this time as I understand my problem better.
My asp.net net page has a search functionality that searches the database based on 5 fields. The result is displayed in the gridview. The gridview is editable and I can update values. The problem is if I want to update multiple values, gridview won't allow it. So I included an extra column for checkbox. I have added a footer which has link to update all checked records.
Ok so here is the problem? How do I send the whole gridview to another page where I can capture the gridview values?
By default the page is submitted onto itself. If I change the default action page, the whole gridview and search, nothing will work.
So how do I submit the whole page (or part of it) to a different page other than the default action script?
I have not tried this specifically with a gridview, but I think a Cross Page PostBack should work. I have a search feature on my website as well and this is what I use.
Set the PostBackUrl of the Button that is causing the PostBack to the Destination page.
Let’s assume your source page is search.aspx and your destination page is SearchResult.aspx
Inside search.aspx:
<asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="right"
ValidationGroup = "Search"
PostBackUrl="~/SearchResult.aspx"
onclick="btnSearch_Click"/>
The form will be posted to SearchResult.aspx. Inside SearchResult.aspx, you add this directive:
<%# PreviousPageType VirtualPath="~/Search.aspx" %>
And in the code behind, you can acess any control like this:
PreviousPage.<mycontrol>
Hope this help.
Have you tried using the Session? Just add the DataSource, or the Gridview itself to the Session and load it in the other Page and then Dispose() it.
Good luck!
UPDATE:
I have accomplished this in the past through Cross-Page Posting. This is how I did it now for testing purposes:
Default.aspx:
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" PostBackUrl="SearchResult.aspx" />
SearchResult.aspx.cs:
protected void Page_Init(object sender, EventArgs e)
{
if (PreviousPage != null)
{
if (Page.PreviousPage.FindControl("GridView1") != null)
{
GridView grid = (GridView)Page.PreviousPage.FindControl("GridView1");
grid.ID = "myGrid";
this.form1.Controls.Add(grid);
}
}
}
Hopefully this helps.

Cannot add event handlers to ASP. net page

In visual studio 2008 when I drag and drop asp controlXXX on aspx page. Property page for this control, does not show event handlers button. It can reappear after switching to designer view but then disappears again. Screenshot attached.
Yes, that's annoying, but it works probably only in designer.
But you can still add event handlers manually
in html add onclick property and write name of the method
<asp:ImageButton ID="btnAdd" runat="server" onclick="btnAdd_Click" />
and in code-behind add method with two parameters of types: (object, EventArgs) like this:
protected void btnAddTag_Click(object sender, ImageClickEventArgs e)
{
}
or you can also add event handler in Page_Init method
btnAdd.Click+=new ImageClickEventHandler(btnAdd_Click);
(this will also generate automatically the method, only after += press TAB twice)

Resources