I have a text box and a calendar in my ASP.NET web application.
When I select any date in calendar, I would like the date/month/year of that date to be displayed in the text box.
in .aspx file
<form id="form1" runat="server">
<div>
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged">
</asp:Calendar>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
</form>
in .aspx.cs file
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox1.Text = Calendar1.SelectedDate.ToString();
}
Always use google before you ask question : http://www.google.co.in/search?q=asp.net+%2B+calander+control+%2B+textbox&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
check the answer below
private void Calendar1_SelectionChanged(System.Object sender, System.EventArgs e)
{
TextBox1.Text = Calendar1.SelectedDate;
}
or
use OnClientDateSelectionChanged. Similar example explained here well
CalendarExtender Change date with Javascript
or
Calendar Demonstration
Assuming you already use the onselectionchanged event but not seeing result directly you could use a updatepanel like this
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:UpdatePanel>
If you were just looking for the event then it would look alike this
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox1.Text = Calendar.cal.SelectedDate.ToString();
}
You could also try looking at the Ajax Toolkit CalendarExtender. This gives you a text box that when you click in it opens a calendar and the selected date is automatically added to the text box.
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/calendar/calendar.aspx
Handle the "SelectionChanged" Event of the calender control and inside the event write this code,
txtbox.Text = Calendar1.SelectedDate;
txtbox.Invalidate();
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:UpdatePanel>
Related
I have tried to look through many questions about the dropdownlist and still yet to resolve my issue.
I have a Dropdownlist and a Submit button. When Submit button is clicked after value selected, it should update the Label text with the selected value of the Dropdownlist. However, what happened was each time value is selected from dropdown, it refreshes the page and the value captured was the first item in dropdownlist. How do I capture the selected value correctly before it refreshes?
Am at wits end, not sure where went wrong.
*All items in dropdownlist are unique. No duplication.
My codes:
ASP.NET
<form id="form1" runat="server">
<asp:DropDownList ID="ddlCode" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCode_SelectedIndexChanged"/>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"/>
<asp:UpdateProgress ID="updProgress" AssociatedUpdatePanelID="UpdatePanel1" runat="server">
<ProgressTemplate>
<img alt="progress" src="img/loading.gif"/><br /><h2>Loading...</h2>
</ProgressTemplate>
</asp:UpdateProgress>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Button ID="submitBtn" runat="server" cssclass="btn btn-success" OnClick="submitBtn_Click" Text="Submit"/>
<asp:Label ID="lblCode" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCode" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
</form>
Code behind
string ddlSelectedIndex = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//This is to load code into dropdownlist which works fine.
LoadCode();
}
}
protected void ddlCode_SelectedIndexChanged(object sender, EventArgs e)
{
ddlSelectedIndex =(ddlCode.Items[ddlCode.SelectedIndex].Text).Substring(0, 4);
}
protected void submitBtn_Click(object sender, EventArgs e)
{
lblCode.Text = ddlSelectedIndex;
}
Try it without the "AutoPostBack=True" on the DropDownList... the way your code is written, it causes the page to refresh any time the DropDownList value changes. What you want is a postback when submit is clicked.
Is there a way to update a textbox without using postback? I'd like to populate the txtDFS field with the value of (100 - txtStocked) after the user enters data in the txtStocked field.
The following code works, but it uses postback so the page refreshes. Is there a way to accomplish this without refreshing the page?
<asp:TextBox ID="txtStocked" runat="server" Width="75px" AutoPostBack="true" OnTextChanged="txtStocked_TextChanged"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px"></asp:TextBox>
protected void txtStocked_TextChanged(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
txtDFS.Text = txtStocked.Text;
}
Solved this by using an UpdatePanel. Here is the updated code:
<asp:ScriptManager ID="SCManager" runat="server" />
<asp:UpdatePanel ID="SCPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:TextBox ID="txtStocked" runat="server" Width="75px" autopostback="true" OnTextChanged="Update_Text"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px" ReadOnly="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
protected void Update_Text(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
SCPanel.Update();
}
I'm trying to load/reload a GridView based on the OnSelectedIndexChanged event of a DropDownList. The ddl has AutoPostBack set to true, but still the Grid won't load, unless I encapsulate it within an UpdatePanel. But once I do that, my FileUpload control stops working... What would be the best solution for this problem?
**Edit ** Relevant code:
aspx file
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="upProva" runat="server">
<ContentTemplate>
<%--user control for data selection--%>
<asp:DropDownList ID="ddlAula" runat="server" DataTextField="nmAula" DataValueField="idAula"
CssClass="medio" Enabled="false" AutoPostBack="true" OnSelectedIndexChanged="ddlAula_OnSelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:GridView ID="gvQuestoes" runat="server" AutoGenerateColumns="False" CssClass="gv"
AllowSorting="false" DataKeyNames="idQuestao" OnRowCommand="gvQuestao_RowCommand">
<Columns>
<%--(...)--%>
</Columns>
</asp:GridView>
<asp:Button ID="btnSalvar" runat="server" ToolTip="Salvar" CssClass="botao40 salv40"
OnClick="btnSalvar_Click" ValidationGroup="trabalho" />
<asp:FileUpload ID="fuAnexo" runat="server" CssClass="fileOriginal" />
</asp:Content>
codebehind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnArquivo.OnClientClick = "document.getElementById('" + fuAnexo.ClientID + "').click(); return false;";
txtAnexo.Attributes.Add("onclick", "document.getElementById('" + fuAnexo.ClientID + "').click(); return false;");
fuAnexo.Attributes.Add("onchange", "document.getElementById('" + txtAnexo.ClientID + "').value = this.value;");
}
}
protected void ddlAula_OnSelectedIndexChanged(object sender, EventArgs e)
{
gvQuestoes.DataSource = Questao.CarregarPorAula(Int32.Parse(ddlAula.SelectedValue));
gvQuestoes.DataBind();
}
The DataSource/Databinding is correct (I know because I've added a button to the page, and used the same binding code on the Button _Click event and it works).
You need to rebind the gridview in OnSelectedIndexChanged event. Something like
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Do your processing logic
gridview1.DataSource = new_modified_datasource;
gridview1.DataBind();
}
I found a fairly simple (though not ideal in all cases) with PostBackTrigger:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="upProva" runat="server">
<ContentTemplate>
<%--user control for data selection--%>
<asp:DropDownList ID="ddlAula" runat="server" DataTextField="nmAula" DataValueField="idAula"
CssClass="medio" Enabled="false" AutoPostBack="true" OnSelectedIndexChanged="ddlAula_OnSelectedIndexChanged">
</asp:DropDownList>
<asp:GridView ID="gvQuestoes" runat="server" AutoGenerateColumns="False" CssClass="gv"
AllowSorting="false" DataKeyNames="idQuestao" OnRowCommand="gvQuestao_RowCommand">
<Columns>
<%--(...)--%>
</Columns>
</asp:GridView>
<asp:FileUpload ID="fuAnexo" runat="server" CssClass="fileOriginal" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSalvar" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
Nowhere in your code do you show how you populate ddlAula. So here is my answer: the Page_Load you've shown is abbreviated, and actually you are populating ddlAula there. Furthermore, you are doing it each time, rather than checking IsPostBack. Therefore by the time your eventhandler for ddlAula is hit, the list has been reset and the value you selected is no longer selected.
If my answer is correct, you need to add the check in Page_Load:
if (!IsPostBack)
{
//populate ddlAula
}
My code works fine when used in a normal webform. but when I use it in a webform using masterpages, It doesn't work.
page header : ~/Manager/BaseManager.master
and some nested master pages : Base.master > Pages.master > BaseManager.master
ASP
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger EventName="Click"
ControlID="btnUpdateEditPage" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnUpdateEditPage" CssClass="btnUpdateEditPage" runat="server"
Text="Button" OnClick="btnUpdateEditPage_Click" />
C#
protected void btnUpdateEditPage_Click(object sender, EventArgs e)
{
lblTest.Text += "**";
}
Do the following please:
1- Add UpdatePanel1.Update(); like the following:
protected void btnUpdateEditPage_Click(object sender, EventArgs e)
{
lblTest.Text += "**";
UpdatePanel1.Update();
//Your UpdatePanel should be UpdateMode="Conditional" as what you have now..
}
2- Put the button inside the update Panel
3- Remove the Trigger to not fire a post back, so your code has to be like:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnUpdateEditPage" CssClass="btnUpdateEditPage" runat="server"
Text="Button" OnClick="btnUpdateEditPage_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Place the button inside UpdatePanel.
SOLVED.
I change
<form id="form1" runat="server" action="post">
to
<form id="form1" runat="server">
and my problem solved.
But why?!
How to prevent the content of the UpdatePanel from the PostBack which is occurred in the whole page ?
I have the following code:
<head runat="server">
<title></title>
<script type="text/C#" runat="server">
// I don't want it to call this event handler
// untill I update the UpdatePanel manually by UpdatePanel1.Update();
protected void TextBox2_Load(object sender, EventArgs e)
{
((TextBox)sender).Text = DateTime.Now.ToString();
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToString();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox2" runat="server" OnLoad="TextBox2_Load">
</asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Unnamed1_Click" />
<asp:TextBox ID="TextBox1" runat="server" />
</div>
</form>
</body>
</html>
The previous code will write in the TextBox2 in spite of it exists inside an UpdatePanel with Conditional for UpdateMode.
I want it to update only when I call UpdatePanel1.Update();
Any help!
If you put Button1 and TextBox1 in the same UpdatePanel as TextBox2, this should give you your desired functionality.
If you want to keep them in separate UpdatePanels, you can, but you'll need to specify a trigger on UpdatePanel1 for the Unnamed1_Click event (or call UpdatePanel1.Update() in code).
Try using IsInAsyncPostBack:
protected void TextBox2_Load(object sender, EventArgs e)
{
if (ScriptManager1.IsInAsyncPostBack == true)
((TextBox)sender).Text = DateTime.Now.ToString();