asp.net OnClientClick not rendered for initially disabled Button - c#

I have a disabled asp.Button, which I enable later with JavaScript. Like this
<asp:Button ID="btnSave" runat="server" Text="Save" Enabled="false" OnClientClick="; return ValidateFields();" OnClick="btnSave_Clicked" />
However, the "onclick" method is not rendered as html when the control is disabled. My work around is to add the following code in PageLoad.
btnSave.Attributes["onclick"] = "return ValidateFields();";
Is there a more convenient work around for this?
Thanks

You can use the html attribute for disabled
<asp:Button disabled="disabled" ID="btnSave" runat="server" Text="Save" Enabled="false" OnClientClick="; return ValidateFields();" OnClick="btnSave_Clicked" />
I assume you then make it enabled in clientside? if so then you can enable it with :
document.getElementById('MainContent_btnSave').removeAttribute('disabled'); //or jquery alternative where MainContent_btnSave is the clientid of the control

Well, it's obvious why the framework behaves like this: if a button is disabled, why it should have an onclick event handler? The user can't click it if it's disabled! Because of this, the framework removes the attribute.
So, I don't think you have other options besides appending the attribute manually.

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.

how to put 2 forms (run at server) in one page (asp.net, c#)

I'm trying to build html page with 2 forms in it, that both of them will run at server, how can I do that, or there is something else I can do instead?
Thanks.
(Tell me if you want to see the code).
I noticed you just said ASP .NET instead of picking a specific ASP .NET technology: WebForms or MVC.
In WebForms, unfortunately not. However, you can create an asp:Panel and use the DefaultButton attribute to contain the scope of each separate form.
In MVC, it's extremely easy since you use the Form tag. In ASP.NET MVC you can implement as many forms as you want (and it is valid & correct behavior of web pages).
EDIT:For comment
<asp:Panel ID="Panel1" runat="server" DefaultButton = "Button1">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick = "Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick = "Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="Button" OnClick = "Button3_Click" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:Panel>
Like this you can use more than one panel on the same page for some group of server control and you can set DefaultButton for handling server side event.
The same can be done from code behind
C#
Panel1.DefaultButton = "Button1";
VB.Net
Panel1.DefaultButton = "Button1"
Default Button property ensures that whenever user presses Enter key the button that is set as default will be triggered.

ValidationGroup Not Firing

I have a text box with required field validator:-
<asp:TextBox ID="txtCName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCName" runat="server" ErrorMessage="Pleas Enter Name" ControlToValidate="txtCName" ></asp:RequiredFieldValidator>
I need to show error message on button click:-
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
ValidationGroup="sub">
I am using the following code to assign validation group to the required field validator (it is in document.ready() function) :-
$("#<%=rfvCName.ClientID %>").attr('ValidationGroup', 'sub');
But it is not showing any error message on button click. Please help me.
I need to do it using Jquery. I don't want to assign ValidatioGroup directly to the control.
Add validation group ValidationGroup="Sub"
<asp:RequiredFieldValidator ID="rfvCName" runat="server" ErrorMessage="Pleas Enter Name" ValidationGroup="sub" ControlToValidate="txtCName" ></asp:RequiredFieldValidator>
when the page(server side) prerender the object requiredFieldValidator, some javascript is added into page automatically by .net framework.
this javascrript code check the validationgroup.
add manually the validationgroup attribute client-side without this javascript code not work.
you can see this code in developpers tool searching "ValidationGroup"
if you cannot add attribute in html, you can add this attribute in a event server side like load or prerender.

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();.

How to execute in JavaScript function in ASP.NET using C#

i have a textarea which i want to disable when I check a checkbox. I made a working demo in plain HTML and JavaScript , but When I am migrating to Asp.net, neither the function is being executed nor I can keep the OnSelect event in my check box.
<asp:CheckBox ID="ChkOthers" runat="server" Text="Others(Please Explain)" Width="205px" AutoPostBack="True" OnCheckedChanged="ChkOthers_CheckedChanged" />
<asp:TextBox ID="TxtOtherReason" runat="server" MaxLength="1024" TextMode="MultiLine" />
i want to create client side validation when check box is checked textbox is enable else disable.
I guess you problem is in the id of the controls so here is my solution for your problem
//Remove the AutoPostBack property and the OnCheckedChanged="ChkOthers_CheckedChanged"
<asp:CheckBox ID="ChkOthers" runat="server" Text="Others(Please Explain)" Width="205px" OnClick="javascript:YourFunction()" />
<asp:TextBox ID="TxtOtherReason" runat="server" MaxLength="1024" TextMode="MultiLine" />
and put the following javascript to handle the enable and diable of the textbox
<script>
function YourFunction() {
document.getElementById('<%= TxtOtherReason.ClientID %>').disabled = document.getElementById('<%= ChkOthers.ClientID %>').checked == false ? true : false;
}
</script>
It has been a while for me since I used ASP.NET but from memory there is something like an "OnClientClick" event and a validation property in asp.net properties for a control that you can use to call client side JavaScript code..
Sorry for the vague answer - perhaps someone can expand on it.
You are using a server side handling of the event. Use a client side handling with "OnClientClick". You can do something like:
<asp:CheckBox ID="ChkOthers" runat="server"
Text="Others(Please Explain)"
Width="205px"
AutoPostBack="True"
OnClientClick="alert(this.checked);"
OnCheckedChanged="ChkOthers_CheckedChanged" />
It seems this also does not work. I am yet to prove.
Also see a similar post on StackOverflow: OnClick vs OnClientClick for an asp:CheckBox? and there is also an issue in MS site: http://connect.microsoft.com/VisualStudio/feedback/details/103398/checkbox-onclientclick
you can put the code in cs files .after it loaded you can make the onclientclick method to an exist javascript function .
and in the javascript function you can get the text box use the name or the simple id which you can get from the source file the server gives to the browser!

Categories

Resources