How to get the client id with ct100$ - postback

This is the extension to my previously asked question. I think I am not getting an answer. So I am describing it again.
I have this button inside radajaxpanel. I want to postback and I am am using the javascript code. Everything works fine
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="OnBtnExcel_Click" OnClientClick="PostBack(); return false;" />
rendered as
<input type="button" name="ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$btnSave" value="Save" onclick="PostBack(); return false;__doPostBack('ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$btnSave','')" id="MainContent_MainContent_MainContent_btnSave">
I am hard coding the id of button as 'ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$btnSave' in the code below. is there a way to capture this ?
function PostBack() {
$find("<%= RadAjaxPanelFilter.ClientID %>").__doPostBack('ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$btnSave', '');
}

Related

Unable to Redirect to different page on click ? mvc4

I am trying to return to a different page on button click but i'm unable to achieve . It hits the controller action method(onclick) but the view remains the same & url in the browser remains unchanged .
Html:
<input type="submit" class="btn btn-primary" value="Login" onclick="window.location.href='#Url.Action("Home", "Index")' "/>
Even when i keep http://www.google.com it doesnt seems to work .
am i missing anything important here
An input of type submit will try to submit a form. The JavaScript you have attached to that button will run but then immediately get "overridden" by the button trying to do something else. You can do a couple of things:
Option 1: Use a different type such as button:
<input type="button" onclick="window.location.href='#Url.Action("Home", "Index")' "/>
Option 2: Return false in your JavaScript, this prevents the default action from happening.
<input type="submit" class="btn btn-primary" value="Login"
onclick="window.location.href='#Url.Action("Home", "Index"); return false;' "/>
A nice description of preventing the default action from happening is here.

Why Html.BeginForm in MVC4 is affected by the button click event,does it happen in MVC5?

My web page have a button and a form,I use #Html.BeginForm to produce the form and when I click the button,I will jump to the login page.To my surprise,when I click the button,the form post to my controller automatically.But when I use #using (Html.BeginForm,it seems OK.
My web page
<button onclick="logout()">logout</button>
#Html.BeginForm("ExportFile", "Student", new { id = "exportForm" }))
{
#Html.Hidden("year")
#Html.Hidden("fileName")
}
My new web page will work well
<button onclick="logout()">logout</button>
#using (Html.BeginForm("ExportFile", "Student", new { id = "exportForm" })))
{
#Html.Hidden("year")
#Html.Hidden("fileName")
}
I find the different HTML is System.Web.Mvc.Html.MvcForm):
<form id="exportForm" method="post" action="/Student/ExportFile">
System.Web.Mvc.Html.MvcForm) {
<input id="year" type="hidden" value="" name="year">
<input id="fileName" type="hidden" value="" name="fileName">
}
What is the difference between then?Someone teach me to add #{Html.EndForm();} when I using #Html.BeginForm,but it seems not work.May be it works in MVC3.
You can use these two methods only if you add the button type.
<button onclick="logout()" type="buttton">logout</button>
The using statement automatically adds the closing tag for the form's HTML. You should always use #using if you are using Html.BeginForm().

Can't get onclick on a button to be accepted

I currently have a link in the below form:
Change
In order to fit the look of the site in which I'm adding this link, I want to change it to a button input, as so:
<input type="button" value="Change" onclick="changeNumbers('Numbers', '#Url.Action("ChangeNumbers")')" />
However, I'm running into a snag with this second form: the single quotes around #Url.Action("ChangeNumbers") are being flagged as Unterminated string constant. Can anyone tell me what I'm doing incorrectly and how to fix it?
EDIT
It didn't occur to me to just try the page - it looks like the second form works. So now my question is - why is Visual Studio flagging this as incorrect?
You're not doing anything "incorrectly" per se, it's just that Razor isn't perfect, and things like quotes within quotes tend to cause it to freak.
One quick fix would be to store the URL in a variable and then use the variable:
#{ var url = Url.Action("ChangeNumbers"); }
<input type="button" value="Change" onclick="changeNumbers('Numbers', '#url')" />
However, an even better fix is to not use the onclick attribute at all. Put this where it belongs: in JS.
<script>
$('#myButton').on('click', function () {
changeNumbers('Numbers', '#Url.Action("ChangeNumbers")');
});
</script>
Used jQuery above, since it's included in MVC by default
I've found that to make Visual Studio happy in this scenario, the easiest thing to do is simply change the <input /> element to a <button></button> element and the error will resolve itself:
<button type="button" onclick="changeNumbers('Numbers', '#Url.Action("ChangeNumbers")')">Change</button>
Otherwise, to continue using an <input /> the markup will need to be changed to the following:
<input type="button" value="Change" onclick="#("changeNumbers('Numbers', '" + Url.Action("ChangeNumbers") + "')")" />

grails calendar reset

I installed calendar Version 1.2.0-SNAPSHOT in grails and working good .
But facing one Issue :
When click on cancel button i was doing form reset .but the calendar field is becoming clear and not reseting to default date .
<g:form url="${response}" id="program" name="program" method="post">
<calendar:datePicker name="startDate" id="startDate" defaultValue="${program.startDate}" dateFormat="%Y-%m-%d"/>
<input type="button" id="cancel" name="cancel" value="Cancel" onclick = 'reset_form(this)' />
</g:form>
function reset_form(this_)
{
document.getElementById('program').reset();
}
any one have idea please ?
thanks.
Two ways to solve this issue as far as I can see. Either correct the typo inside your JavaScript and refer to the correct id of your form (program not new_program) or use the standard reset button for forms <input type="reset" .../> instead of your javascript solution.

Redirect user to directory based on form input

I know enough about the coding end of web design to be embarrassed by what I don't know. What I want to do is to have various print promotions in newspapers and what not along the lines of: for more information please visit www.mysite.com/2345.
If the visitor doesn't enter the entire url in the nav bar and ends up at the main index, I want to have a text field there so they can enter "2345", hit enter or submit, then be redirected to www.mysite.com/2345 wherein the folder's index page will load.
I usually search and find the coding info I'm looking for, but I can't figure out a concise way to search this particular problem. Can anyone help with this or point me in the right direction for help elsewhere?
Thanks.
Pretty simple with JavaScript, here's a working example:
<form onsubmit="location.href='http://www.mysite.com/' + document.getElementById('myInput').value; return false;">
<input type="text" id="myInput" />
<input type="submit" />
</form>
You can do it using javascript. Here's a terribly ugly example but should give you an idea.
<form>
<input type="text" id="number" name="number" />
<input type="submit" onclick="window.location = window.location + '/' + number.value; return false;"/>
</form>
Ideally you'd also handle it in whatever server side language you're using as well. Here's a PHP example:
<?
if(isset($_POST['number'])){
header('Location: http://www.yourdomain.tld/'.$_POST['number']);
exit;
}
?>
Very simple example with PHP, so that you can understand how it works. Very simple.
<?php
if (isset($_POST['bt']))
{
header("Location: http://localhost/" . $_POST['folder']);
}
?>
<html>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="folder" id="folder" />
<input type="submit" name="bt" id="bt" value="Go To" />
</form>
</html>
This is you index.php File, in your htdocs/www folder.
The PHP notices when you click the button and it will redirect you to
http://www.yourdomain.com/what-you-have-writen-in-the-textfield
You can also do it with JavaScript, but with PHP it will work even if your visitor browser has JavaScript disabled.

Resources