I can access the calendar control inside the Gridview Footer Template.
But it's not so easy to do the same thing in the EditItem Template
Can anybody suggest how to do this?
I am using 3 controls in the EditItem Template: Calendar, ImageButton, TextBox
as well as the Footer Template.
You can use the FindControl method in FooterRow of GridView to make the calendar visible True/False
protected void MyDateInsButton_Click(object sender, EventArgs e)
{
if (GridView1.FooterRow.FindControl("MyDateInsCalendar").Visible == false)
{
GridView1.FooterRow.FindControl("MyDateInsCalendar").Visible = true;
}
else
{
GridView1.FooterRow.FindControl("MyDateInsCalendar").Visible = false;
}
}
And get the selected date inside the Footer TextBox
protected void MyDateInsCalendar_SelectionChanged(object sender, EventArgs e)
{
Calendar MyCal = (Calendar)sender;
((TextBox)GridView1.FooterRow.FindControl("txtinsMyDate")).Text = MyCal.SelectedDate.ToString("d");
GridView1.FooterRow.FindControl("MyDateInsCalendar").Visible = false;
}
How do you access the calendar control in the EditItem template to make it visible True and False?
protected void MyUpdButton_Click(object sender, EventArgs e)
{
}
The GridView1_RowUpdating method will work for getting the selected date from the calendar control into the textbox control, but I still want to make the calendar appear and disappear when the user presses the image button.
Any help is appreciated.
Thanks.
You are almost there. Do like this:
protected void MyUpdButton_Click(object sender, EventArgs e)
{
var MyDateInsCalendar = GridView1.Rows[GridView1.EditIndex].FindControl("MyDateInsCalendar") as Calendar;
MyDateInsCalendar.Visible = false;
}
And in GridView1_RowEditing, set EditIndex, rebind data:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = MyDataSource; //Set it to your datasource
GridView1.DataBind();
}
Hope it helps!
Related
I know people have asked this over the Internet before but i have not used ant Javascript and have done it very basic.
Basically when they click an (ImageButton)icon next to a Textbox it will trigger an OnClick event in the C# which will make the Calender visable. Then when they selected anything on that calender it will trigger SelectionChanged on c# agian and then will set the date they clicked on in a Textbox.
HTML
<input id="txt_DateOfInterview" type="Date" class="aclass" runat="server" />
<asp:ImageButton runat="server" imageurl="~/Images/Calender.png" id="calendericonDOI" CssClass="calendericonDOI ClanderDOI" OnClick="calendericonDOI_Click" ></asp:ImageButton>
<asp:calendar runat="server" ID="ClanderDOI" CssClass="ClanderDOI" OnSelectionChanged="ClanderDOI_SelectionChanged" BorderColor="#6a3d98">
<TitleStyle BackColor="Orange"/>
</asp:calendar>
C#
protected void calendericonDOI_Click(object sender, ImageClickEventArgs e)
{
ClanderDOI.Visible = true;
}
protected void ClanderDOI_SelectionChanged(object sender, EventArgs e)
{
txt_DateOfInterview.Value = ClanderDOI.SelectedDate.Date.ToString("d");
ClanderDOI.Visible = false;
}
Question: How can I make the calender stay visable when they click to see next month only.
Your sample code works as you expect. When you change selected month it does not trigger 'SelectionChange' event.
There is separate event for month change (VisibleMonthChanged) where you can hide calendar, but to achieve what you want just make sure that you do not hide calendar somewhere else in your code:
protected void ClanderDOI_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
ClanderDOI.Visible = true;
}
Using this code
Calendar will not show at Page Load
Calendar will show when you will Click SelectDate(btnSelectDate) Button
Calendar Will disappear after date selection
Calendar will not disappear on next month selection
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Calendar1.Visible = false;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Calendar1.Visible = false;
}
protected void btnSelectDate_Click(object sender, EventArgs e)
{
Calendar1.Visible = true;
}
protected void Page_Load(object sender, EventArgs e)
{
bindbranches();
bindbranches1();
}
public void bindbranches()
{
DataTable dtbranch = new DataTable();
dtbranch = objsupplyBAL.getbrnch();
ddlbranch.DataSource = dtbranch;
ddlbranch.DataBind();
ddlbranch.Items.Add(new ListItem("--select--", "0"));
ddlbranch.SelectedIndex = ddlbranch.Items.Count - 1;
}
public void bindbranches1()
{
DataTable dt = new DataTable();
dt = objsupplyBAL.getbrnch();
ddlbranch1.DataSource = dt;
ddlbranch1.DataBind();
ddlbranch1.Items.Add(new ListItem("--select--", "0"));
ddlbranch1.SelectedIndex = ddlbranch1.Items.Count - 1;
}
My dropdownlist's are not binding without refreshing.If i select one dropdownlist another one is refreshing. What i have to add extra to my code. Can any one tell...
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bindbranches();
bindbranches1();
}
}
if you add this...it's work properly ...first try this...
The reason that is happening is you are running the code every time the page postsback, try the following to only populate the items once (on the initial page load) :
protected void Page_Load(object sender, EventArgs e) {
if (!this.IsPostBack) {
bindbranches();
bindbranches1();
}
}
Alternatively you can also handle the Page.Init event to run this code, this will change the dropdowns when the page is first loaded and will keep the values throughout subsequent postbacks :
protected void Page_Init(object sender, EventArgs e) {
bindbranches();
bindbranches1();
}
If you want the second dropdown to refresh only when the first item is selected, try the following solution :
protected void ddlbranch_SelectedIndexChanged(object sender, EventArgs e) {
bindbranches1();
}
And bind ddlbranch_SelectedIndexChanged to the selected index changed event of your ddlbranch control. This will only run the code when the page is initially loaded and when the user selects an item from the ddlbranch dropdown
You will have to set AutoPostBack Property of the drop down to true if you want to fill another drop down on change of one drop down so on change event will start executing
Loot # http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.autopostback.aspx
I use a gridview to display the search result. After clicking search button, the gridview will show page 1, but when I click page 2 link, the gridview disappeared and it was back when I click search button again and show page 2's content.
here is my code
<asp:GridView ID="searchresult" runat="server" AutoGenerateColumns="true" AllowPaging="true" OnRowDataBound="searchresult_RowDataBound" OnPageIndexChanging="searchresult_PageIndexChanging"
HeaderStyle-BackColor="#f9e4d0"
HeaderStyle-Height="20px"
Font-Size="11px"
AlternatingRowStyle-BackColor="#cfdfef"
Width="800px" style="text-align:left">
</asp:GridView>
and code behind
protected void search_Click(object sender, EventArgs e)
{
List<someclass> totalResult = new List<someclass>();
..... //some code to generate the datasource
searchresult.DataSource = totalResult;
searchresult.AllowPaging = true;
searchresult.DataBind();
}
protected void searchresult_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void searchresult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
searchresult.PageIndex = e.NewPageIndex;
DataBind();
}
I have no idea why the page 2 won't show up until I click search button again. when I clicked the page 2 link, the page did postback but the RowDataBound event was not fired
You have to give your grid a datasource. It appears you are only doing this on search_Click, so your grid is only going to have data then. Try something like:
protected void search_Click(object sender, EventArgs e)
{
PopGrid();
}
protected void searchresult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
searchresult.PageIndex = e.NewPageIndex;
PopGrid();
}
protected void PopGrid()
{
List<someclass> totalResult = new List<someclass>();
..... //some code to generate the datasource
searchresult.DataSource = totalResult;
searchresult.AllowPaging = true;
searchresult.DataBind();
}
searchresult_PageIndexChanging event handler will make that functionality work. However I recommend you use a gridview control inside a Panel with vertical scroll bar. My user love it and it is a lot faster moving up and down the gridview without defining any page index changing.
I hope it work for you.
I have a listbox on my website, which contains some elements.
I made the event, OnSelectedIndexChanged, so when the user presses an element, this value will be put into a textbox
protected void Page_Load(object sender, EventArgs e)
{
listbox = new Listbox();
// Add to page etc.
listbox.SelectedIndexChanged += new EventHandler(listbox_SelectedIndexChanged);
}
void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
textbox_name.Text = listbox.SelectedItem.ToString();
}
catch
{
textbox_info.Text = "Choose employee";
}
}
It works in c# windows forms, but not in web forms for some reason.
Is it possible to get it to work?
Thank you
Set autopostback property of the listbox true
I am using Visual Studio 2010, and I created a site (.aspx).
I have 2 radiobuttons, and a DropDownList.
I want to have an invisible dropdownlist and whenever I click on the one radiobutton then the downdownlist to appear!
I have added a code like this but nothing changes and I can't understand why!!
protected void RadioButton_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
DropDownList4.Visible = true;
else
DropDownList4.Visible = false;
}
protected void Page_Load(object sender, EventArgs e)
{
DropDownList4.Visible=false;
}
The only thing I am getting, is an invisible dropdownlist than never becomes visible!
Both of my radiobuttons have the same action "radiobutton_checkedchanged" ..
Thank you!
your code is ok , set AutoPostBack property of radiobutton to true
since RadioButton_CheckedChanged(object sender, EventArgs e) events occurs after page load
it will work no need for checking !IsPostBack
Modify your code as below:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList4.Visible=false;
}
}