Server Controls, Client Controls, Panels, Default Buttons and Client Events - c#

Ok so I would like to be able to be in the part number textbox press enter and it do a 'Quick Search'. However when press enter it activates the 'Search' Button instead. The 'Search' Button as the diagram shows is a default button of the panel it is in. But the 'Quick Search' is not in that same panel so I am kinda stumped on how to change this action so it calls click on the button on the 'Quick Search' and not the 'Search'. If this doesn't make since ask more questions and I will update the diagram and question.
Thanks in advance!
New Facts
In the browser Render ... these are in the same form
I want the Quick Search Button to be a client side button which makes it hard to use a panel and a default button

Try to place Part Number textbox and Quick Search button in separate Panel with DefaultButton="bnQuickSearck" attribute like follows:
<asp:Panel runat="server" DefaultButton="bnQuickSearck">
<asp:TextBox ID="tbPartNumber" runat="server"></asp:TextBox>
<asp:Button ID="bnQuickSearck" runat="server" Text="Quick Search" />
</asp:Panel>
EDIT If you have client-based button you can use the following code on javascript:
<div id="divId">
<input id="txtId" type="text" />
<input id="btnId" type="button" value="Quick Search" onclick="alert('test')"; />
</div>
<script>
$("#divId").bind("keypress", function (e) {
if (e.keyCode == 13) {
$("#btnId").click();
return false;
}
});
</script>
Make sure jQuery installed in your web application in this case.

It sounds like you have one big form. If this is the case, when you hit enter in the Part Number field it activates the default action, in this case it would seem to be the "Search" button. If you can break up your forms, they'll each have their own default actions. This will eliminate any need for crazy redirects, AJAX, server-side foo, etc.

Related

how to add hide event to the button in webform?

im trying to hide div by clicking the button, but it does hide it for a second and return the div (looks like it refreshing page).here is my html code :
<div id="page">
<p>First Paragraph</p>
<p>Second Paragraph</p>
<p>Yet one more Paragraph</p>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
and here is my jquery code:
$(document).ready(function () {
$("#Button1").click(function () {
$("#page").hide();
});
});
What Rick said in his answer AND you need to add Type="Button" to your button.
<Button ID="Button1" type="button">Button</button>
The default button type is submit, which will cause the page refresh. AND, as Rick mentioned, ASP buttons will also submit. So, you need to use an HTML button and set the type="button" attribute.
From MDN
The type of the button. Possible values are:
submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute
is dynamically changed to an empty or invalid value.
reset: The button resets all the controls to their initial values.
button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are
triggered when the events occur.
An ASP Button with runat="server" causes a server post-back. If you aren't going to be acting on that click from the server, just use a client-side button like:
<button id="Button1" type="button">Button</button>
Set the client id mode to static on the button, then it'll work. It's not working because that hasn't been specified so the name is changing.
<asp:Button ID="Button1" ClientIDMode ="Static" runat="server" Text="Button" />
If you wanted to disable the postback to simply hide something without processing anything, then just using a client side button is the best option.

ASP.net C#. Multiple pages in one

So I am using ASP.NET 4.5 web forms.
I have this form where I want a user to enter data, after the users enters data they should be able to press the next button and they can enter more data.
Is it possible to do this on one ASPX form, or do I need to create another ASPX form.
e.g
Username
Password
--- User clicks the next button---
Email
Phone
--- User clicks finish ---
Yes this is possible through asp.net Wizard control or MultiView control. You should be able to find these controls in ToolBox under standard category.
To learn more about these controls, please follow these links:
https://msdn.microsoft.com/en-us/library/wdb4eb30(v=vs.140).aspx
https://msdn.microsoft.com/en-us/library/ms227665(v=vs.140).aspx
Create two divs, one for username and pwd and another for email and phone.
Using jquery 'hide' and 'show' effects you can control both divs, set 'show' for when user click next button.
I am not sure whether this is a right solution for you.
Try this:
Initially, I hide the email and phone, when the user clicks next, it set the style to display:block to show the two textboxes
<script>
function showDiv() {
document.getElementById('divEmailAndPhone').setAttribute('style', 'display:block');
}
</script>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" Text="Username">
</asp:Label>
<asp:Label runat="server" Text="Password"></asp:Label>
<button onclick="showDiv(); return false;">Next</button>
</div>
<div id="divEmailAndPhone" style="display:none">
<asp:Label runat="server" Text="Email"></asp:Label>
<asp:Label runat="server" Text="Phone"></asp:Label>
</div>
</form>
you can do it in one page.
Just add two other textboxes and set visible false to them.when you press next button change the label text and set visible true to these textbox and hide username and password text box.

add numbers to textbox

<input type=text id="txtNum"/>
ex. of button
<asp:Button ID="btnNum1" runat="server" CssClass="btnNumbers" Text="1" />
<asp:Button ID="btnNum5" runat="server" CssClass="btnNumbers" Text="5" />
Basically I have a huge numberpad on my page using Buttons label from 0-9 and backspage and clear. This is going to be for a touchscreen device
I am not sure how to go about when a user touches button1 to place a '1' in my textbox. Then if they hit button5 my textbox value would append the 5 to the 1.
I would like to use javascript to perform this task so it does not do a postback for every button click please help.
The easiest way to prevent the postback is to not use the asp.net button control. Just use standard html button
<button type="button" class="btnNumbers">1</button>
Then just attach to the click event for the button and append it's value to the textbox.
Add following method
$(document).ready(function() { $(".btnNumbers").click(function() { $("#txtNum").val($("#txtNum").val() +$(this).val()); return false; });
});

Using multiple forms, submit buttons on an ASP.NET web forms page

I have 3 different submit buttons(Log In, Search, Insert) in the same page and what I want is to find the best way to have this 3 buttons doing a different things when I press them, or I press enter while I am about to press them.
As you can see from my photo, when I press Log In the Insert is pressed too because I have a global form in my Master Page.
I tried to use different forms, but i can't because I need them to have runat="server", which is not possible for a page to have.
I use asp Texboxes:
<asp:TextBox class="text-input" ID="txtLoginEmail" runat="server"
Height="15px" Width="130px"></asp:TextBox>
and asp Buttons:
<asp:Button class="submit google-button" ID="btnLogin" onclick="btnLogin_Click"
runat="server" Text="Log in" />
except my Search button which is linkButton:
<asp:LinkButton ID="searchLink" runat="server" onclick="searchLink_Click">Search</asp:LinkButton>
You may use Validation Groups to cause only required text boxes validation on specific button click. And then in event handler to concrete button you may execute specific logic.

Multiple buttons on form, after clicking one how to select the other as the defualt ASP.NET

I have a ASP.NET form that has a search box, with a Go button. When you first visit the page and type in the search you can hit enter to click the go button. Then when presented with the list of results you click another button to mark the selected result for use. Then I allow the user to repeat to their hart's content to select multiple results. However the 2nd+ time that you enter a search query and hit enter it clicks the button instead of the button. You can see in the browser (IE7+) that the button is selected dispite you typing into the search field. How can I revert it so that after ever button press it selects the button as default?
I've tried using btnGo.Focus() in the button's onClick but that has no effect.
You can do a couple things.
Set the DefaultButton property of the form to the ID of your search button. This can work in a few situations, but lots of people have trouble with it, especially with complex or dynamic forms. Give it a try, simplest is best.
<form runat="server" DefaultButton="btnSearch"> ....
Add a javascript handler to the textbox in question, such that no matter the structure of the page, it will always click the search button when they press enter. The easiest way to do it would be with jQuery:
$("#myTextBox").keydown(function(e) {
if (e.keyCode == 13)
{
__doPostBack('" + <%= btnSearch.UniqueID + "','')");
}
});
but you could do something similar in the codebehind by adding an attribute to the textbox:
myTextBox.Attributes.Add("onKeyPress", "if (event.keyCode == 13) ... ")
<asp:Panel ID="pnl1" runat="server" DefaultButton="ImageButton1">
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" />
<asp:ImageButton ID="ImageButton1" runat="server" />
</asp:Panel>
or
http://weblogs.asp.net/jeff/archive/2005/07/26/420618.aspx
This should work:
<script language="javascript" type="text/javascript">
function FocusButton() {
var btn = document.getElementById('<%= btnGo.ClientID %>');
btn.Focus();
}
</script>
Then on your search textbox
<asp:TextBox ID="SearchTxt" runat="server" onClick="FocusButton">

Categories

Resources