I have a web application got from my client to do some changes on it. Here, is one button named Submit. If user clicks this button then it check on database and get the result but I can't find the code for this button. I need to change the query but I cannot find the source.
Here is the aspx code:
<asp:Button ID="Submit" runat="server" Text="Submit" CssClass="btn btn-info" ValidationGroup="submit" />
But if I inspect element using firefox then I can see this:
<input type="submit" name="Submit" value="Submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Submit", "", true, "submit", "", false, false))" id="Submit" class="btn btn-info"/>
I don't understand where does this onclick function comes from and how it works with the SQL query.I cannot find any click event into the .cs file as well. Please advise me on this.
Check PostBackUrl property for the Button server control.
Put at the onClick="Submit" in ur button field like
then on code behind the click event method will be shown and do your code
Related
I am trying to hide my content by clicking the button. But when the page is loading, content is hiding but after finishing page load content is showed. This content doesn't hide. I am using Visual Studio 2017 community and asp.net/vb.net framework. But the same JQuery code is working on my text editor and my browser.
Here is my sample code:
<p id="justp" class="testcls">Hi Hide me!</p>
<asp:Button ID="testbtn" runat="server" Text="hide me" />
<script>
$(function () {
$('#testbtn').click(function () {
$('.testcls').hide();
});
});
</script>
I am try to passing alert inside hide() and it is run successfully :
$('.testcls').hide(alert("Hello"));
What is the problem?
How can I solve this?
In case your are just using this button to hide/show your content on the client side (no server side calculation or something like that) use a normal input / button tag:
Replace this:
<asp:Button ID="testbtn" runat="server" Text="hide me" />
With this line:
<button type="button" id="testbtn">Hide me!</button>
As mentioned above this solution will only work if you don't need to submit any data.
What is the problem?
The button submits your form after the click so the page will be refreshed and the view will return to the default status.
How can I solve this?
You've several ways to change this behavior :
if you have got a control on the asp code you could remove the runat="server" to prevent the button from submitting :
<asp:Button ID="testbtn" Text="hide me" />
You could simply add the return false; statement at the end of the event.
Another way is preventing the default behavior using preventDefault() like :
$('#testbtn').click(function (e) {
e.preventDefault();
$('.testcls').hide();
});
The one that I recommend to you is using UseSubmitBehavior:
<asp:Button ID="testbtn" runat="server" UseSubmitBehavior="false" Text="hide me" />
Last choice is using the plain HTML code like :
<button type="button" id="testbtn">Hide me!</button>
I have a javascript handler method for giving a confirmation if page unloads.
<script language="javascript" type="text/javascript">
function handler(form)
{
confirm("are u sure?");
}
</script>
And I have used it here
<asp:Button ID="CreateTestButton" runat="server" Text="Submit" class="btn btn-default" OnUnload="handler(this)"
OnClick="CreateTestButton_Click"/>
Thanks for the help!
OnUnload is a server side event. It is called when the control unloads from memory on the server. You can't use it to call a JavaScript function.
I think your misunderstanding comes from the difference between server side and client side. In web frameworks, your server side code executes first and renders HTML (along with CSS and JavaScript). That's all sent to the client where it is then executed in the browser.
If you want to have the user confirm before they navigate away from the page, you need to add an event handler from the appropriate event on the client side. Like this:
window.onbeforeunload = function() {
// Do something
}
See this question for more detail.
Keep in mind that you should not abuse this capability by asking them on every page. You typically only do this if there is important page state, such as the user has partially filled out a form.
Well, may be you can do something like below,
<form action="YourPageName.aspx" method="post">
<input name="TextBox1" type="text" value="" id="TextBox1" />
<input name="TextBox2" type="password" id="TextBox2" />
<input type="submit" name="Button1" value="Button" id="Button1" />
</form>
I want to disable link button after user click on it.
But I made something wrong here.
Button is still enabled after click. What am I doing wrong?
<asp:LinkButton ID="submit" runat="server"
CssClass="button"
Text="OK"
OnClick="Submit_Click"
OnClientClick="this.disabled=true">
</asp:LinkButton>
Just give return false; to your JavaScript. The reason is that your asp:LinkButton is getting rendered like this.
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$submit','')"
class="button"
id="ContentPlaceHolder1_submit"
onclick="this.disabled = true;return false;">
OK</a>
So, if you do not give return false, these things happens
It fires the onclick event and disables the anchor tag. (anchor tag does not have a disabled property. button and input type="submit" has that property.)
It moves on to fire the postback and hits the server side click event
As there is a postback, the client side JavaScript disabling wont persist (even if it were a button)
By enforcing return false you are asking the asp:LinkButton not to do anymore processing.
P.S: I have been using PostBack Ritalin to prevent multiple clicks on asp:Button. IMO, its definitely worth a look in this case.
Use the javascript debugger to check the method is being called. LinkButtons are server generated javascript powered anchor tags & therefore can be tricky.
Try this java script function ,
On your OnClientClick
<asp:LinkButton ID="submit" CssClass="button" Text="OK" onclick="myButtonId_Click(this);return false;" ></asp:LinkButton>
add function ,
protected void myButtonId_Click(target) {
target.disabled = true;
}
I would like to bind my object to my view using #Html.DisplayFor helpers. Then click an edit button and change them to #Html.DisplayTextBoxFor helpers. I want to make this change client side.
For Example: Using a View like below
#Using Html.BeginForm("profile", "user", FormMethod.Post)
#<h2>User Profile</h2>
#<div>
#Html.LabelFor(function(m) m.email)
#Html.DisplayFor(function(m) m.email)
</div>
<input type="button" class="btn-primary" value="Edit Mode" />
<input type="submit" class="btn-primary" value="Save" />
End Using
On clicking the "Edit Mode" button, I want the email to become an editable textbox. Which I will then submit back in my form.
What you are asking is not possible. You cannot run server code on the client.
Therefore, you have to choose another option:
Use a TextBox, also for showing the content, disable and enable them when needed;
Use a AJAX call to load the edit form from the server. Create an Action that returns a PartialView to output and load it in place of the display part (you can use jQuery for example to get the actual results from the server);
Do a full postback to the server and reload the entire page.
You do not have to use is DisplayFor() in this case
#Html.TextBoxFor(x => x.email, new {#readonly = "true", id = "email"})
<input type="button" class="btn-primary" value="Edit Mode" id="edit" />
Then with JQuery
<script>
$('#edit').on('click', function () {
$('#email').attr('readonly', 'false');
});
</script>
You will need to add an id to the button to access.
So right now it all looks pretty with ...
<button type="submit" runat="server" name="subscribe" id="Button1" class="link-button" onserverclick="saveListing">
Until it is time to validate the data before calling saveListing function codebehind (in VB .Net).
How can true/false be return so that when true saveListing will be called, otherwise not?
Thank you.
add onclick='if(! validateFunction()) return false;.
I can't recall correctly whether HTML button control had onclicentclick property. Surely, if the web control <asp:Button ... it has a property OnClientClick where you can give the same value above for this effect as well.