Server Control Auto Postback with form has _blank attribute - asp.net

My case is I have an asp.net page has a form
<form id="form1" runat="server" target="_blank">
and a button redirect to another page and this page will open in a new window because of the target attribute of the form .
<asp:Button ID="button1" runat="server" PostBackUrl="~/kindofpage.aspx" Text="Generate" />
and I have a dropdownlist has auto postback = true to post the past to fill another dropdownlist by selected data .
<asp:dropdownliast id="Make" name="Make" runat="server" autopostback="true"></asp:dropdownlist>
the question is : why when I select item from the auto postbacked dropdown an blank page opened ?
I need a way to post the page by the dropdownlist without openning a blank page ..
Thank you,

For lack of a better idea, you could just remove the target="_blank" attribute from your markup, and when your button is clicked, modify the form tag with JavaScript and set the attribute.
You can set the OnClientClick property and run JavaScript when it's clicked. For example:
<asp:Button ID="button1" OnClientClick="document.getElementById('form1').setAttribute('target', '_blank')" runat="server" PostBackUrl="~/kindofpage.aspx" Text="Generate" />

You could always just adjust your buttonpress code to open a new window such as this:
<asp:Button ID="myBtn" runat="server" Text="Click me"
onclick="myBtn_Click" OnClientClick="window.open('kindofpage.aspx', 'kindofpage');" />
then remove the:
target="_blank"
From the form tag.

I struggled with a similar situation but solved it in the following way.
As mentioned in this answer, you can use the OnClientClick property to set the target to "_blank". E.g.
<asp:Button ID="button1" OnClick="codebehind_method" OnClientClick="document.forms[0].target = '_blank';" runat="server" Text="targets new window" />
Then, in the aspx page that my "codebehind_method" function redirects to, I reset the target of the opener form like so:
<script type="text/javascript">
function resetTarget() {
opener.document.forms[0].target = '';
}
</script>
<body onload="resetTarget()">
Now, if you go back to your opener form and use a control that does not have the "OnClientClick" property set, the AutoPostBack should occur in the same tab.
If you want to find your form by ID, replace "document.forms[0]" with:
document.getElementByID('yourFormName')

<form id="form1" runat="server">

Related

code behind with postback url

I try to make postpackurl in code behind so that I can send the code but my issue is when I click the button its keep it in the same page .
my question is how I can make the postbackurl directly go to next page?
Asp.net Button Has property named postbackurl which you can set postbackurl:
<asp:button id="Button2"
text="Post value to another page"
postbackurl="Button.PostBackUrlPage2cs.aspx"
runat="Server">
</asp:button>
Use
response.redirect("frmdefault.aspx")
in your button click code.
In the ASPX file add the below Script:-
<script type="text/javascript">
function SomeMethod() {
window.location.reload("nextpage.aspx");
return false;
}
</script>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return SomeMethod();"/>
When the user clicks on the button1 the nextpage.aspx page will be executed and no postback will happen on the current page

how to set a default 'enter' on a certain button

There is a textbox on a ContentPage. When the user presses Enter in that textbox I am trying to fire a 'Submit' button on this ContentPage. I'd like to fire off that particular button's event.
Instead, there is a search textbox & button on the top of the page from a MasterPage, and this search button's event fires off.
How do I control to fire off this ContentPage's submit button, instead of the MasterPage's search button?
I am using Ektron CMS for my content management.
The easiest way is to put the fields and button inside of a Panel and set the default button to the button you want to be activated on enter.
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>
if you need to do it from code, use
Me.Form.DefaultButton = Me.btn.UniqueID
Where btn is your button control.
You can use the DefaultButton property on either a server-side form control or Panel control. In your case, group the controls together in a Panel that should fire off the same button:
<asp:Panel ID="SearchBox" runat="server" DefaultButton="BtnSearch">
...
<asp:Button ID="BtnSearch" runat="server" Text="Search!" />
</asp:Panel>
....
<asp:Panel ID="UserPanel" runat="server" DefaultButton="BtnUserSubmit">
...
<asp:Button ID="BtnUserSubmit" runat="server" Text="Submit" />
</asp:Panel>
You can now use UseSubmitBehavior property to disable all the buttons you don't want to fire when hitting submit (check out the documentation for more info)
<asp:Button ID="BtnNotToFIre" runat="server" Text="Search" UseSubmitBehavior="false" />
Microsoft say:
<form id="Form1"
defaultbutton="SubmitButton"
defaultfocus="TextBox1"
runat="server">
enter link description here
$(document).ready(function(){
document.getElementById("text_box_id")
.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("button_id").click();
}
});
});

ASP.NET WebForms - Keeping contextual focus on Button controls

Consider the following:
<form runat="server">
<div>
<asp:TextBox runat="server" ID="tb1" />
<asp:Button runat="server" ID="b1" OnClick="b1_Click" />
</div>
<div>
<asp:TextBox runat="server" ID="tb2" />
<asp:Button runat="server" ID="b2" OnClick="b2_Click" />
</div>
<div>
<asp:TextBox runat="server" ID="tb3" />
<asp:Button runat="server" ID="b3" OnClick="b3_Click" />
</div>
</form>
Each TextBox has an associated Button. I want to be able to switch the focus on each of these Button controls, so that when I place my cursor in the 2nd textbox (tb2) and press Enter, the associated button (b2) gets clicked and the associated OnClick event gets fired.
I've got a few ideas myself, but I'd like you guys' feedback/lessons-learned before I start potentially wasting time on implementing a broken solution.
NOTE:
Using the HTML fieldset element is not an option--Some of the interfaces are very complex.
There can be multiple inputs associated with one button.
You could trap the keydown event on the Textbox and then fire the button's callback javascript if it's the enter key. You can get the callback reference using ClientScriptManager.GetPostBackEventReference
Alternatively you could wrap every textbox in it's own Panel, which exposes a DefaultButton property.
Well you could do a nice simple route using jQuery if you are using it.
Simply doing the following might work nicely:
<script language="javascript" type="text/javascript">
jQuery(function(){
jQuery('input').keydown(function(e){
if (e.keyCode == 13) {
jQuery(this).next().trigger('click');
return false;
}
});
});
</script>
And then code side you would have the relevant event handler triggered, or just simply see which button was clicked by querying the sender object id

How to set html button as default for ASP.Net form?

Well, I'm trying to make ASP.NET urls looking user-friendly, like it was explained in this question. So I created an ASP.Net form, and placed asp:textbox and asp:button on it. Also I set onclientclick attribute to call JS function which navigates to smart URL by setting windows.location.href. In Firefox it works well but in IE and Opera the browser first navigates to smart url but then it closes connection and sends a postback using an asp.net form action.
I tried to solve it using html button instead of server ones. It works, but the problem is that it can't be set as default for the asp.net form. So' if user clicks on it, it does its work. But if the user just presses enter when form is active, the form performs its action, so the button is not pressed and JS url rewriting doesn't occur. So how can I solve this problem?
My JS looks like this:
function searchRedirect() {
var query = $get('colSearch');
window.location.href = 'colSearch?q=' + query.value;
return false;
}
and in search.aspx i have
<form id="MainForm" runat="server" method="get">
<asp:TextBox id="colSearch" runat="server" Width="615px" CssClass="searchLine"></asp:TextBox>
<input id="Button1" type="button" value="Search!" onclick="searchRedirect();" class="search" />
I also tried with asp:button:
<form id="MainForm" runat="server" method="get" defaultbutton="submitReqBtn">
<asp:TextBox id="colSearch" runat="server" Width="615px" CssClass="searchLine"></asp:TextBox>
<asp:Button runat="server" Text="Search!" ID="submirReqBtn"
onclientclick="searchRedirect();" CausesValidation="False"
EnableViewState="False" UseSubmitBehavior="False"></asp:Button>
</form>
Your onclientclick event needs to return false;
The form accepts an attribute showing which of the buttons are set as default: use it as in
<form id="form1" runat="server" defaultbutton="Button1">
where Button1 is the id of a button on the page.

.net Accordion Causing me Problems

I had a bunch of controls that I displayed, hid, enabled and disabled based on actions in the web page. Everything worked until i put them into an accordian. Now I can't get the Javascript to be able to update their state. I have a small example
this is the Javascript
<script type="text/javascript">
var ctrl = document.getElementById('<%= btmRocp.ClientID %>');
function ShowPanel(control)
{
alert('<%= btmRocp.ClientID %>');
ctrl.disabled = true;
}
</script>
This is the Accordian
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:Accordion ID="MyAccordion"
runat="Server"
SelectedIndex="0"
>
<Panes>
<cc1:AccordionPane ID="accordianPane0" runat=server>
<Header>Create New Report </Header>
<Content>a
<asp:Button ID="Button1" onmouseup="ShowPanel('') " runat="server" Text="Button" />
<asp:Button ID="btmRocp" runat="server" Text="Button" />
</Content>
</cc1:AccordionPane>
<cc1:AccordionPane ID="accordianPane1" runat=server>
<Header>Create New Report </Header>
<Content>b</Content>
</cc1:AccordionPane>
</Panes>
</cc1:Accordion>
I would love to know what i am doing wrong here the Alert prints out the right ID.
If i do something where i pass the "this" Object to the function i can disable that button but I truly need it to disable, or hide like 10 objects
Does anyone have an idea?
Sample Code at http://www.riconllc.com/accordian.zip
What is the default state of the Accordion? collapsed? I have no idea how the Accordion works, but I'm suspecting that it is modifying the HTML DOM such that when the page first loads "btmRocp" is not actually present on the page itself, until it becomes "visible". That is, it might be injecting controls into and out of the page, based on the accordion status.
Your best bet in figuring out this behavior is to insert "debugger;" statements into your page at appropriate points, to inspect the live DOM at those points in time.
<textbox id="debugbox" onblur="this.value = eval(this.value);"></textbox>
Is a good way to monkey with script on your page as well.

Resources