Submit form to a non default page - asp.net

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.

Related

How to submit data twice on an ASP.NET page

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>

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 PostbackURL doesn’t work if I put in Javascript Validation

I would like to share my problem with you in detail.
1) I have a textbox and a button in my page consider it as home.aspx
2) I have written the code like this:
<asp:Button id="btnSubmit" runat="server" Text="Submit" PostBackUrl="~\search.aspx"
OnClientClick="validate();" />
3) I have to check wheteher the textbox is null. If not null i want to redirect to search.aspx as given in the postbackurl.
4) Using java script i have validated;
function validate()
{
if(document.getElementById('txtCity').value!='')
{ alert('please enter the city to search'); returnn false;}
}
5) If the textbox is null. It alerts to enter the city. If not the page remains. It is not redirected to the search page.
I hope you can..
Kumar,
You will need to provide much more information then this.
A blind guess at what you are asking, is that you have a button on your page that causes validation. However, also on your page you have a linkbutton that should not cause validation, but just direct to the PostBackUrl.
You can accomplish this one of two ways:
1. Add a validation group to your button and all the controls it should validate.
2. On your LinkButton, add CausesValidation="false" to your declaration.
If these blind guesses are not helpful, please provide much more information.

ASP.NET invoke ASP.NET buttons server event in javascript

I am having an ASP.NET page with one Asp.net button control and a normal html link (anchor tage) I want to invoke the postbackl event of asp.net button control when someone clicks on the link.
I used the below code
<a href="javascript:myFunction();" class="checkout" ></a>
<asp:Button ID="btnCheckout" runat="server" Visible="false"
onclick="btnCheckout_Click" />
and in my javascript i have
function myFunction()
{
var strname;
strname = "Test";
__doPostBack('btnCheckout','OnClick');
}
But when runnin gthis , i am getting an error like __doPostBack is undefined
Can any one tell me why it is ?
Thanks in advance
This anyway wouldn't have worked. When you make your .NET control invisible by using 'Visible="false"' it isn't rendered, that means not available at the client.
Back to your question.
1- Where is myFunction defined? Between the tag?
2- Are there more .NET controls on the page? If there aren't any other .NET controls, .NET doesn't add all the scripts that are required for postbacks and stuff.
Why not do the following (based on TheVillageIdiot answer):
<asp:LinkButton ID="lbtnCheckout" runat="server" CausesValidation="false" OnClick="lbtnCheckout_Click" CssClass="checkout" />
With the above example you don't need the fake button and make it invisble. You still can do your postback. Way more cleaner approach I would say.
First of all I tried your code and also not get anything like __doPostBack, then I added another button on the page which was visible but it was all the same. Then I added a LinkButton and got __doPostBack method. You can do post back from javascript but then EventValidation is problem, as it does not allow this kind of thing. I had to use the following to overcome it and it worked:
protected override void Render(HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation(
new PostBackOptions(btnCheckout, "OnClick"));
base.Render(writer);
}
I think I'm bit incoherent in answering so I'll mark it as wiki :)

asp:ImageButton not firing onclick event

I have a page that uses a master page, several RequiredFieldValidators, and the Web Toolkit autocomplete extender. The following code only shows the bare minimum of the page:
<%# Page Language="C#"
AutoEventWireup="true"
CodeFile="Login.aspx.cs"
MasterPageFile="~/master.master"
Inherits="Login" %>
<asp:Content id="Content1"
contentplaceholderid="ContentPlaceHolder1"
runat="server">
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<div>
<asp:ImageButton class="submitButton"
imageurl="images/button_submit.gif"
id="btnSubmit"
runat="server"
onclick="btnSubmit_ServerClick"/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Code-behind:
protected void btnSubmit_ServerClick
(object sender, ImageClickEventArgs e)
{
//breakpoint here does not get hit
}
The <form runat="server"> tag is in the master page. The code above does not fire the onclick event. If I get rid of the master page and add a form tag to the page, it works. Is the form tag in the master page not supported, or is this supposed to work somehow?
alt text http://digitalcopy.warnerbros.com/images/mainmenu.gif?provider=00079&disc=03403AAA-1D20-47F2-91FA-5EE632832659
You can also check if your ImageButton does not trigger validation. If it does set its CausesValidation property to false (of course if it makes sense).
I had a similar issue (different scenario). I used Page.RegisterRequiresRaiseEvent(ImageButton) and my onclick event started to fire. Why I needed to do that? I don't know.
My solution was to set the ImageButton's CausesValidation to false.
I have a similar issue with the image button and found the root cause. You are using
"ib.ID = i + ":" + j;"
as the ID of the ImageButton, the ":" is illegal name to use, as you are creating it programmatically, ASP.NET allows it to be created.
At runtime, if you look at the HTML source of the page, you will see the special characters are either ignored or replaced with "_". So the page is unable to find the correct control, thus the event won't fire. Try changing the name with plain text, the event will fire.
ib.ID = i + ":" + j;
should be changed to
ib.ID = i.toString()+":"+j.toString();
If it still doesn't work try making use of the StringBuilder to buildup the ID and assign it later to ib.ID property
This is solution that worked for me
If you are binding through data bound controls then use OnCommand attr instead of OnClick attr
You have to have the control in a form runat=server somewhere, it can be in the Master page or the .aspx file. Double check that the master page form tag is runat=server
AutoEventWireup is the property that allows the syntax you are using. Double check the setting in the Master Page, WebForm and it can also be set in the web.config.
if that doesnt work, you can always explicilty code it (which I prefer)
<script runat=server>
protected override void OnInit(EventArgs e)
{
btnSubmit.Click += delegate(object sender, EventArgs e1)
{
};
base.OnInit(e);
}
</script>
UpdatePanel can mess with server side events being raised also, so try it without the UpdatePanel. And I am sure you have a ScriptManager in the Master Page
From the code you supplied, you seem to be missing the <asp:scriptmanager> from your page. You must do one of the following:
Have the <asp:scriptmanagerproxy> on the page and the <asp:scriptmanager> on the master page.
Have <asp:scriptmanager> on your page and no <asp:scriptmanager> on the master page.
Personally, I recommend having the <form> tag on the master page, but that's personal preference.
You can always try taking out the UpdatePanel and seeing if it works. I usually start without the UpdatePanel, get everything working the way I want and then add in the UpdatePanel and debug anything that causes.
The form in the MasterPage works for me so the ScriptManager/ScriptManagerProxy mentioned by #Keltex might be an issue, though I forget them sometimes and usually get away with it.
With the UpdatePanel the button's click event will be handled via Javascript, so you might grab FireBug or equivalent (depending on browser) and follow through what actually is happening. Is it tripping on the validation and you don't see it? Is there a JS error somewhere (the Control Toolkit isn't perfect always)? Is the page actually posting back at all and just not hitting the event handler?
On my webpage i am creating imagebuttons dynamically inside a table that is contained by an updatepanel. The buttons are created by this code:
for (int i = 0; i < 15; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < 20; j++)
{
TableCell tc = new TableCell();
ImageButton ib = new ImageButton();
ib.Click += new ImageClickEventHandler(ImageButton1_Click);
ib.ImageUrl = "../img/defaultCell.jpg";
ib.ID = i + ":" + j;
tc.Controls.Add(ib);
tc.Width = 25;
tc.Height = 25;
tr.Cells.Add(tc);
}
GameTable.Rows.Add(tr);
}
}
The image buttons will not trigger click events. HOWEVER, if the line 'ib.ID = ...' is commented out, they do! That single alternation seems to fix all the issues.
I have no idea why.
If anyone can explain this, and also tell me how to trigger events keeping the ability to set button id's, i'd be much thankful
I think it may have something to do with the fact your re-assigning the id's after creating & assigning the event handler?
Do you even need to assign the id's? - Surely this is done for you anyway? - True removing the 'ib.ID = i + ":" + j;'
Make sure you use OnClick rather than onClick
Your update panel could be messing with the postback. Try it without the UpdatePanel and see if that is the culprit.
I had the same problem that OnClick event of ImageButton was not firing. But the actual problem was, at form level, I had onSubmit="return false;"
I was facing the same issue, where I was dynamically creating an ImageButton and click of that event was not triggering. hence, i created the Image button in if (IsPostBack) . Now it is working fine. And even if the page gets refresh, the ImageButton will be retained.
I've just solved a similar issue where OutputCache was enabled. When changing from asp:ImageButton to asp:Button, the event is correctly fired. Probably asp:ImageButton has some bug with OutputCache.
After none of the above suggestions worked for me, I did one more try by calling the button creation in OnInit(). This fixed my issue and now the OnClick event is firing.

Resources