Inserting text in TextBox at cursor - c#

I have a textbox in aspx page in which the user enters text. Now when user clicks on a button called "Sin" the textbox should show "Sin[]" and the cursor has to be placed in between brackets.Like as follows "Sin[<cursor here>]" Now when the user clicks on some other button say "Cos" the textbox text should show "Sin[Cos[]]" and the cursor has be placed between the brackets of Cos as follows: "Sin[Cos[<cursor here>]]".
How is this handled. Any simple code please..
Thanks in advance

I hope you are using jquery.
<asp:TextBox id="txt" runat="server" />
<input type="button" id="sinBtn" value="Sin" tag="Sin[<cursor here>]" />
<input type="button" id="cosBtn" value="Cos" tag="Cos[<cursor here>]" />
//also you can generate the two buttons with server controls
Here is the javascript:
$(document).ready(function(){
$('input[tag]').click(function(){
var theText = $('#<%= txt.ClientID %>');
theText.txt(theText.txt().replace('<cursor here>',this.tag);
})
});
If you don't have a string.replace method, extend one using prototype.

I think this shouldnt be difficult with jQuery.
Look at these links:
jQuery Tutorial
inserting-a-text-where-cursor-is-using-javascript-jquery
10-jquery-and-non-jquery-javascript-rich-text-editors

Related

How to set the current tab/content on page postback

I have the following JSFiddle which changes the tab/content based on user selection: http://jsfiddle.net/sikni8/3d64w6gf/1/
HTML snippet sample:
<asp:Button ID="btnRegister" ClientIDMode="Static" runat="server" Text="Register" OnClick="btnRegister_Click" />
<!--<input type="submit" name="commit" value="Register" runat="server" />-->
The btnRegister button perform some action when clicked. When the page does a postback, how can I ensure the tab/content which was there is displayed.
For example, if I click on the Register button the page refreshes and brings the first tab/content to view. I would like to change it so that when Register button is clicked and the page reloads, it should remain with the same tab/content.
I can use sessionStorage but I would like to request some assistance.
Tried this but didn't work:
<script>
$(document).ready(function (e) {
localStorage.setItem("whichTab", $("#tabs nav ul li.tab-current"));
alert(localStorage.getItem("whichTab"));
});
</script>
I am using cbpFWTabs.js file to achieve the tab class switch which is in the fiddle.
I did something similar by using hidden field, where I stored the id/class of the current tab in hidden field and after postback I just displayed the current tab again.

ASP textbox calls javascript function

I have a search textbox in asp. And I want it to send request to the server each time the text is changed there. I have a javascript function which sends request but it is not being called each time when I type something in the text box. How can I call javascript function from ASP textbox?
That is my textbox:
<asp:TextBox ID="search" name="Search" runat="server" onchange="javascript:text_changed();"></asp:TextBox>
That is my js function:
function text_changed() {
searchedword = document.getElementById("ContentPlaceHolder1_search").value;
SendRequest();
}
You should use onKeyPress event to call the function.
<asp:TextBox ID="search" name="Search" runat="server" onKeyPress="javascript:text_changed();"></asp:TextBox>
Shivam's answer is right. You can use KeyPress event to get users key strokes with that event.
But i want to inform you, you should not use ASP.NET control ids like that :
document.getElementById("ContentPlaceHolder1_search").value;
Because you'll get errors if you put your textbox somewhere else in html hierarchy, ASP.NET regenerates it's id.
Try that one, instead :
function text_changed(textObj) {
searchedword = textObj.value;
SendRequest();
}
<asp:TextBox ID="search" name="Search" runat="server"
onKeyPress="javascript:text_changed(this);"></asp:TextBox>
The functionality you asking can achieve by
Use onkeyup or onkeydown instead.
This will then run the function when you type or click on the textbox. You can also then detect the keycode of the event, and prevent the function if you dont want it to run for certain keys.
Use the below code
$("#search").keydown(function(){
text_changed();
});
$("#search").keyup(function(){
text_changed();
});
Demo Here
give it a try :)
$("#search").change(function(){
//your ajax codes
});
<script>
function multy(val)
{
alert(val.value+"--"+val.id);
}
</script>
<input id="txtdemo" type="text" onchange="multy(this);"></br>
<input id="txtdemo" type="text" onchange="multy(this);">
</input>
Thanks... :)

Trouble checking checkbox

I have received several reports from people not being able to register on my website due to them not being able to check a checkbox stating they agree to the Terms of Use. Basically, once the checkbox is checked a button is enabled to allow one to continue with registration.
I've tried myself and a lot of the time, clicking on the actual checkbox doesn't do anything but clicking on the associated label usually does. I've given that as the workaround but I'd really like to have clicking on the actual checkbox work all of the time.
The registration page is: http://www.lonelycache.com/Account/GeocachingAuth.aspx
The page form has the following for the checkbox (formatted for readability):
<asp:CheckBox ID="AgreeTOUCheckBox"
AutoPostBack="true"
runat="server" Text=" I have read and agree to the"
OnCheckedChanged="AgreeTOUCheckBox_OnCheckChanged" />
The generated html is (formatted for readability):
<input id="MainContent_AgreeTOUCheckBox" type="checkbox"
name="ctl00$MainContent$AgreeTOUCheckBox"
onclick="javascript:setTimeout('__doPostBack(\'ctl00$MainContent$AgreeTOUCheckBox\',\'\')', 0)"
/>
<label for="MainContent_AgreeTOUCheckBox"> I have read and agree to the</label>
Any ideas on what I might do to get this to work consistently?
Your checkbox is working fine, it's just hidden behind another panel. If you click and drag below the checkbox to highlight the page itself, you can see your left footprint panel is hiding most of the checkbox.
Try clicking just the very right edge of the checkbox and it will work.
It's probably as easy as adjusting your style like so:
<div style="padding-left:10px">
<asp:CheckBox ID="AgreeTOUCheckBox"
AutoPostBack="true"
runat="server" Text=" I have read and agree to the"
OnCheckedChanged="AgreeTOUCheckBox_OnCheckChanged" />
</div>
The same is true of your Authorize button. Try clicking the very left of that and it won't work.
Let me know if that does it for you.
This problem has nothing to do with ASP.NET. I've checked your website and you have a DIV (id: boots) that is sitting on top of the majority of the checkbox (it displays the boots image on the left as it's background image).
If you set the width of that div to be 180px rather than 200px, it should solve your problem,
I tested your site. I can't check either.
According to what I tested, your checkbox is just to enable the button. If so, you can just simply use jQuery to do that.
Here is the sample:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#<%= AgreeTOUCheckBox.ClientID %>').click(function () {
if ($('#<%= AgreeTOUCheckBox.ClientID %>').attr('checked')) {
$('#<%= AuthorizeButton.ClientID %>').removeAttr("disabled");
} else {
$('#<%= AuthorizeButton.ClientID %>').prop("disabled", "disabled");
}
});
});
</script>
<asp:CheckBox ID="AgreeTOUCheckBox" runat="server"
Text=" I have read and agree to the" />
<asp:Button runat="server" ID="AuthorizeButton"
Text="Authorize" Enabled="False"/>

Create pop up panel or similar

I have a request.. where I need to create a link, then after the link is clicked.. a pop up should come up with a gridview in there but whatever is selected from that grid view I should pass it to a label on the page where the pop up was originated.. How can I do this?
I do not want to create a separate page.. just want to be able to add a gridview maybe in a panel... and then make the panel pop upon clicking on a linkbutton.
So so far, I have the panel and the gridview in the panel, how do i make it pop up?
PS : I also have Telerik just have not used it much (is there anything from there I can use)
Thank you
Have a look at the ajax control toolkit.
This example demos the popup functionality that you want.
I'd put the gridview in a DIV and hide the div. You can then use JQuery to handle showing the DIV, capturing your value selected and setting your label. This can all be done client side, avoiding any trips to the server as it seems that shouldn't be necessary for what you've described. Here's a very simple sample. I'm simply showing a textbox and the value entered is updated into the label. You'll of course want to add some styling and html here to make your div look more like a form. But it should get you started. You'll also need to include the jquery references.
<script type="text/javascript">
$(document).ready(function () {
$('.ok').click(function (e) {
//Cancel the link behavior
e.preventDefault();
$('#lbl').text($('#input').val());
$('#dialog').hide();
});
$('#btnShow').click(function (e) {
//Cancel the link behavior
e.preventDefault();
//Set the popup window to center
$('#dialog').css('top', $(window).height() / 2 - $('#dialog').height() / 2);
$('#dialog').css('left', $(window).width() / 2 - $('#dialog').width() / 2);
$('#dialog').show();
});
});
</script>
<label id="lbl">Old Value</label>
<input type="button" id="btnShow" value="Get Value" />
<div id="dialog" style="display:none; width:440px; height:200px; position:absolute; ">
<input type="text" id="input" />
<input type="button" value="OK" class="ok" style="width:70px"/>
</div>

Form GET in ASP.NET

I'm trying to convert a classic ASP page to ASP.NET 3.5.
On the page, there is a small form to submit your e-mail address to an external newsletter site. Here's the code:
<form name="emailForm" action="http://www.site.com/emailsignup.aspx" method="get">
<input type="text" name="email" />
<input type="submit" id="btnSubmit" name="btnSubmit" />
</form>
I was hoping I'd just be able to drop this on the page and it would work, but it doesn't, it just reloads the page.
How am I supposed to do this? Does it need to be done in the code behind of the button's click event?
In ASP.Net, by default controls - like the button - that cause postbacks will submit the page back to itself EVEN if you set the action attribute on the page to another file. What you want is called Cross-Page Posting. The following MSDN pages shows you how to do this with ASP.Net 4, but there is a link at the top to get to older versions:
http://msdn.microsoft.com/en-us/library/ms178140.aspx
http://msdn.microsoft.com/en-us/library/ms178139.aspx
Otherwise you can just use the Button's Click Event Handler in the code behind.
Hope this helps.
you must be missing runat="server" in the form tag
try to create a page through the IDE and paste the code for input tags between the form tags
<input type="text" name="email" />
<input type="submit" id="btnSubmit" name="btnSubmit" />
It surely is ending nested in the form aspx get by default.
Depending on the layout of your page, you can modify it so you don't end with a nested form. If that's not possible, I think you can't get around to use a form, so instead you'll have to look at a different solution like building the get with js.
The easiest way would be to put that <form> tag outside the main <form runat="server"> tag that usually wraps all ASP.NET controls.
If you're using a master page and the only content placeholder you can use is within that <form runat="server" tag, or you need this form tag in the page structure within the main <form runat="server"> tag then you need to:
Take out the simple <form> tag but leave the HTML <input> tags. Handle the client onclick event (the JavaScript versions, not ASP.NET postback handlers) of the submit button. That handler should grab the e-mail from the text box and issue something like window.location = 'http://www.site.com/emailsignup.aspx?email=....' in Javascript. Make sure to cancel the default HTML button action handler so it doesn't bubble up and submit the ASP.NET form too.

Categories