There are several linkbutton controls in my masterpage, I bind click event to redirect to other pages, it works. But if my content page has Validator, it doesn't works. When I click the linkbutton, the validator will stop the event, what should I do to resolve it? Thanks.
Page.aspx
<asp:LinkButton ID="lnkbtnHomePage" runat="server" Font-Underline="true" ForeColor="White" OnClick="lnkbtnHomePage_Click">HomePage</asp:LinkButton>
Page.aspx.cs
protected void lnkbtnHomePage_Click(object sender, EventArgs e)
{
Response.Redirect("Index.aspx");
}
There are two options which you can use for this.
The first is to add the attribute CausesValidation="false" to the buttons which you do not want to trigger any validation on the page.
The second option is to add the ValidationGroup="[groupname]" attribute to the controls and buttons which you want to trigger the validation.
The first option is good if you have a one or two buttons on the page which shouldn't trigger the validation, however the second options gives you a safeguard against you adding a new controls which could interfere with your validation.
The second option is also very handy when you have different sections of a page which have their own validation groups and shouldn't effect others. For example if you have some standard controls on your master page which are shown throughout your site, but then have individual pages with their own forms. You can easily control their behaviour.
However, as Yassine Houssni has mentioned, you would be better off just using standard <a> tags or even <asp:HyperLink> controls for simple behaviour as changing pages. If your link suddenly changes, then you will need to recompile the project rather than being able to update the link within the .aspx/master page.
hope this help http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx
u can pass validation for link button
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.causesvalidation.aspx
I'm looking to create a custom date picker with code examples from several sources.
Is the code to display/hide an ASP.NET control when a user clicks a button usually done with JavaScript or ASP.NET code? By display/hide, I mean within the page not a popup window.
Please provide a simple example. (If ASP.NET, VB example preferred over C#)
The answer is, it depends. Do you want the date picker show/hide to trigger a postback and thus some code on the server, or do you want it to act purely on the client?
If you want it to act purely on the client, then, modify the markup for your button:
<asp:Button runat="server" ID="myButton" OnClientClick="ShowHideCalendar()" Text="myButton" />
<script language="javascript" type="text/javascript">
var calendarVisible = false;
function ShowHideCalendar()
{
if (calendarVisible)
{
// Code to *SHOW* calendar here
// Show the DIV it's contained in, pop the window with it in, etc..
}
else
{
// Code to *HIDE* the calendar here
}
}
</script>
The key bit is the "OnClientClick" property of the asp:Button control.
Its best practice to do such thing asynchronously, rather than having a full postback that refreshs the entire page.
That means that you have two options:
Update an UpdatePanel in which your
control is placed. That gives you
the benefit of only re-rendering the
content in the UpdatePanel.
Use
clientside scripts to toggle the
control. You also need to perform a
callback, that tells your codebehind
that you just toggled the visibility
to asure your code is in the same
state as the webpage displaying it.
I'd prefer using the second one.
So far, I've seen (and I'm using the following) scripts to show/hide a div or other controls depending on another control in ASP.NET
$('[id$=myRadio_0]').click(function() { $('[id$=myDiv]').show(); });
$('[id$=myRadio_1]').click(function() { $('[id$=myDiv]').hide(); });
and of course, my div in html like
<div id="myDiv" runat="server" visible="false">
and that works fine when the user selects either option of the radiobuttonlist.
However, when I assign that radiobuttonlist a value of 1 or yes on my Page_Load on code behind, that isn't (and probably can't be) caught by jQuery, and my div remains invisible even though the control has a value of Yes/1.
So, do I need to set the visibility of that div from code behind, or is there a way in jQuery to force a scan of these dependencies after i've set the values for the main controls in code behind?
Couple of things you can do.
1) Change your div to a Panel server control (<asp:Panel id="myDiv" runat="server">), with the ID of "myDiv". Then in the code behind, when you set the radiobutton to 1, you can also set your panel control's visibility. Your current jQuery code will still work.
2) Write another line of jquery to test for div visibility when the page loads. Something like,
if ($('[id$=myRadio_0]').val() == 1 && $('[id$=myDiv]:hidden')
{
$('[id$=myDiv]').show();
}
Personally I'd go for option 1, since you're already dealing with setting up the state of your form in the codebehind, I wouldn't split the "setup" code into both the client and the server code.
Some weird stuff is happening, I am converting an application that used to use javascript to open another web page in a tiny window for data input to use a ModalPopupExtender.
It seems to work fine, but in the OK event, when I do txtData.Text (the textbox in my modal popup), it comes back with a comma before the data, so if you type "Rabbit", it comes back as ",Rabbit".
Also when I use it multiple times, in another place where I might click to show it, and type "Fish", it starts coming back with stuff like ",Rabbit,,Fish"
I don't know why or how to stop it from doing this... any ideas?
Same here. No clue for why it happens. Every postback initiated by buttons within the panel adds commas and previous values to all text fields.
Hooking into TextBox.Text setter revealed that the corrupted data comes from postdata collection. So it means that just before postback the ModalPopupExtender corrupts the data.
P.S. I'm not using UpdatePanel, but regular Panel so there are no triggers associated with buttons.
Updated: Solution found.
The problem seems to go away when rolling back to May's release of AjaxToolKit (http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326).
Just sharing this solution to everyone on this problem. Try not to use asp control instead use html.
*<input type="text" id="txtID" runat="server" class="myClass" />*
this works fine for me.
Thanks,
I also found a forum indicating that it may be standard html behaviour for when there are multiple controls on the form with the same name. This in mind (and assuming there is a bug in the ajax controls) The way I coded around it was to add in my Page_Load the following kind of statement for each of my textboxes.
string[] vals = txtValue.Text.Split(Convert.ToChar(","));
txtValue.Text = vals[vals.Length - 1];//It appears my latest value was always in the last item
Since the form load happens before the button event I sort out my fields before they get to the event that deals with their values.
I had a similar problem, having a jQuery Dialog inside an UpdatePanel. As I could read on different sites, the problem is caused by duplicates inside the DOM tree.
In the end I found a very simple solution. I assign the dialog to the div, then open or close it by JavaScript and then run this little peace of code to remove the duplicates:
var count = $(".dialog").length;
for (i = 1; i < count; i++) {
$(".dialog").first().remove();
}
EDIT: it turned out not to be SO simple. In the end, my code looked like this:
In document ready (and also asynchronous page calls):
function AddDialog() {
var dlg = $(".dialog").dialog({ autoOpen: false });
dlg.parent().appendTo($("form:first"));
var targetSelector = ".myDialog"; // watch out: can't use ID here!
if (mustOpenDialog) {
$(targetSelector).last().remove(); //-- remove last copy
var dlg = $(targetSelector).dialog({ autoOpen: true });
var count = $(targetSelector).length;
for (i = 1; i < count; i++) {
$(targetSelector).last().remove();
}
}
if (mustCloseDialog) {
$(targetSelector).dialog("close");
var count = $(targetSelector).length;
for (i = 1; i < count; i++) {
$(targetSelector).first().remove();
}
}
}
In my complete code, mustOpenDialog and mustCloseDialog are set in codebehind.
I had the same problem with previous values coming back comma separated. It seemed that my ok button was inside the update panel and I had it in the triggers section aswell. Removing the button from the triggers section of the updatepanel solved the problem.
best regards - Tobias
My answer was similar to Smarty's. My page looked like this...
<UpdatePanel>
<GridView>
<Button> <-- These buttons fire modal popup programmatically.
<Button>
<Button>
</GridView>
<ModalPopup>
<HiddenField> <-- Dummy target of modal popup.
</UpdatePanel>
The fix was to change it to this...
<UpdatePanel>
<GridView>
<Button> <-- These buttons fire modal popup programmatically.
<Button>
<Button>
</GridView>
</UpdatePanel>
<ModalPopup>
<HiddenField> <-- Dummy target of modal popup.
I had to register each button as a postback control with the scriptmanager on the gridview rowdatabound event. The down side is that I have more full postbacks. But, it solved the problem.
This is pretty late reply, but I am documenting it here so that other may benefit.
My Scenario
Open user control inside jquery dialog on button click. This user control had update panel inside it and few textboxes. First time open the dialog, it worked like charm. On subsequent clicks to open the dialog, I noticed a weird behavior. I had few textboxes inside the user control(inside update panel). On any partial post back, the text of textboxes changed to current text, current text. If the value of textbox was fish, then upon any partial postbacks its changed to fish, fish.
The reason for this was I was using jquery to open the dialog. I also appended the dialog to form upon the click of the button.
dlg.parent().appendTo($('form:first'));
So on subsequent clicks multiple user controls where appended to DOM and hence while building the post back data, there existed more than one control with same id and hence there values where appended using "," - comma.
So my solution was simple, while closing the dialog, I just removed it from DOM
I got hint from following link. Posting it here for future reference
here
For some reason it doesn't seem to happen if the textbox is set to ReadOnly.
I'm thinking there could be a workaround by displaying an editable textbox to the user, catching the keystrokes to it, and updating a readonly textbox that is hidden from the user.
Still a bit messy, but I can't roll back to May's release because there's another bug in that release with the ComboBox that I need to avoid!
UPDATE:
As a bit of background, I have a user control (ascx) inside my modal popup because I need to reuse it. The ascx has to handle the user's input itself (the containing page doesn't know what's going on inside the control) so when the user clicks a button I do a callback and process the data. If a successful result is returned to the client callback function then I simulate a click of what the containing page thinks is the "OK" button which is actually invisible to the user.
I've changed my code to add a hidden, readonly textbox and copy the text from the original textbox into the new one every time the text changes.
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>
becomes
<asp:TextBox runat="server" ID="txtName" onchange="document.getElementById(this.id + 'RO').value = this.value"></asp:TextBox>
<asp:TextBox runat="server" ID="txtNameRO" ReadOnly="true" style="display:none;"></asp:TextBox>
then when passing values back in the callback instead of getting the value of txtName, I use txtNameRO.
I don't think this will help if you're doing a postback, but you could add a callback before the postback like I have. Hopefully this will help someone anyway!
I had this issue and lost quite a bit of time on it and found that it was caused by an extra tag hiding out that I forgot to remove when changing markup.
Make sure on the .aspx page all tags line up correctly.
<div>
**Lots of code here**
</div> <-- That guy owes me a vacation!
</div>
I have successfully implemented a complete hack based on Jen's response. I use an asp:HiddenField to hold the value I want to post back, and I populate it with a pure HTML input box
<asp:HiddenField ID="txt" runat="server"/>
<input type="text" onchange='document.getElementById("<%= txt.ClientID %>").value = this.value;' />
This is lighter weight than Jen's solution as you're still only posting back one server control.
BTW, this is a very significant bug in the Ajax Toolkit. You can vote and comment on it here:
CodePlex Issue
Pretend you're in Chicago. Vote early, vote often.
For me the solution was to get the modal poup out of the main update panel. Once I did that the commas went away. If you truely need an update panel inside of the modal popup, create one of it's own inside that panel.
I did not need to roll back to previous version of the toolkit.
I have been trying to solve this for a day now and some very helpful person in my office has come up with a terrific solution!
We moved the ModalPopUpExtender and subsequent pop up panel controlled by the extender to ouside of the outermost UpdatePanel on the page, created a fake button and gave the ID of the fake button to the TargetControlID property of the ModalPopUpExtender.
<asp:Button runat="server" ID="dummyButton" CausesValidation="false" Style="display: none" />
<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="dummyButton" CancelControlID="CancelPopUp" PopupControlID="PanelID" BackgroundCssClass="modalBackground" />
Inside my forms/page where the PopUp extender and panels used to be, I created an OnClick event on the button that we used to use to trigger the ModalPopUpExtender.
<asp:ImageButton ID="ButtonID" runat="server" Text="Add" ImageUrl="~/Images/Add-32-1-small.png" OnClick="LoadPopUp" />
On my button that triggers the ModalPopUpExtender, I have created an OnClick event called "LoadPopUp" - in LoadPopUp in the code behind I simply put ModalPopUpExtender1.Show();
protected void LoadPopUp(object sender, EventArgs e)
{
ModalPopupExtender1.Show();
}
I had the same problem except I am not using the AjaxControlToolkit or update panels. My problem was solved by moving the <form> tag from the ASP.net master page to the page where the textboxes are. May help someone.
has same problem and i found the solution here: http://ajaxcontroltoolkit.codeplex.com/workitem/26259
The problem is ajax last version, just INSTALL year 2009 ajax version and modalpopup will work fine with your actual code.
Tuve el mismo problema y encontré la solución en el link que indico arriba.
El problema viene con las versiones recientes de ajax, simplemente instala la version del año 2009 y el modalpopup funcionará bien con tu código actual.
The solution is to get the modal popup out of the main update panel. Once Do that the commas will go away. If you truly need an update panel inside of the modal popup, create one of it's own inside that panel.
I have a control that on 1 page is initially shown, but on another page is initially hidden.
For the page that initially shows the control, the javascript in the control works fine, but for the page that initially hides the control, the javascript doesn't work.
I've ended up registering the javascript in the code behind using:
this.Page.ClientScript.RegisterClientScriptBlock(...);
It works fine, but it gets ugly with large javascript blocks. Plus it I want to change it, I have to rebuild the solution, which I'd rather not have to do.
Is there another way to register javascript using markup if it isn't sent to the browser when the page is loaded? I'm trying to keep network traffic slim by only sending what I need to the browser when the page loads.
thanks,
Mark
The issue is not making the button invisible, but having an invisible button that one can click on from javascript, and having the button call it's event handler.
ex:
<asp:button id="button1" runat="server" style="display:none">
is something you should use or you can setting up a css class with display:none in it instead and then assign that to your control.
I assume you're getting the control by using document.getElementById, if that is the case, you can check if it is null before using it:
var ctrl = document.getElementById("someID");
if (ctrl) {
do something here...
}
If your control is hidden by setting its server side property 'Visible' to false, then the browser can't attach the js to anything because ASP.NET doesn't render the control. You could try setting the CSS property 'display' to 'none'.