I'm working on a web page,
I need that when it loads first time it would get data from api's.
Then I want to go from page to page with the same data.
I used IsPostBack
Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//First Time Load Data
leadsList = new List<Lead>();
var client = TVPLeads.Session.GetWebClient(Session);
string PostId = PostId = Request.QueryString["postId"];
PostId = "1";
try
{
leadsList = API.GetPostLeads(client, PostId);
}
catch (Exception) { }
page = (page == 0) ? 0 : page - 1;
DisplayLeadsPage(leadsList, page, "");
}
}
private void pageChoosen(object sender, EventArgs e)
{
int page = int.Parse(((Button)sender).CommandArgument);
DisplayLeadsPage(leadsList, page-1, "");
}
DisplayPagination(){
.
.
Button prev = new Button{
ID = "page_"+i,
CssClass = "Pagination",
Text = "i",
CommandArgument = ""+i,
OnClientClick = "pageChoosen"
};
prev.Click += pageChoosen;
divPagination.Controls.Add(prev);
.
.
.
}
I clicking on a button , got to Page_Load function the postBack is true as expected , but the function is not firing(checked with debugger).
if I remove the IsPostBack and it would make all over again , then the button function is firing.
What's the problem with that? How to use it right ?
The first time you request your page, your page is not posted back. The rendering engine of asp.net creates the page and sends it back to the client.
When you click a button then this click we trigger a postback and a handler that is defined the Page class will execute some code. Then the page will be build and when on Page_Load comes into the scene, the code that is in the if(!Page.IsPostBack) will not be executed, because the IsPostBack property of the Page class is true.
There are two key concepts to conceive there. The first is about the Page lifecycle, the events that the page goes through in each request.
The second is that the Page is not posted back only the first time the client requests it (or the times that she does a full refresh of the page, clicking F5 for instance in Chrome).
Update
Respond to button click
If you haven't defined a server side button, you should define one. How?
In you markup, just add the following line of code:
<asp:Button id="buttonId" Text="Submit" runat="server" />
Then in the designer double click on the button. As you will notice, you will be navigated to the code behind class and a method would have been created, which would be the click handler. Then inside the body of this method, you could write the commands that should be executed, when the user clicks on your button. If you now go back to your markup, you will notice the another attribute would have been added to the Button with name OnClick.
Update
Dynamically built buttons need to be created again when the page is posting back. Otherwise they won't work.
Try adding an event handler to the control.
Your control will look something like this (on the aspx page):
<asp:button id="YourButton" runat="server" OnClick="YourButton_Click" />
Then your backend handler should also be public:
public void YourButton_Click(object sender, System.EventArgs e) {
Edit: You can also do this:
leadsList = new List<Lead>();
if (!Page.IsPostBack)
{
...
}
else
{
page = (page == 0) ? 0 : page - 1;
DisplayLeadsPage(leadsList, page, "");
}
Found an answer for the problem.
When the page is posted back to server.
I need to rebuild the controls Otherwise, those controls will be null.
Related
Do web controls ever appear like you are changing their values but actually retain the previous value?
I created a pop-up modal for users to edit an item. When the user clicks edit on an item on the main page, the following sequence happens:
The item's ID is passed to the Page_Load event of the modal page, and is used to populate the page control's with the item's data.
The user changes a value in a control. Ex: Changes text in a TextBox contol.
The user clicks save, triggering the Click event which creates a DataTransferObject with the values in the textboxes, which will be stored.
However, on step 3, the control's new value (TextBox.Text) still holds the value that it orginially had, not the value the user put in.
Add.aspx:
<%# MasterType VirtualPath="../MasterPages/Popup.Master" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="TextBoxDescription" runat="server"></asp:TextBox>
<telerik:RadButton ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>
</asp:Content>
Add.aspx.cs
//Cannot access the new values here
protected void btnSave_Click(object sender, EventArgs e)
{
//This will print the new text on Create, but the old text on Edit
System.Diagnostics.Debug.WriteLine(TextBoxDescription.Text);
}
//works properly
protected void Page_Load(object sender, EventArgs e)
{
objIDParam = Convert.ToInt64(Request.QueryString["ObjectID"]);
editMode = (objIDParam != 0) ? true : false;
if(editMode)
PopulateFields(objID);
}
//works properly
private void PopulateFields(long objID)
{
MyObject obj = GetObjectByID(objID);
TextBoxDescription.Text = obj.Description;
}
It is worth noting that this popup page is used for both creating items AND editing items. Create works fine (i.e. The item isn't saved with all blanks, but rather the user input). Editing an item will properly pull all that data back in, and let the user edit the fields, however I can't access the changed values in my code.
You need to check for IsPostBack in the Page_Load method.
The Page_Load gets called before the btnSave_Click method, so the TextBoxDescription.Text is getting reset to obj.Description before the btn_Save method runs.
Try returning out of Page_Load if you're posting back:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
objIDParam = Convert.ToInt64(Request.QueryString["ObjectID"]);
editMode = (objIDParam != 0) ? true : false;
if(editMode)
PopulateFields(objID);
}
Have a look at ASP.NET Page Life Cycle Overview for more info.
Previously when my RadGrid was not a batch edit grid I was able to use the grid's AddNewRecord button to redirect the user to another page with the following code:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "InitInsert")
{
Response.Redirect(redirectUrl + "?ProductID=" + this.ProductId);
}
}
After I made my grid a batch edit grid the Add New Button doesn't go into the ItemCommand event anymore and instead tries adding an inline insert record row to the grid. Is there anyway I can still use this button and override its functionality to still redirect the user?
So I've tested this and confirmed what I suspected in the comments. When EditMode="Batch", the "Add New Record" button, along with others, no longer cause a postback. You can override this by removing the JavaScript of the OnClientClick in the RadGrid1_ItemCreated like so:
Add this to your RadGrid1 attributes:
OnItemCreated="RadGrid1_ItemCreated"
Code behind (note: there is actually a Button AND a LinkButton):
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item.ItemType == Telerik.Web.UI.GridItemType.CommandItem) {
//This is the icon with the plus (+) sign unless you've changed the icon
Button iconButton = e.Item.FindControl("AddNewRecordButton");
if (iconButton != null) {
iconButton.OnClientClick = "";
}
//This is the words "Add New Record" or whatever you've called it
LinkButton wordButton = e.Item.FindControl("InitInsertButton");
if (wordButton != null) {
wordButton.OnClientClick = "";
}
}
}
This should allow the postback to happen and the code you posted should be able to run.
I have this code in my codebehind:
for (int i = 0; i < linkList.Count; i++)
{
var link = UppercaseFirst(linkList[i]);
var linkButton = new LinkButton
{
Text = link + " > ",
ID = Convert.ToString(i),
CommandArgument = urlList[i]
};
linkButton.Command += new CommandEventHandler(lnkWeb_Click);
bcHolder.Controls.Add(linkButton);
}
and here is the lnkWeb_Click method:
protected void lnkWeb_Click(object sender, CommandEventArgs e)
{
var url = e.CommandArgument.ToString();
//code...
}
This method is not getting triggered when I click on one of those generated linkbuttons.
Anyone have any idea what the problem is?
Tried OnCommand="lnkWeb_Click" in the aspx file and the method got trigged, but not those that I generate by code. They dont even have OnCommand="lnkWeb_Click" attribute.
The problem here is with the control life cycle. If you want to handle events of some control properly - you have to add this control to the page on every page loading process, that is on every postback.
Look what happens in your case:
Initial button is clicked
During the post back your dynamic link buttons are added to the page, event handlers are assigned to them
User clicks on the newly generated link button
During post back these dynamic link buttons are not added to the page again, ASP.NET does not know the origin of a event so it does not call the handler.
To fix this you might need to store in the View State information about link buttons that have to be added (please do not store the controls themselves, that would be a huge overhead). Also pay attention to their IDs - they have to be the same for the same controls.
Update. Some more hints on the View State solution.
Basically you need some indicator that during the page loading you need to create some dynamic link buttons. The very basic way to do it is to store the list of the link button identifiers (or texts, or both) and then during Page_Load check if there is anything stored in View State. For example:
// Property to access the view state data
protected List<string> Links
{
get { return ViewState['links']; }
set { ViewState['links'] = value; }
}
...
protected void Page_Load(object sender, EventArgs e)
{
...
if (this.Links != null && this.Links.Count > 0)
{
// inside this method you create your link buttons and add them to the page
// you actually have this code already
RenderLinkButtons();
}
}
...
// Not sure about what name you have here
protected void InitialButtonHandlerName(object sender, EventArgs e)
{
List<string> linkList = ...; //your variable, guessing a type
// this is exactly the method you use already to add links to the page
// just one more action added to it - store info about these links into View State to use it on later post backs
this.Links = linkList;
RenderLinkButtons();
}
Please use it just a point in right direction - you might have different implementation depending on your requirements and preferences. But I hope concept is clear now.
So I am creating a simple question/answer format and having an issue when submitting an answer. I dynamically create divs/buttons/textboxs from the database via a "RenderQuestions()" function. This creates a list of questions and answer textbox/buttons. When attempting to answer a question, I type my answer click submit and nothing happens. I do it again and it shows my first answer. It's a "step behind".. If I refresh it then shows all answers as it should. I've been struggling with this all night. Here's some code:
-----My page load----- (Relevant parts)
protected void Page_Load(object sender, EventArgs e)
{
//If authenticated hide login & show welcome bloc
if (User.Identity.IsAuthenticated)
{
//Show question & render
questionsBloc.Visible = true;
//if(Page.IsPostBack)
RenderQuestions();
}
-----RenderQuestions() function---- (The relevant parts)
//Initialize & get answers
List<Answer> answers = new List<Answer>();
answers = um.GetAnswers(q.QuestionID);
//Initialize html render
HtmlGenericControl questionDiv = new HtmlGenericControl("div");
TextBox txtAnswer = new TextBox();
Button btnAnswer = new Button();
//Set Answer Button
btnAnswer.Text = "Answer";
btnAnswer.Click += new EventHandler(btnAnswer_Click);
//Set ID's
btnAnswer.ID = "btnAnswer" + q.QuestionID.ToString();
questionDiv.ID = "questionDiv" + q.QuestionID.ToString();
//Set classes
questionDiv.Attributes.Add("class", "questionBloc");
btnAnswer.CausesValidation = false;
btnAnswer.EnableViewState = false;
//btnAnswer.UseSubmitBehavior = true;
//Fill inner text with question
questionDiv.InnerText = q.QuestionContent; //Insert question..
//actionDiv.InnerText = "Like/Dislike/Comment/Flag"; //Insert answer..
//Add answer textbox and button to action div
actionDiv.Controls.Add(btnAnswer);
//Add question div to qaDiv
qaDiv.Controls.Add(questionDiv);
//Add action div to qaDiv
qaDiv.Controls.Add(actionDiv);
//Add all controls to feedbloc
feedBloc.Controls.Add(qaDiv);
-----My btnAnswer event handler -----
private void btnAnswer_Click(object sender, EventArgs e)
{
UserManager um = new UserManager();
um.PostAnswer("My first answer!");
//RenderGlobalFeed();
}
That's every reference to my button.. Should I be initializing the btn click event in my page_init? Any help is much appreciated.
Thanks guys
Set AutoPostBack=true on btnAnswer. It's not triggering the server to act on the button click.
If you want to get event btnAnswer_Click triggered , then you must render the same Content and assign the eventHandler in every pageload(ie; the page load after the client button click must render the button again and EventHandler must be assigned).
Asp.net won't trigger the event if it doesn't find the controls in the pageload. Remember, after clicking a button, the page load event triggers first and then only the Click_event will be triggered.
The RenderQuestions() must be called in the btnAnswer_Click Event too. This will avoid the a step back problem.
In this scenario I would recommend you to learn about ajax (using jQuery library) requests in asp.net (using WebMethods or webservices) to avoid these postbacks.
I have created a Button programmatically.
I now want to redirect to a new aspx page on the click of this Button, such that we will enter into one of the methods of the new page (like page_load(), etc.)
Is this possible?
Eg:
Button oButton = new Button;
oButton.Text = "NextPage";
// Redirect to "Redirect.aspx" on click
But I am not able to find an entry point in Redirect.aspx where I can do some changes to the UI components once we get redirected to "Redirect.aspx"
Regards,
Parag
You need to handle Click event of oButton.
Button oButton = new Button();
oButton.Text = "NextPage";
oButton.Click += (sa, ea) =>
{
Response.Redirect("Redirect.aspx");
};
You can use query string parameters and depending on the value of param call the appropriate method in Page_Load of Redirect.aspx
e.g.
Redirect.aspx?val=1
in Redirect.aspx
protected void Page_Load(...){
string var = Request.QueryString["val"];
if(var == "1")
some_method();
else
some_other_method();
}
You could also use the PostBackUrl property of the Button - this will post to the page specified, and if you need to access items from the previous page you can use the PreviousPage property:
Button oButton = new Button;
oButton.Text = "NextPage";
oButton.PostBackUrl = "Redirect.aspx";
Then in you wanted to get the value from, say, a TextBox on the previous page with an id of txtMyInput, you could do this (very simple example to give you an idea):
void Page_Load(object sender, EventArgs e)
{
string myInputText = ((TextBox)PreviousPage.FindControl("txtMyInput")).Text;
// Do something with/based on the value.
}
Just another example of how to accomplish what I think you're asking.
See Button.PostBackUrl Property for more info.