how to add hide event to the button in webform? - c#

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.

Related

Server Controls, Client Controls, Panels, Default Buttons and Client Events

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.

Reset a page in ASP.NET without postback

I want to reset a form but I'm using a few "Required Field Validator" every time I click the reset button the error message shows requiring fields... What I tried:
ReuiqredFieldValidator.Enabled = false;
TextBox.Text="";
Response.Redirect("Samepage.aspx");
Response.Redirect("Request.RawUrl");
<input type=reset>
Try this:
<asp:Button
runat="server"
ID="reBt"
Text="Reset"
OnClientClick="this.form.reset();return false;"
CausesValidation="false"
/>
from here
You can apply ValidationGroup property to group of controls and the relevant asp button and do not provide ValidationGroup to reset button.
Or same can be achieved vice-versa
This is applicable as you using asp.net controls
Refer this link
you can try it
<input type="reset" causesvalidation="False">
It should work.
It works for input submit so I think it should work on input reset also.
Details Input =submit
Edit 1
Or if you are using jQuery you can use like this
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
References
Clear form fields with jQuery
How to reset a form using jQuery with .reset() method
jQuery reference
:reset Selector

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; });
});

How to show/hide buttons contain in a template by javascript

I have two asp.net buttons inside a template (Expand and Collapse)
I want to implement a simple client side javascript function to hide the expand button after press it and show the collapse button and vice versa.
<asp:Button ID="btnExpand" runat="server" CommandName="Expand"
CommandArgument='<%# Container.DataElement("Id")%>' Text="+" />
<asp:Button ID="btnCollapse" runat="server" CommandName="Collapse"
CommandArgument='<%# Container.DataElement("Id")%>' Text="-" />
I tried OnClientClick event but I didn't know how to get the sender button and the second button from javascipt because they're in a template and their IDs will be generated.
I tried also to change their visibility from the code behind in the server (by Visible property) but the problems is the event handler will be fired after the postback and the changes will not be applied in the client.
Any help !!
sorry if the question is silly, I'm new in the web development.
Thanks in advance.
Use ClientID like:
<!-- Supposing you have the following button control -->
<asp:button id="myButton" runat="server" />
<script type="text/javascript">
var theID = '<%= myButton.ClientID %>';
// Now do whatever you like with it.
</script>
Be careful where you place that code. You need to make sure that the html client renderer has finished creating that item.
In the function where you are implementing the hide and show template, put $("#buttonID").hide();.

Disabled asp button

i have asp button like this:
<asp:Button ID="ImportToDB" runat="server" OnClick="ImportToDB_Click" />
And i need to call a javascript function when mouseover on this button. So i have in page_load():
ImportToDB.Attributes.Add("onmouseover","javascript:OnButtonMove(" + ImportToDB.ClientID + ")");
javascript function:
<script language="JavaScript" type="text/javascript">
function OnButtonMove(id) {
//something
}
</script>
Everithing work fine only if button is enabled. but when i disable button, this javascript function will never fire.
what i am trying to do: I have button and when is something wrong i just disable it. And when user mouseover this(disable) button, I show him DIV with a message.
Can someone tell me why i cannot call this JS function while button is disabled?
If the event is never fired when the button is disabled, you could put the button in a <div> and add the event handler to this instead. Then check if the button is disabled inside your javascript function.
When this button is disabled it can't be accessed from client side javascript only by server side code (post back), a work around to achieve this by client-side way is to use a div control above this button as Graham Clark says like this
<div onmouseover="javascript:OnButtonMove(this.children[0].id)">
<asp:Button ID="ImportToDB" runat="server" Text="test" Enabled="false" />
</div>

Categories

Resources