Displaying Asp.net AJAX Calendar Extender Two Ways - asp.net

Is there a way to set up the calendar extender so that the calendar displays when the text box recieves focus AND when the when the element with the "PopupButtonID" is clicked? With my current settings it seems to be one or the other.

It's a bit of an ugly way to do it, but you can do this if you're prepared to use two extenders.
<asp:TextBox runat="server" ID="DateTextBox" />
<asp:ImageButton runat="server" ID="CalendarImageButton" ImageUrl="~/date_16x16.gif" />
<ajaxtoolkit:CalendarExtender runat="server" id="Extender1" TargetControlID="DateTextBox"/>
<ajaxtoolkit:CalendarExtender runat="server" ID="Extender2" TargetControlID="DateTextBox" PopupButtonID="CalendarImageButton" />
This way the calendar will appear whether you focus on the textbox or click the imagebutton.

Not that I'm aware of; it's one or the other. The only way I can think of is set it to use the popup control, then add a JS event handler for the textbox focus, and manually find the calendar extender, and there may be a show() method so you could manually invoke the trigger potentially. Not 100% sure. To find out do:
function textboxFocus() {
var c = $find("<%= calextenderid.ClientID %>");
//can use firebug to see if c.open method exists, or check for something else
}
Again, never done it, so not 100% sure.
HTH.

Use the CalendarExtender's show() method, referencing it by BehaviorID:
<asp:TextBox runat="server" ID="DateTextBox" />
<asp:ImageButton runat="server" ImageUrl="~/date_16x16.gif" OnClientClick="$find('Extender1').show();return false;" />
<ajaxtoolkit:CalendarExtenderrunat="server" id="Extender1" BehaviorID="Extender1" TargetControlID="DateTextBox"/>

Related

Prevent webform postback onclick button

I have a webform with an asp button that when clicked, needs to perform a function in c#. The function does not affect anything on the page so I do not need a refresh of the page.Is there any way to prevent an asp button from doing a postback? I am not familiar at all with JavaScript so I need to perform this function in c#. Surely there is a way. I have researched but nothing works. CausesValidation="false"' does not work. UseSubmitBehavior=false does not work. And neither does setting the OnClientClick to return false. Keep in mind I am not using JavaScript. Anyone know how?
Add a ScriptManager in your Page first,
Then add an update panel which will have all the controls. This will prevent the entire page being posted back.
here goes an example
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="pnlHelloWorld" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="lblHelloWorld" Text="Click the button!" />
<asp:Button runat="server" ID="btnHelloWorld" OnClick="btnHelloWorld_Click" Text="Update label!" />
</ContentTemplate>
</asp:UpdatePanel>
If you do not want to monkey with the ScriptManager or the server side stuff, you can always use an HTML control as in:
<input type="button" id="myButton" value="Do Something" onclick="doSomeFunction(); return false;" />
OR
<button id="myButton" onclick="doSomeFunction(); return false;">Do Something</button>
I did discover that attaching the click event to a button element via jQuery will result in a postback (at least that's what happened to me), so I switched to the tried and true input element.

RequiredFieldValidator have to click twice

I have run into the same problem as described here.
Only the question is marked as answered with only an explanation as to why you may have to click twice when using a RequiredFieldValidator on input fields - once as the blur of a textbox(for example) will correct the validation and then again to actually post the form.
I don't want to have to click a button twice! Does anyone know a solution or workaround to this?
You could add EnableClientScript=false to the validator.
That prevents the client-side validation, so you will always get a postback (which may not exactly be what you want, though). The validation will still be done, just server-side.
Be sure to wrap the button-click logic in a if (Page.IsValid) { ... }, to check the status of the validators.
Apologies for not posting code previously I assumed this to be a standard problem with the RequiredFieldValidator, but have since realised my particular problem was coming from a CompareValidator, used to ensure entered passwords matched.
The CompareValidator was causing the issue that I described, causing me to have to click away from the field to blur and validate, before being able to click on the post button.
I'm not sure why but changing the Display of the CompareValidator from Dynamic to Static has cleared the problem up.
If the validator is Display="Dynamic", and the appearance of the error message causes the submit button to move, then the MouseUp event is not received by the submit button. In this case, the validation fires and clears the error message, but the submit button does not fire. To solve the problem, either set the the validators to be Display="Static", or rearrange the form so that the submit button does not move when error messages appear.
Here's a way to reserve about one, vertical line of space for a dynamic validation message:
<div style="height:1.5em;overflow:visible;">
<asp:RequiredFieldValidator ID="R1" runat="server"
ErrorMessage="Name is required" ControlToValidate="TextBoxName"
Display="Dynamic"></asp:RequiredFieldValidator>
</div>
I did not find it necessary to set EnableClientScript="false", although that did help for a CustomValidator that had no client-side validation function implemented.
Posting your code is always a good idea, That way we could run your code in a test environment and modify it to ensure it works before posting our answer.
I would suggest adding
causesValidation="true"
to your button to see if that works.
I have a better idea.
Add Text="" to textbox Control.
Add InitialValue="" to Validator Control.
What it will do, when it will be posting, it will find the value of the text box is still the initail value and it will throw an error and the form will not be posted.
Try this:
<asp:RequiredFieldValidator ID="reqFieldCloseComment" ControlToValidate="tbCloseComment" ValidationGroup="ChangeStatus" ErrorMessage="Please enter a reason" Display="Dynamic" runat="server" InitialValue=""></asp:RequiredFieldValidator>
<asp:TextBox ID="tbCloseComment" runat="server" CausesValidation="true" TextMode="MultiLine" Height="107px" Width="400px" Text=""></asp:TextBox>
<asp:Button ID="btnCloseRequestFinal" Text="Finish" CssClass="CloseReqButton" runat="server" ValidationGroup="ChangeStatus" />
Here is code that is working fine for me and helping to get rid of double click.
<asp:TextBox ID="TextBox1" runat="server" autocomplete="off"
Enabled="true" MaxLength="20" onfocus="SetActiveControl(this);" Text=""
CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1" Display="Static" ErrorMessage="Ha!" SetFocusOnError="True" EnableClientScript="true" ForeColor="" InitialValue="" />
$(function() {
$("input.btn").on("click",function(){
if(Page_BlockSubmit == true) {Page_BlockSubmit = false};
})
});
Page_BlockSubmit is a JS variable defined by the js generated from code behind when you define the validator . I haven't went deeper to know why MS need this variable, but the scenario is:
the first click will make Page_BlockSubmit become false.
the second click will check the value of Page_BlockSubmit and return true.
if you implemented the code I posted, every time you click the button, the variable will be set as false which will trigger the submit at every click.
And, you can use google chrome to trace the value of Page_BlockSubmit.

Postback using button in GridView

I have the following in an ASP.NET GridView:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ibtnDown" runat="server" ToolTip="Down" CommandName="Down"
ImageUrl="~/images/arrow.png" Width="16px" Height="16px"
CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
It's simply an image button that when clicked raises the RowCommand event for the GridView. It is very convenient in that it allows me to use the CommandName and CommandArgument in order to determine the button that was clicked and for which row it was clicked.
However, I find that the .NET ImageButton is limited in that it can't display CSS sprites in order to change the image on mouseover. At least I couldn't get it to work, but of course that doesn't preclude some manner in which it's possible.
Anyway, I'd like to use a standard HTML button tag because I know how to get it to work with my sprites.
<button name="btnDown" id="btnDown" type="submit" class="downArrow"></button>
But what I don't know how to do is get the button to cause a postback and raise the RowCommand event and still allow me to somehow access the CommandArgument and CommandName parameters.
Right I would make the button a server control first, then use its various properties as an alternative to commandName and CommandArgument. Additionally I would use OnServerClick event in place of rowCommand. I would use at last the sender object parse it to an hmt button and extract the values out of its properties. I think I'vent got lost.
<button runat="server" name="Down" id="btnDown" title="<%# Container.DataItemIndex %>" type="submit" class="downArrow" OnServerClick="ClickMe"></button
Have you considered using a button field to perform the action you wish to do?
Take a look at the following link
ButtonFields within a GridView
Just use (original post here):
<asp:LinkButton ID="LinkButton1" runat="server">
<div class="sprite-box_mac_osx_disc_button" />
</asp:LinkButton>
Of course use your own css with all width, height, background and background-position. And add Command and CommandArgument to LinkButton.

How not to trigger RequiredFieldValidator on hidden field (TextBox)

I think this is a common problem. I have a form where I show/hide fields dynamically using jQuery, depending on some radio buttons.
I have RequiredFieldValidator's on all the fields, but I don't want them to be triggered if their ControlToValidate is hidden (using jQuery).
Is that possible? Thanks in advance.
EDIT: Here is the solution, thanks to Marek. It might not be very obvious if you have weird clientIDs because of MasterPages
This is the ASPX
<asp:TextBox ID="txtName" runat="server" />
<asp:RequiredFieldValidator ID="vldName" ControlToValidate="txtName" runat="server" ErrorMessage="You must enter Name!" />
...
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
This is the jQuery
$(function() {
$('#ctl00_cphContent_btnSubmit').click(function() {
if (!$('#ctl00_cphContent_txtName').is(':visible'))
ValidatorEnable(ctl00_cphContent_vldName, false);
});
});
Hope it will make someone's life easier
If I remember correctly there's a function called ValidatorEnable(validatorClientId, isEnabled) that allows you to disable/enable the ASP.NET validators via javascript. You could use jQuery right before your form submit to disable all your invisible validators.
There's some documentation about client side API available from the validators here http://msdn.microsoft.com/en-us/library/aa479045.aspx
Hey u can set the control to validate property from C# code when u r setting visiblility from C# code.This wil solves ur problem.
if ($('#input-name').length) {
// do something
}
This will now solves ur problem

queue asp.net UpdatePanel postbacks

is there a way to queue postbacks with UpdatePanel?
I have a form with many textboxes. each textbox is wrapped inside it's own UpdatePanel with AutoPostBack set to true. so, when textbox changes, postback occurs.
viewstate is disabled (so do not need to worry about it).
the problem appears when user changes one text box and then quickly tabs to the next text box, changes it, and tabs again. some of the postbacks are getting lost and I want to avoid that.
You can get a client-side "hook" when the update panel is about to fire. This means that you could, at least, temporarily disable the text boxes (or have some sort of 'please wait' notification) while the update panel is refreshing.
The following snippet of ASP.NET/Javascript shows how to intercept the update panels firing and disable the textboxes.
<form id="form1" runat="server">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<div>
<asp:UpdatePanel runat="server" ID="updatePane1">
<ContentTemplate>
<asp:TextBox runat="server" ID="textBox1" AutoPostBack="true" OnTextChanged="textBox_TextChanged" />
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:UpdatePanel runat="server" ID="updatePane2">
<ContentTemplate>
<asp:TextBox runat="server" ID="textBox2" AutoPostBack="true" OnTextChanged="textBox_TextChanged" />
</ContentTemplate>
</asp:UpdatePanel>
<br />
</div>
</form>
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequest);
function InitializeRequest(sender, args) {
if (args._postBackElement.id == 'textBox1' || args._postBackElement.id == 'textBox2') {
document.getElementById('textBox1').disabled = true;
document.getElementById('textBox2').disabled = true;
}
}
</script>
I know this isn't exactly what you originally asked for ("is there a way to queue postbacks with UpdatePanel"), but the net effect is that it forces the user to queue up their requests so no more than one is being processed at a time. You can probably amend this to something more elegant too.
There's no built in way to control this. jQuery has some pretty nifty stuff that makes AJAX calls pretty simple. You might try looking into handling your own postbacks that way.
I know, this is not a answer to your question. But I would recommend a re-think, if you really need to wrap each text box with an update panel. Update panels are useful but you need to be careful in their usage. An plain jQuery Ajax solution may be better in your scenario.

Resources