The server tag is not well formed for repeater C# - asp.net

I try to open popup from linkbutton inside repeater. Here is my code:
<asp:Linkbutton cssclass="blue-button" id="LinkbtnPrint" runat="server" CommandName="PrintItem" CausesValidation="false" OnClick="javascript:dnnModal.show('<%# DotNetNuke.Common.Globals.NavigateURL("RptAppendixI","doanhnghiepid",Request.QueryString["doanhnghiepid"],"NamBC",NamBC2.ToString(),"ThangBC",ThangBC2.ToString(),"mid",Moduleid.ToString())+"?popUp=true" %>',false,580,950,false)">
<i class="fa fa-print" aria-hidden="true"></i>
</asp:Linkbutton>
I get the error message: "System.Web.HttpParseException: The server tag is not well formed.".
How can I fix this problem?

Its probably because you're mixing two different technologies, if you're using javascript to handle the request and the way you can avoid the exception is by removing the runat="server" attribute. After that you can handle the command that you're defining in the linkbutton in ajax call to the method from javascript, also there is this. I am not sure what are you trying to do while opening the modal but you can simplify it by creating a function inside the javascript that would handle those values instead of passing them straight away from the click event calling both js framework and dotnet framework at the same time.
OnClick="javascript:dnnModal.show('<%# DotNetNuke.Common.Globals.NavigateURL("RptAppendixI","doanhnghiepid",Request.QueryString["doanhnghiepid"],"NamBC",NamBC2.ToString(),"ThangBC",ThangBC2.ToString(),"mid",Moduleid.ToString())+"?popUp=true" %>',false,580,950,false)
if this doesn't help you could try to remove code above and do ajax call to navigate the url that you're calling on click.

Related

Prevent page refresh in ASP.NET

I have the following code in my aspx file:
<button type="button" id="btnAskQuestion" runat="server" onserverclick="btnAskQuestion_Click">Ask Question</button>
I've tried every combination of onclick="return false;" and onclick="preventDefault()" I can think of, including putting them in the javascript function that gets called. Everything I try has one of two results: either I get a postback, or the server side code (btnAskQuestion_Click) never executes.
Any idea what I might be doing wrong?
You cannot execute server-side code this way, using onserverclick causes postback.
If you wish to prevent full page refresh and still execute server-side code, you have to call a client-side JS function via onclick and execute an AJAX call from there.
Another alternative is to use your button as a trigger for UpdatePanel - this way only partial postback will be performed.
Try using the property UseSubmitBehavior="false" in the button markup.
or you can use a "trick" :
Markup
<button onclick="myFunction()">Click Me!</button>
<div style="display:none">
<asp:Button runat="server" id="btnButton" .../>
</div>
js
function myFunction()
{
if (true statement)
$("[id$=btnButton]").click();
else
alert("false");
}
What this does is that you handle stuff with normal markup and do the logic using js. And you can trigger a click of the button that do something in the server.
There're OnClick, that fires on server and OnClientClick that fires on client browser. You should do this:
<asp:Button ID="btnAskQuestion" runat="server"
OnClick="btnAskQuestion_Click"
OnClientClick="return myfunction();">Ask Question</asp:button>
If myFunction returns true, then you will have a postback to the server.
My answer is appropriate only for ASP:Button, not the button control you are working with. Given the choice, I'd switch to ASP:Button.
You're looking for OnClientClick. If you put your JavaScript code there, it will kill the PostBack before it can hit the server.
On the other hand, if you're looking to execute server code without a PostBack, that's impossible. The PostBack is what triggers the server to act.

Jquery UI Button Server + Client Click Handlers

I am trying to write a simple HTML form using asp.net and Jquery UI but am running into a problem when trying to process click event handlers on a button within this form. I was tryign to use OnClientClick and OnClick but once the clientside method gets accessed and returns the boolean the server side method is not called accordingly( not called at all actually)
Linky to code since I could not get the code tags to work properly: http://pastebin.com/LZNMqASt
I found the problem, Actually you are displaying "div#loginForm" element in to the dialog and its not taking the form element.
Put form element inside of "div#loginForm" container and the problem will be fixed.
For some reason the return type of the javascript method was not being accepted as a valid boolean. The below solution fixes the OnClientClick event
<asp:Button runat="server" ID="btnLogin" Text="Login" OnClick="btnLogin_OnClick"
OnClientClick="if(ValidateLoginForm() != true) return(false);" UseSubmitBehavior="False" />

ASP.NET URL Navigation

I have asp.net button and on it's button click I am redirecting it using
Response.Redirect ("SomeURL.aspx");
I am not passing anything to SomeURL.aspx. Can this be achieved without a roundtrip to the server?
You could use an html anchor tag. This is the simplest approach and probably the best since anchors are the proper control to allow navigation.
My link
If you still want to use the asp.net button you could do something like this
<asp:Button runat="server" ID="myButton"
OnClientClick="window.location.href='SomeURL.aspx'; return false;"
Text="Submit"></asp:Button>
You can try with this code - based on Javascript Navigate
window.navigate("SomeURL.aspx");
Sample
<input type="button" value="Navigate to SomeURL" onclick="funcNavigate();">
<script language="JavaScript">
function funcNavigate() {
window.navigate("SomeURL.aspx");
}
</script>
Can this be achieved without a roundtrip to the server?
Not using code behind.
However, you could wire up a client side click handler or use a hyperlink to accomplish the same thing.
<button onclick="window.location='SomeURL.aspx'; return false;">Some URL</a>
or
Some URL
The hyperlink is the simplest answer.

link button property to open in new tab?

In my application I have some link buttons there but when I right click on them I cannot (they are in disable mode) find the menu items Open in new tab or Open in new window.
How do I show those menu items?
Code example:
<asp:LinkButton id="lbnkVidTtile1" runat="Server" CssClass="bodytext" Text='<%#Eval("newvideotitle") %>' />
From the docs:
Use the LinkButton control to create a hyperlink-style button on the Web page. The LinkButton control has the same appearance as a HyperLink control, but has the same functionality as a Button control. If you want to link to another Web page when the control is clicked, consider using the HyperLink control.
As this isn't actually performing a link in the standard sense, there's no Target property on the control (the HyperLink control does have a Target) - it's attempting to perform a PostBack to the server from a text link.
Depending on what you are trying to do you could either:
Use a HyperLink control, and set the Target property
Provide a method to the OnClientClick property that opens a new window to the correct place.
In your code that handles the PostBack add some JavaScript to fire on PageLoad that will open a new window correct place.
Here is your Tag.
<asp:LinkButton ID="LinkButton1" runat="server">Open Test Page</asp:LinkButton>
Here is your code on the code behind.
LinkButton1.Attributes.Add("href","../Test.aspx")
LinkButton1.Attributes.Add("target","_blank")
Hope this will be helpful for someone.
Edit
To do the same with a link button inside a template field, use the following code.
Use GridView_RowDataBound event to find Link button.
Dim LB as LinkButton = e.Row.FindControl("LinkButton1")
LB.Attributes.Add("href","../Test.aspx")
LB.Attributes.Add("target","_blank")
try by Adding following onClientClick event.
OnClientClick="aspnetForm.target ='_blank';"
so on click it will call Javascript function an will open respective link in News tab.
<asp:LinkButton id="lbnkVidTtile1" OnClientClick="aspnetForm.target ='_blank';" runat="Server" CssClass="bodytext" Text='<%# Eval("newvideotitle") %>' />
This is not perfect, but it works.
<asp:LinkButton id="lbnkVidTtile1" runat="Server"
CssClass="bodytext" Text='<%# Eval("newvideotitle") %>'
OnClientClick="return PostToNewWindow();" />
<script type="text/javascript">
function PostToNewWindow()
{
originalTarget = document.forms[0].target;
document.forms[0].target='_blank';
window.setTimeout("document.forms[0].target=originalTarget;",300);
return true;
}
</script>
LinkButton executes HTTP POST operation, you cant change post target here.
Not all the browsers support posting form to a new target window.
In order to have it post, you have to change target of your "FORM".
You can use some javascript workaround to change your POST target, by changing form's target attribute, but browser will give a warning to user (IE Does), that this page is trying to post data on a new window, do you want to continue etc.
Try to find out ID of your form element in generated aspx, and you can change target like...
getElementByID('theForm').target = '_blank' or 'myNewWindow'
When the LinkButton Enabled property is false it just renders a standard hyperlink. When you right click any disabled hyperlink you don't get the option to open in anything.
try
lbnkVidTtile1.Enabled = true;
I'm sorry if I misunderstood. Could I just make sure that you understand the purpose of a LinkButton? It is to give the appearance of a HyperLink but the behaviour of a Button. This means that it will have an anchor tag, but there is JavaScript wired up that performs a PostBack to the page. If you want to link to another page then it is recommended here
that you use a standard HyperLink control.
It throws error.
Microsoft JScript runtime error: 'aspnetForm' is undefined
<asp:LinkButton ID="LinkButton1" runat="server" target="_blank">LinkButton</asp:LinkButton>
Use target="_blank" because It creates anchor markup. the following HTML is generated for above code
<a id="ctl00_ContentPlaceHolder1_LinkButton1" target="_blank" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LinkButton1','')">LinkButton</a>

Validator in <noscript> causes JavaScript error

The following .NET 3.5 code, placed in an aspx file, will trigger a JavaScript error when the page is loaded (for users who have JavaScript enabled):
<noscript>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="txt_RequiredFieldValidator" runat="server"
ControlToValidate="txt"></asp:RequiredFieldValidator>
<asp:Button ID="btn" runat="server" Text="Button" />
</noscript>
The error happens because the JavaScript generated by the ASP.NET validator control does not contain a null check on before the second code line below:
var ctl00_body_txt_RequiredFieldValidator =
document.all ?
document.all["ctl00_body_txt_RequiredFieldValidator"] :
document.getElementById("ctl00_body_txt_RequiredFieldValidator");
ctl00_body_txt_RequiredFieldValidator.controltovalidate = "ctl00_body_txt";
Can anyone suggest a workaround to this?
Footnote: Why am I doing this? For my non-JavaScript users I am replacing some AJAX functionality with some different UI components, which need validation.
You should add the following to the RequiredFieldValidator:
enableclientscript="false"
Seeing as you are using these in <noscript> tags, there is no point in supplying client side vaildation of the controls - they are only going to display if JS is turned off.
This will force the validation to happen (automatically) on the server side for you.
Just make sure you call "Page.IsValid" before you process the response.
See BaseValidator.EnableClientScript for more details.
The javascript is looking for an element contained in the noscript? AFAIK there's no clean way to detect script support from the server side.
I think you'll need to build in a redirect (I've seen this done with a meta-refresh in a header noscript if you don't mind a validation failure) to push noscript users to a "please turnscript on page" or do some surgery to loosen up the validation/control binding which may take some amount of wheel reinventing. This is one of those areas where asp.net's tight coupling between controller and view really punishes.

Resources