I have a gridview that has columns for the days of the week.
I run this code on the fist load of the page so that the dates are correct.
if (!IsPostBack) {//my public function to set the dates, It works as expected.
setUpGrid ();
}
Then I have button that when clicked will show the following week 7 days in the future. That all works as well here is the code
protected void NWeeks_Click (object sender, EventArgs e)
{DateTime hd2 = Convert.ToDateTime (gvappts.Columns [2].HeaderText);
if (ViewState ["hd2"] == null)
{ViewState ["hd2"] = 0;}
ViewState ["hd2"] = ((int)ViewState ["hd2"]) + 7;
gvappts.Columns [2].HeaderText = hd2.AddDays ((int)ViewState["hd2"]).ToString ("ddd dd MMM",
CultureInfo.CreateSpecificCulture ("en-US"));
}
My problem is, you have to click the button twice before it will fire off the next week calculations. I thought the ViewState would take care of this but not sure where I am going wrong.
Thanks In advanced
I realized after looking at this for awhile that since I am dealing with a GridView in order to refresh the HeaderText you also have to DataBind the Gridview. Adding a simple GridviewName.DataBind (); to my function solved my problem.
Thanks for the help!
I imagine you could move your code to the Page_LoadComplete event to ensure all controls are loaded when setUpGrid() is called.
Check out ASP.Net Page Life Cycle
void Page_LoadComplete(object sender, EventArgs e)
{
if (!IsPostBack)
{
setUpGrid ();
}
}
Related
I have created a dropdownlist in my webpage which is outside the gridview and I have added automatic refresh. My problem is that I`m unable to retain the selected value in dropdownlist after the refresh. It goes to the default settings in the dropdown. Please help.
Thanks a lot for replying..
part of my code goes this way....
page_load(...)
{
Refresh
if(!IsPostBack)
{
//calling my function which includes databind..
myfunction();
}
}
i tried the same code as you people suggested but its not working..
even now after refresh, the default values appear in dropdownlist
You probably need to implement something like this in your page_load handler:
if (IsPostback) return;
//here populate the dropdown
Let me guess your Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
DataBindGridView(); // loads the datasource of the grid and calls gridView1.DataBind();
DataBindDropDown(); // loads the datasource of the dropdown and calls dropDown1.DataBind();
}
Do not reload all on every postback, just if !(IsPostBack):
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataBindGridView();
DataBindDropDown();
}
}
If you need to refresh your GridView don't use Page_Load but the appropriate event handler. If you use an ASP.NET Timer to reload your page periodically to refresh the grid, use it's Tick event.
protected void GridRefreshTimer_Tick(object sender, EventArgs e)
{
DataBindGridView();
}
I used to think my c# programming wasn't too bad, but today I am seriously questioning my head, something so small is defeating me...
I am trying to get a DropDownList to behave but we are not getting on today. I have a simple DropDownList in an ascx control which is dynamically loaded into an aspx page
<asp:DropDownList ID="ddl_SortBy" runat="server" AutoPostBack="true">
<asp:ListItem Value="0">Sort Alphabetically A to Z</asp:ListItem>
<asp:ListItem Value="1">Sort Alphabetically Z to A</asp:ListItem>
</asp:DropDownList>
and some code behind..
private short SortBy = 0;
protected void Page_Load(object sender, EventArgs e)
{
this.ddl_SortBy.SelectedIndex = -1;
this.ddl_SortBy.SelectedIndexChanged += new EventHandler(ddl_SortBy_SelectedIndexChanged);
if (!IsPostBack)
SearchDirectory();
}
public void ddl_SortBy_SelectedIndexChanged(object sender, EventArgs e)
{
SortBy = Convert.ToInt16(this.ddl_SortBy.SelectedItem.Value);
SearchDirectory();
}
I can never get the first item to trigger the selected index change event - as the SearchDirectory() function is not called. I can understand that it's possibly the case that when the control loads the first item IS selected so when selecting, the index isn't actually changing.
I have tried setting the selected item index to -1, and ClearSelection() on page load, but no luck.
Any ideas? Thanks
You are always resetting the SelectedIndex to -1 on every postback:
this.ddl_SortBy.SelectedIndex = -1;
So put that also in the postback-check:
if (!IsPostBack)
{
this.ddl_SortBy.SelectedIndex = -1;
SearchDirectory();
}
I find myself in the same need to execute a change event on Page_Load like you're expecting.. the thing is that I perceived that in ASP.NET before to put some ListItems it set .SelectecIndex to 0.
So if I put some thing like this.ddl_SortBy.SelectedIndex = -1, after that the debugger still shows 0... so it never turn on -1 and never executes the change event. It's like if ASP.NET always considers the first ListItem as the selected one when nothing (-1) is selected, so force it do -1 or 0 never calls change event at the first.
Searching a bit more: "The default value is 0, which selects the first item in the list."
From http://msdn.microsoft.com/en-us/library/vstudio/system.web.ui.webcontrols.dropdownlist.selectedindex(v=vs.100).aspx so I think we must force the call to event like:
ddl_SortBy_SelectedIndexChanged(null, null);
Move the event handler assignment to OnInit from OnLoad and delete the SelectedIndex line.
protected void Page_Init(object sender, EventArgs e)
{
this.ddl_SortBy.SelectedIndexChanged += new EventHandler(ddl_SortBy_SelectedIndexChanged);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
SearchDirectory();
}
My master page view state was switched off which is why it wasn't working. Though that is not what I want to allow.
My solution was to revert to real form submission data retrieval using Request.Form.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
SortBy = Convert.ToInt16(Request.Form[this.ddl_SortBy.UniqueID]);
// search directory
SearchDirectory();
}
Works absolutely fine now :)
Thanks everyone.
How about this simple workaround. Set the first item in the drop down to be:
<asp:ListItem Value="-1">(choose how to sort)</asp:ListItem>
That way, the selected index will change and the event will fire when the user selects the desired sort order.
Remove extra fiddling with the drop down from the code behind first.
I am working on an aspx page in VS 2005. I have code like this,
int RepID = 0;
protected void Page_Load(object sender, Eventargs e)
{
if(!Page.Ispostback)
{
get value from database and display it in textbox;
}
else
{
}
}
protected void Save_OnClick(object sender, Eventargs e)
{
Update Database with modified textbox Text ;
Response.Redirect(//To the same page);
}
After the Response.Redirect, i was looking for the page to refresh and get the modified value from database. Instead, it uses the else loop in Page_load and displays the old value because it doesn't go into the if loop as it is the Postback. How can i display from database after the response.redirect is used. I know i am missing a logic but i am not sure what? Thanks alott guys..
Delete
Response.Redirect(//to same page);
Your Save button click should post back to the same page it is on.
After adding a control in the dayrender event, is there a way to find the control later? I have tried
calendar.FindControl("lblSample")
but without success.
Here is some of my code to be more clear:
protected void calSample_DayRender(object sender, DayRenderEventArgs e)
{
Label lblSample = new Label();
lblSample.ID = "lblSample";
lblSample.Text = "Sample";
e.Cell.Controls.Add(lblSample);
}
After the day render event and the page loads completely, I have a link button event where I try and get the control back
protected void lbtnSave_Click(object sender, EventArgs e)
{
//Not working
Label lblSample = calSample.FindControl(lblSample);
//Also can't get to work, this was using Ross' suggestion and the recursive find function he wrote about. I'm probably just not using it correctly.
Label lblSample = ControlFinder.FindControl<Label>(calSample, "lblSample");
}
The issue was because the control was not added to the page until the dayrender method - meaning you could not get a reference to it on a post back. Using the Page.Request.Params collection the OP was able to grab the value out on the postback.
The problem is that the find control is not recursive and the control you want is probably inside another control.
This shows you how to make a recursive find control method that would help: http://stevesmithblog.com/blog/recursive-findcontrol/
Alternatively if you post the calendar controls code I can probably help you a bit more.
Ross
This answer is because of Ross' comment above showing me that I could use the Page.Request.Params to find the value I was after. It's not the cleanest solution but it works!
If you add a dropdownlist to a calendar control in the day render event
protected void calSample_DayRender(object sender, DayRenderEventArgs e)
{
DropDownList ddlSample = new DropDownList();
ddlSample.ID = "ddlSample";
ddlSample.DataSource = sampleDS;
ddlSample.DataBind();
e.Cell.Controls.Add(ddlSample);
}
You can get the selected value back like this, of course I need to put in more checks to verify that the dropdownlist exists, but you get the picture
protected void lbtnSave_Click(object sender, EventArgs e)
{
string sampleID = Page.Request.Params.GetValues("ddlSample")[0];
}
I'm with a problem and want to know if someone can help me.
I'm creating a table with some controls and I want to save all control values in every postback. As the controls are just defined after the page_load, I can't solve my problem this way:
object o;
protected void Page_Load(object sender, EventArgs e)
{
o = createObject();
Create_Table();
if (Page.IsPostBack)
Save_Data();
}
I thought I could execute Save_Data() at the begining of every postback event, but I think that should exist a better way to solve my problem.
Thanks.
Since you want it to be at the page level why not use ViewState? Since o appears to always be set with the same data there probably isn't need to set it more then once, though if you really want to you can remove the if not postback stuff...
protected object o
{
get {
return ViewState["o"];
}
set {
ViewState["o"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) { o = createObject(); }
Create_Table();
if (Page.IsPostBack)
Save_Data();
}
Your variable 'o' will not contain your original value once the postback is done. This is because each request creates a new page object on the server and your member variable values will be lost. It would be better to use the built in 'Session' property to save your data between requests.
See my answer here