OnClick event in Textbox - c#

I have a textbox in the webpage.
//.aspx
<td class="style2">Start of Date:<asp:TextBox ID="TextBox1" runat="server"
Height="22px"
ontextchanged="TextBox1_TextChanged" Width="157px"></asp:TextBox>
</td>
//aspx.cs
protected void cntCalendar_SelectionChanged(object sender, EventArgs e)
{
TextBox1.Text = cntCalendar.TodaysDate.ToShortDateString();
}
Now I'm able to take the value in the textbox from calendar,but my problem is I'm not able to get the calendar to show onclick in the textbox and hide after date selected.
Can anybody help me with this please.

Use the CalendarExtender class from the ASP.NET AJAX ToolKit.
Download and usage
You shouldn't do the post backs for the selecting date.

Sounds like you want to use something like the AJAX calendar extender. This control will negate the need of the asp:Calendar control.
See below for an example.
<asp:TextBox ID="txtStartDate" runat="server" CausesValidation="true" >
</asp:TextBox>
<ajax:CalendarExtender ID="ce1" runat="server" Format="dd-MM-yyyy" TargetControlID="txtStartDate" PopupPosition="Right" >
</ajax:CalendarExtender>
The calendar will popup on click and when a date is selected, the textbox will be populated with the date selected in the format specified in the Format attribute

VMAtm is right on with the CalendarExtender.
However, if you have your own custom calendar you're using and you just need to manually pop that up via client onclick of the textbox, do this in Page_Load or PreRender:
TextBox1.Attributes["onclick"] = "showMyCalendar();";
or
TextBox1.Attributes[HtmlTextWriterAttribute.Onclick] = "showMyCalendar();";

Related

How do I disable dates in asp .net textbox textmode = date?

I'm having this issue where I want to set disable on the dates in the textbox with the textmode = date. I would prefer it to be done in c#.
Any advice?
Here is the aspx page:
<asp:TextBox ID="tbStartDate" runat="server"
AutoPostBack="true" OnTextChanged="tbStartDate_TextChanged"
TextMode="Date"></asp:TextBox>
And the C# file
protected void disablePastDateintextbox()
{
????????
}

Bind a textbox value in a label box using c# inside a uframe

I have entered a value in text box and click the button the value should bind in a label.
The code must be in c# not in query. There is no database also.how please say the code behind in c#. It is button click event. while click the button the page should post back and bind the data how to bind a textbox value in a label box using c#. The value must bind in postback method.
easy , take a look at the below code:
<asp:TextBox Id="txt1" runat="server" />
<asp:button Id="btn1" runat="server" onClick="Button1_Click"/>
<asp:Label Id="lbl1" runat="server"/>
//in the code behind implement the Button1_Click
protected void Button1_Click (object sender, EventArgs e)
{
lbl1.Text= txt1.Text;
}
It as easy as the following:
LabelID.Text = TextBoxID.Text;
Where LabelID is the ID of your Label control and TextBoxID is the ID of your TextBox control.
You should place this code inside the button click's event.
Update
Please use for your purpose ASP.NET Web Server Controls.
<asp:TextBox ID="textBoxId" runat="server"/>
<asp:Label ID="labelId" runat="server"/>
Then you can access the values of Text property of both the above controls, like above:
labelId.Text = textBoxId.Text;

How to implement a click event on textbox in ASP.NET?

In my web application I need a functionality so that when users click on textbox to input values, it should make the button and the other fields visible?
I am using the code provided below but, could not get it working.
C#:
protected void TextBox1_Click(object sender, EventArgs e)
{
ButtonSearch.Visible = true;
}
ASP.NET:
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" OnClick="TextBox1_Click"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" Visible="False" />
How to accomplish this?
Set AutoPostback="True". This way the event will be fired server-side, and the button will become visible.
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" OnClick="TextBox1_Click" AutoPostBack="true"></asp:TextBox>
However, if you only want to toogle visility of a button, you really should considerate javascript. This will save a trip back to the server.
<asp:TextBox onclick="txtBox1_ClientClicked()" ID="TextBox1" runat="server" OnClick="TextBox1_Click"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" OnClick="ButtonSearch_Click" Text="Search" style="display:none;" />
<script type="text/javascript">
function txtBox1_ClientClicked(){
var theButton = document.getElementById('<%=ButtonSearch.ClientID%>');
theButton.style.display = 'block';
}
</script>
You do not need to post back to the server to accomplish your job. You can use client side onFocus event and javascript/jquery, for example.
I know I used an input of type text, and you are using an ASP Control which posts on server, but here is a JSFiddle to get you on the right track: http://jsfiddle.net/Mmjtz/1/
$("<%= ButtonSearch.ClientID %>").click(function(){
$("#TextBox1").show():
});
In this code you need to pass fields ID which you want to visible on the click of button.
Put the textbox inside a div and use the div's onClick event from codebehind. It's not what you asked but it works for me without any errors. Here is a javascript function to implement requested event:
function toggleVisibility()
{
document.getElementById('TextBox1').disabled = true;
/*
...some other code...
*/
}
And of course, you have to define your onclick event at the div definition after implementing this JS function.
<div id="TBdiv" onClick="toggleVisibility()">
<asp:TextBox ID="TextBox1"..../>
</div>
IMPORTANT: Since you now disabled your TextBox from codebehind, you have to enable it in somewhere before you want to use it again. Otherwise you will not see it while the page is running.
jQuery is the perfect solution for your problem. The code would be something like this:
$("#TextBox1").on("click",function(){$("#ButtonSearch").css("visibility", "visible");})
You include the script by adding <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> to the page and then you can add the piece of code above to within <script></script> tags.

Unable to write to asp.net textbox control with the html5 "date" type

I have an asp.net page with a an html5 TextBox control set as the "date" type. The control on my page looks like this:
<asp:TextBox ID="TextBoxMyDate" runat="server" type="date"/>
It works great in the page and I can click on it and set the date. I can also read the date through TextBoxMyDate.Text. However, I cannot figure out how to programaticaly set the date. I have tried various formats of:
TextBoxMyDate.Text = DateTime.UtcNow.ToString("MM/dd/yyyy")
TextBoxMyDate.Text = DateTime.UtcNow.ToString("MM/dd/yyyy")
My guess is that its much more complicated than simply setting the text value but I don't know where to go from here. Any suggestions?
This is actually the correct way to set the date but the format was wrong!
TextBoxMyDate.Text = DateTime.UtcNow.ToString("yyyy-MM-dd");
I was confusing how the browser displays the date and the html5 standards for this control!
Yes, it is quite possible. Here is an example of a date selection filter with ajaxcontrotoolkit and the textbox field.
Overview:calendar ajaxControlToolkit Overview
Tutorial:Tutorial ajaxControlTollkit Date textbox
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
<asp:CalendarExtender
ID="CalendarExtender1"
TargetControlID="txtStartDate"
runat="server" />

How to bind calender selected value to a txt bx

I am trying to create an ajax popup calender. So When the user clicks a image button the calender should pop up and the selected date from the calender should be bind to a text box.
This is my aspx code:
<asp:TextBox ID="txtBxDate" runat="server" MaxLength="10" Width="75px">
</asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/images/toolbox.gif" />
<asp:PopupControlExtender ID="ImageButton1_PopupControlExtender" runat="server"
DynamicServicePath="" Enabled="True" ExtenderControlID=""
PopupControlID="Panel1" Position="Bottom" TargetControlID="ImageButton1">
</asp:PopupControlExtender>
<asp:Panel ID="Panel1" runat="server" Width="200px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
My Code Behind:
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
txtBxDate.Text = Calendar1.SelectedDate.ToString();
}
But when I selected a date from the calender, the selected date wasn't binded to the text box. What I am doing wrong here? or how can I fix it?
The reason it's not updating is because txtBxDate is outside of the UpdatePanel. Either put txtBxDate in the update panel with the calendar, or put it in a separate update panel.
EDIT
After inspecting your code a little more closely, it looks like you're trying to manually create the functionality that's already built into the AJAX Toolkit Calendar control.
See here: Calendar Sample
It seems that you have your targetcontrolid and popupcontrolid mixed up. I think it should be:
PopupControlID="ImageButton1" TargetControlID="txtBxDate"
You can get rid of the code-behind setting the selected date on the text field. The calendar extender should take care of that automatically.

Categories