how to get javascript calender value on server side - asp.net

I am new on Asp.net. I am using Javascript calender It use Input tag.if I use runat="server" so that I can get value on cs page then it does not display calender. How can I get calender value from input tag to .cs page

Simplest way:
Have a hidden field on your control.
Set its in javascript by using :
$('#<%=hdDate.ClientID %>').val("date value here");
and then you can access this hidden field in cs file.

create a hidden field as in your aspx page which should be runat="server".
then
On button click call javascript function say jsOnclikfun() and write this function as
<script type="text/javascript">
function jsOnclikfun() {
var dateInputtext = document.getElementById("Id Of input textbox which you are trying to make runat=server");
var hdDateValue = document.getElementById('<%= hiddenField_Id.ClientID %>');
hdDateValue.value = dateInputtext.value;
return true;
}
</script>
Now on server side where ever you need data get it from that hidden field as hiddenfieldID.Value.
Hope this helps.

Related

ASP.net - Gridview Textbox Enter Button Postback

I have a GridView with a Textbox and when the user changes the text within that box, I want them to be able to hit the enter button and postback and update the changes they made within that textbox on the row in the GridView.
I can't figure out how to do this?
Thanks,
Mark
You need to use some JavaScript code to do that - its on the page part. Here is one that I use (jQuery)
$(document).ready(function(){
// capture the editors
var AllEditors = jQuery('#<%= gvGridViewID.ClientID %> :input[type=text]');
AllEditors.keydown(function (e) {
if (e.keyCode == 13)
{
e.preventDefault();
// the [value=Update] is the default value to Update control of GridView
jQuery(this).parents("tr").find("input[value=Update]").click();
}
});
});
If you have it inside UpdatePanel, you need to initialize it each time the UpatePanel fires. If you have it inside a custom control, you need to add extra variables on the function names to avoid conflicts.

is it possible to generate jquery datepicker from behind / C#?

I am working with asp.net and i have some question here, any help will be great.I am trying to generate controls from Codebehind(C#) since the amount of data which is returned from server is uncertain, now my question is, is it possible to generate jquery datepicker control from C#? how to do that? show me some examples.
thank you.
Yes it is possible . You can have a common class which you want to use for Date Picker while creating your control add the class on the textbox on the server side.
txt.Attributes.Add("class", "myCal");
On the client side call this javascript
$(doucment).ready(function(){
$(".myCal").datepicker();
});
This will loop through all the textboxes with myCal as class and will it make it into datePicker
you may check here to learn and see the sample codes of datepickers. I hope it works for you.
Try this..
Codebehind:
TextBox txt = new TextBox();
txt.ID = "datepicker";
div1.Controls.Add(txt);
If u want for more number of textboxes u can use cssclass instead of id.
TextBox txt = new TextBox();
txt.cssclass = "date";
div1.Controls.Add(txt);
Aspx:
<script type="text/javascript">
$(function () {
$("#datepicker").datepicker();//or $(".date").datepicker(); for more number of textboxes
});
</script>
Add required scripts and styles in aspx as well.To know more see this link.

Getting source control id in JavaScript function

I have taken three textboxes over aspx page. A JavaScript function is associated with these textboxes as:
TextBox1.Attributes.Add("onkeypress","MyFunction()");
TextBox2.Attributes.Add("onkeypress","MyFunction()");
TextBox3.Attributes.Add("onkeypress","MyFunction()");
The MyFunction() is defined within a JavaScript file.
What I want is to get the control id of the textbox on which the function is associated within the JavaScript function itself. That is, the JavaScript function should look like this:
function MyFunction() {
var myControl = ?;
alert("Key press event occured in "+myControl.ID);
}
I need the value of ?. What should I write there so that I could get the control id, i.e. TextBox1, TextBox2, or TextBox3 on which the event occurred?
I know that <% =TextBox1.ClientID %> will provide the id of TextBox1 on client. But I think as I am attaching the onkeypress event with the textbox I should get the source control id within JavaScript function itself.
Try:
TextBox1.Attributes.Add("onkeypress","MyFunction(this)");
JS will put the reference to the widget into this when the code for onkeypress is evaluated.

Passing Listbox Text to Javascript Pop-up Window URL

Is it possible to pass the contents of a textbox or Listbox into the URL portion of the javascript code window.open(URL)? I have an asp.net listbox control that displays URL values. When ever an end user clicks on another listbox, the URL listbox provides the specific URL. I am trying to pass this URL into the above javascript code, but I do not know the correct syntax to do this. This code will execute as an onclick event.
For clarification, similar to typing “+ ListBox.Text.ToString() +” or ‘” & List.Text & “’” to add the content of a listbox into something else, such as a textbox. Is there specific syntax to do the same, but add the listbox.text into javascript?
Thank you,
DFM
Simply add a client-side onclick handler to your listbox as shown below:
<asp:ListBox id="ListBox1" runat="server" .....
onclick="openPopup(this)">
........
</asp:ListBox>
Then add the following javascript code:
<script type="text/javascript">
function openPopup(e){
window.open(e.value);
}
</script>
Sure, this should be pretty easy with jQuery. Obviously the URL generation could be reduced to a single statement, but should give you the general idea.
$(document).ready(function() {
$("your-element").click(function() {
var str = $("#listbox-id").val();
var url = "your-url.com/" + str;
window.open(url);
});
});

AutoPostback with TextBox loses focus

A TextBox is set to AutoPostback as changing the value should cause a number of (display-only) fields to be recalculated and displayed.
That works fine.
However, when the field is tabbed out of, the focus briefly moves on to the next field, then disappears when the page is redrawn so there is no focus anywhere.
I want the focus to be on the new field, not the textbox I've just changed.
Is there a way to work out which field had the focus and force it to have it again when the page is redrawn?
This is "by design". If you are using ASP.NET 2.0+ you can try calling the Focus method of your TextBox once the postback occurs (preferably in the TextChanged event of the TextBox).
I am not sure if there is any built-in way to track focus but I found this article in CodeProject which should do the trick.
You could also consider refresh display-only fields using AJAX UpdatePanel. This way you won't lose focus from the new field.
Also I have proposed pure server-side solution based on WebControl.Controls.TabIndex analysis, you can use it, if you like.
This is what is happening:
1) TAB on a field - client event
2) Focus on next field - client event
3) Postback - server event
4) Page redrawn - client event new page overrides preious client events
The solution of your problem is to:
a) get the element that has gained focus BEFORE postback
<script>
var idSelected;
$("input").focusin(function () {
idSelected = this.id;
});
</script>
b) store the ClientID (actually in var idSelected) somewhere (i.e. an hidden Textbox, vith ViewState = true) BEFORE postback
** b) get ClientID ** (extract from hidden TextBox and put it in var idSelected) AFTER postback
d) get the element with ClientID and set focus AFTER postback
<script>
$(document).ready(function () {
if (idSelected != null) {
$("#" + idSelected).focus();
idSelected = null;
});
});
</script>
Note: this sample scripts use JQuery.
Remember to put Jquery.js in your solution and a reference in your page
<form id="form1" runat="server" enctype="multipart/form-data" method="post">
<asp:ScriptManager runat="server" >
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery.js" ScriptMode="Auto" />
....
Note2: this solution works without AJAX.
Look at this answer: to make Javascript work over Ajax you must use code like this:
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
function EndRequestHandler(sender, args)
{
MyScript();
}
</script>

Resources