I have a .net 3.5 page which has a form control (form1) and with that form is an iframe which contains a page which contains a form (form_tester). What I am trying to do is submit form1 and at the same time submit form_tester that is within the iframe, how do you do that? I know I can submit the form via javascript but I am just wondering if there is a standard way, or a better way of doing it?
Thanks.
I did it in the end by adding a button to the page (form) the was encapsulated within the iframe. Then I added an onclientclick event to the submit button. I then added some javascipt which clicks the button on the encapsulated page and hence it get's submitted. The hardest part was working out how to access the elements on the encapsulated form.
window.frames["frame1"].document.forms['hidden_form']["submit_btn"].click();
but it does work.
Related
In the early days of .NET, you had no choice. Your entire page content was wrapped in a single form tag and you made it work. Somewhere along the way, we started moving away from that, however where I read it eludes me. I recall reading that the new standard is not to put a single form tag in your master page and instead place the form tag in each usercontrol that needed it.
This worked well for me up until recently. I've now created a usercontrol that works fine if the form tag is in the Master page, but if the form control is in the usercontrol, when the form postsback, none of the controls retain their preselected values (i.e. my dropdownlist selection).
Should I go back to placing my form control in the Master page and strip the form tag out of all of my usercontrols? Or did I read correctly and the new recommendation is to put form tags in usercontrols?
If you are using ASP.NET web forms you should have the form tag in the master page.
See this link:
http://msdn.microsoft.com/en-us/library/fb3w5b53%28v=vs.100%29.aspx
From this article:
The user control does not have html, body, or form elements in it. These elements must be in the hosting page.
Got a very frustrating problem that I'm trying to solve regarding C#.NET's way of handling dynamically added usercontrols.
The basic rundown of my page setup is that I have an ASPX page which has a Master Page. The Master Page has some content placeholders. On the codebehind in the ASPX page, I call the Master.FindControl method to find the content placeholders, and then use the LoadControl method to load a UserControl into the placeholder.
On the UserControl, I have a series of textboxes, and a submit button. In this implementation, when I click on the submit button, and put a breakpoint into the OnClick event, I find that the OnClick event is never called.
However, if, instead of using this approach, I remove the Master Page, and add the content placeholders directly on the ASPX page, then use Placeholder.Controls.Add to add the UserControl instead of Master.FindControl, the button works perfectly.
Does anyone have any suggestions as to how I can resolve this without removing the Master Page?
I have a FormView that has two buttons on them. What I need to happen is when the user clicks on the buttons, a popup window is displayed. The data in this window is being pulled from a stored procedure in which I will need to pass variables. Can someone supply code on how to do this?
Create a new page. This is your popup (ie popup). On the your calling page add a hyperlink that calls you popup page but make sure the Target attribute is set to _new which tells the link to open a new browser instance.
Open popup page.
In your pop up page you would then call your ADO.net sql specific code in the Page_Load event.
Are you passing a url encoded variable to this page?
I have an updatepanel,a modal popup extender inside that and an image button to open the pop up(it has not any click event). The 'div' for the modal pop up is outside the updatepanel. In the modal pop up the records come in a table with a link in each table row.When the link is clicked,a javascript function causes a hidden control to postback and fetch values from database.First time it is working fine,but next time the image button(TargetControlID for modal pop up extender) does not work and it is causing a postback and loading the page.Help plz...
Thanks in advance.
Mohak
Are you using myLink.ClientID or did you hardcoded the ID of the link in the Javascript function?
The ID of the link is probably changed when doing the first postback, the second time ASP.NET has generated a new ID (not visible in the HTML because of the AJAX-request) for your link and this doesn't match anymore with the ID in your Javascript function.
The solution would be to use myLink.ClientID
I have a master page with one form on it. It is a search form which must always be visible. When the button of that form is clicked I want the form data to be sent to search.aspx. The problem is, I don't know how. I cannot set the form action to search.aspx, because all my other pages which use the master form will go to search.aspx. This I don't want.
Hope someone can help me out :)
Thnx.
In order to pass the values of the control "txtSearch", when Server.Transfer is executed, you could do many things, including passing it via a querystring variable or setting up a session variable, and then check either of those in the Page_Load event of Search.aspx, and if it's populated, call the event that is fired when the user would hit the submit button on the Search.aspx page.
Also, if the Search.aspx file is using the same masterpage, then you can use this.Master.FindControl("txtSearch") to get the control (it you look a the source of the file after it is generated in the browser, you'll notice that controls in the master page aren't really called by their ID, rather that have something appended to them (i.e. it would now possibly be called "ctl00_txtSearch")
You could create your search form in a separate form, and get it to use GET instead of POST.
Either that, or have the master form handle the search button click and use Server.Transfer to go to the search form.
You can have multiple forms in one page I believe. So one form (your search form) would have its action set to search.aspx and the other would be set for the page itself.
ASP.NET webform pages only have one form (which would generally be included on the master page). You can set the postback url for the search button to your search page..
<asp:Button ID="btnSearch" runat="server" Text="Search" PostBackUrl="~/search.aspx" />
..or just redirect to it from the handler in your master page like this:
protected void btnSearch_Click(object sender, EventArgs e)
{
Response.Redirect(#"~/search.aspx?q=" + Server.UrlEncode(txtSearch.Text));
}
..or use Server.Transfer as suggested by David Kemp.
Note: If you use Request.Query[#"q"] on your search page to get your query, you don't need to use Server.UrlDecode() - it's done for you.
I would:
Add some code to the master page code-behind to detect the source of the POST.
Once I have the source of the POST (e.g. the Search box). I would then pass its query to the Search form.
I used a similar process with having a HTML login form on the master page.
I posted a question and subsequent solution here - check it out:
Form Elements in ASP.NET Master Pages and Content Pages
Once I got my head round it, it seemed a pretty simple and reasonably elegant solution.
The benefit of this is that you have complete control over how the data is sent to the search form. And you don't need to enable transfer of form data to the search form and all that nasty stuff, just create a new GET request to the search page and let it do what it is supposed to do :)
Hope this helps.
NOTE:
You can only have one form with runat="server" on an ASPX page. Additional forms MUST be HTML FORMS.
Because your search form is in the master page, you can probably structure it to contain 2 forms. Place the search form tags with the action set to "search.aspx" outside of the tag that is used by the rest of the site.
<body>
<form action="search.aspx>
<!--search box and submit button-->
</form>
<form runat="server">
<!--rest of page inc placeholder-->
</form>
</body>
If the structure of the page will not enable this, you can set the submit button's PosbackUrl to point to "search.aspx". In this case, "search.aspx" would need to be coded to look in the PreviousPage property for the form data, or use Request.Form to access the input.