switch masterpage button default button property in ASP.NET - asp.net

I'm developing an ASP.NET web app (C#, VS2010), I have a master page which has a button (search button) and a textbox, when I press enter, this search button executes, but I don't want this master page search button to work when I'm in my login page. In my login page I have two textboxes and another button (login button), I want this login button to work as default button (execute with enter key) when I'm in login page. Of course I've not used any javascript or DefaultButton property at all
how can I disable masterpage button enter key in my login page?

You need to define a DefaultButton around each area you want default buttons, it will then use the part of the form that has focus:
<asp:Panel id="SearchPanel" runat="server" DefaultButton="Search" >
<asp:Button id="Search" runat="server" Text="Search" />
<asp:Panel>
<asp:Panel id="LoginPanel" runat="server" DefaultButton="Login" >
<asp:Button id="Login" runat="server" Text="Login" />
<asp:Panel>

In your login Page get a reference to Master Page button control and then disable it.
Button b = Master.FindControl("ButtonID");
b.Enable= false;
To change default buton of master page change it's default button in master page codebehind
form1.DefaultButton = "New button ID";
oR from login page codebehind
Form.DefaultButton = "New button ID";

Related

on every postback page opens in a new tab

I have a weird problem in my page. I have a button called Print. I wanted report should come in a new tab, so i wrote some javascript on button's onClientClick. Which works great with no problem at all.
But problem starts now when user comes back on original page again, now here i have several controls which cause postback. Say for example its a dropdownlist. so whenever user changes dropdown item it causes postback which is fine but everytime it opens a new tab on every postback.
hope I am clear in question...
Any help??
Here is a code:
<asp:Button ID="btnshow" runat="server" Text="Show" Font-Bold="true" ForeColor="Black" Width="90px" OnClick="btnshow_Click" OnClientClick ="document.forms[0].target = '_blank';"/>
The issue is document.forms[0].target='_blank' is setting the target on the form not the individual button so a postback triggered by any control will open in a new tab.
You should use a HyperLink control instead of the Button control. The HyperLink control has a Target property which allows you to specify how the link should be opened.
Below is an example taken from the HyperLink documentation. This will render an anchor tag with target="_blank".
<asp:HyperLink ID="lnkPrint" NavigateUrl="http://www.microsoft.com" Text="Print" Target="_blank" runat="server" />

Adding a click event to a Hyperlink displayed in ListView VB.NET ASP.NET

Is there a way to add a click event to a Hyperlink tag that's render in ListView?
Basically, I have a HyperLink tag that generates a link dynamically and it opens up to a new tab when users click on it. At the same time, when the user click on it, I want to post a text or make the text label visible. Sample code below:
<asp:ListView ...>
<ItemTemplate>
<asp:Label ID="Msg" Text="*You have already accessed this link*" runat="server" Visible="false"/>
<asp:HyperLink ID="label1" NavigatUrl='<%#Eval("Link")%>'Target="_blink" text="Click Link" runat="server"></asp:HyperLink>
<//ItemTemplate>
</asp:ListView>
You can use QueryString. Add the link with a Query Parameter on it and then -
If IsPostBack=True then 'Check if the page is reloading [Because when you clik on the link with the same link, it will reload the page]
'Considering the QueryString will be like - "yourdomain/default.aspx?item=1"
'Check for QueryString
Dim s_ItemId As String = Request.QueryString("item")
if s_ItemId<>"" then
'Do whatever you want
End If

Asp.Net multiple submit buttons, enter submits only the first

I have a few submit buttons in my ASP.NET page. Everything works fine, but when a user hits enter button always the first submit button works. I want to fire each submit button with corresponding fields but cannot have multiple forms. Is it possible to group them without using forms?
You don't need forms, just wrap the related controls in their own panels and use the Panel.DefaultButton property.
Use the DefaultButton property to indicate which button gets clicked
when the Panel control has focus and the user presses the ENTER key.
The DefaultButton can be set to the identifier for a Button control or
any control that implements the IButtonControl interface except a
LinkButton control.
Please use DefaultButton property like this
<asp:Panel runat="server" DefaultButton="bt1">
<asp:TextBox runat="server" />
<asp:Button id="bt1" Text="Default" runat="server" />
</asp:Panel>
Please refer here :- ASP.NET DefaultButton Property

Handle Enter Key for search and login page

I have an ASP.Net 3.5 in VB.Net page that uses a master-page and has several pages it uses to display content. On the master-page their is a search text-box for the site so that shows up on all the pages. Their is also a login page screen to get to a members account.
The issue is: When you are on a content page I would like when a user puts something in to search for and presses enter it will process the search request.
But....
If you are on the login page I would like to disable enter key for the search and if the user presses enter on the login page to process the login request.
Is their an easy way to accomplish this?
Create your form in an asp:panel and set the DefaultButton to be the button of the form you want to submit when enter is pressed
<asp:Panel DefaultButton="btnSubmit" runat="server">
<asp:TextBox ID="UserName" runat="server"/>
<asp:TextBox ID="Password" TextMode="Password" runat="server"/>
<asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Sign In" runat="server"/>
</asp:Panel>
Note: asp:panel creates a div

2 user controls on registered on one aspx page not validating validation properly

I have 2 user controls on registered on one aspx page.
UserControl1 us having one text box with require field and one submit button.
UserControl2 is also having one text box with requirefiled and save button.
Expected o/p is-
When I am clicking on any button out of 2(submit or save). Then only related text boxof that user control should be validate.
But the error is
Both text boxes are validate.
Please help me .
Set the ValidationGroup properties to limit which fields get validated when the buttons are pressed.
So for example, if these were contained within the first user control:
<asp:requiredfieldvalidator id="NameValidator"
controltovalidate="NameTextBox"
validationgroup="UserControlOne"
errormessage="required"
runat="Server" />
<asp:button id="Submit"
text="Submit"
causesvalidation="true"
validationgroup="UserControlOne"
runat="Server" />
Clicking the "Submit" button would only cause the validators that have UserControlOne specified as the ValidationGroup to validate.
Edit: When you call Page.Validate() you are validating every group on the page. Call the overloaded Page.Validate(validationGroup) to validate a specific one. e.g. Page.Validate("UserControlOne")

Resources