Button click event handler before page load - asp.net

I will describe my problem in simple way so it's not exactly what I'm trying to do but the idea is the same.Here is the problem:
I create dynamic buttons from code behind.I get some id from query string,create button with that id ,dynamic add event handler to click event,and add button to placeholder.I store the list of id-s in session and in page load method recreate these buttons and add to placeholder.One of the id-s is CurrentId and it's also stored in session.Buttons click handler do something like this
Button b=(Button)sender;
Session["CurrentId"]=Convert.ToInt32(b.ID);
In page load when I create buttons I want to set button text property different from others if id==Convert.ToInt32(Session["CurrentId"]) when list of id-s are gotten from session.But problem is that click event handler is called after page load,and when I create buttons in page load ,CurrentId in session hasn't been channged by click event handler.Can you suggest any solution to this situation?

It looks like you are attempting to update the buttons you have dynamically created after the click event has fired. Why not just change the button text within the click event as you have described?
i.e.
protected void button_Clicked(object sender, EventArgs e)
{
((Button)sender).Text = "Custom text for active button";
}
Also, you can always update the buttons on the PreRender event which occurs after the control click events but before the controls are served back down to the client.

Related

How to set current item in App Maker datasource?

This seems basic but I can't seem to figure out how to manually set the current item to work with from the datasource?
To illustrate: I have a table and I notice that when I select a row to edit a field, the item of that row becomes the current item, so if I have a link on that row to Navigate to a page, the row of the item selected will be the datasource.item for the navigated page.
However, I also notice that if I just hover over a row, without selecting to edit a field, if then I click on a link to Navigate to a page, it loads the data of whatever row was previously selected/edited. Therefore I'm wondering how to make it so that just on a mouse:over (or click on the shortcut without a prior click on another field in the row) the datasource.item will update to the the row the mouse has gone over instead of requiring to edit a field on the row first. I hope that makes sense.
Assistance is much appreciated. Thank you!
Why it happens:
AM code: generate button click event
User code: handle button's click event
User code: navigate user to different page
AM code: destroy DOM of current page
AM code: build DOM for new page
-- dead code after this line
AM code: row click event handler
AM code: change datasource's current item
Row's click event handler never gets control, because row is destroyed by user's code.
What Morfinismo's solution does?
AM code: generate button click event
User code: handle button's click event
AM code: row click event handler
AM code: change datasource's current item
-- moved lines
User code: navigate user to different page
AM code: destroy DOM of current page
AM code: build DOM for new page
Here are more tech details: Event Loop
In App Maker this problem can be solved with
setTimeout
Force current item update in user code
// button's onClick event handler
app.datasource.ListDatasource.selectKey(widget.datasource.item._key);
CustomProperties
// button's onClick event handler
app.pages.ShowMeNext.properties.Key = widget.datasource.item._key;
app.showPage(app.pages.ShowMeNext);
// next page's onAttach event handler
app.datasources.RecordDatasource.filters._key._equals = app.currentPage.properties.Key;
app.datasources.RecordDatasource.load();
URL parameters and history - this approach is used in most template apps, because it also implements deep linking on some extent.
// button's onClick event handler
var params = {
key: widget.datasource.item._key
};
var page = app.pages.ShowMeNext;
app.showPage(page);
google.script.history.replace(null, params, page.name);
// next page's onAttach event handler
google.script.url.getLocation(function(location) {
app.datasources.RecordDatasource.filters._key._equals = location.parameters.key;
});
Passing value between pages using global scope
// button's onClick event handler
window.key = widget.datasource.item._key;
// next page's onAttach event handler
app.datasources.RecordDatasource.filters._key._equals = window.key;
ListDatasource - list/grid/table datasource
RecordDatasource - datasource dedicated for a specific record (single-record datasource)
Use a timeout function. This happens because it takes some time for appmaker to change the item in the datasource. You can use something like this on the onClick event handler of the button or link that will take you to the other page:
setTimeout(function(){
app.showPage(app.pages.pageToNavigate);
},200);
That should take care of the issue. I hope this helps!

Fill a webform on a webpage using ASP.NET

There is a form on a webpage. It has 2 textboxes and a button. My ASP.NET application when run, should go to this webpage, fill the textboxes and press the button automatically. The webpage received after the button is pressed should be displayed.
Please help!
First you should have an event to receive the data on webform2, either by using session, viewstate, or querystring. Now use a condition to make sure the values are same with what you are expecting.
Private Sub Button1_Click()
//Process your data
End Sub
On Page_load of webform2, after receiving the values and processing them, you can click the button by calling the button click event, e.g.
Button1_Click(Me, e)
This will trigger the button click
Now, still based on the condition you should specify
Response.Redirect("PageName")
This should help

updating Textbox.text in serverside code asp.net

I have a textbox in my default.aspx page that I want to update it's text property in an event handler but the change does not take place after executing the code .
void _gsc_Task_Completed(object sender, TaskEventArgs e)
{
TextBox1.Text = "New Value";
}
How ever , if I do this on the click event of a simple button the textbox.Text value
changes.
what is the difference between a custom event handler like _gsc_Task_Completed and a simple button click event ?
Text box is a server side control. when you change the text this work(change text) is client side so the event not firing. but when i click on a button the page is going to server and back so the event is firing.
You need to consider the page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx
Either your event handler is being called to early (and clobbered by whatever is brought back from the client) or it is being called after the control was already rendered.
If I were you I'd put a breakpoint there as well as normal handlers for page onInit, onLoad, etc. See what order that handler is called (if at all) and what is below it on the callstack.

Clicking Linkbutton in a gridview doesn't fire the codebehind

I have searched for a solution on this site and others and I have not been able to find my exact case so here goes...
I have a panel that is used as a modal popup by using the modal popup extender. Inside of this panel is an update panel that contains a multiview control. The views for the multiview contain grids. Different grid for different views. The left most field on the grid is a template field that contains a link button. This should fire the code behind to redirect to another page. Instead, it appears to reopen the popup with no data displayed.
So...here are the events as I perform them.
I click a button on the webpage that opens my popup that contains my grid. The correct view displays containing the correct grid and my data is correct. I click on my linkbutton that should direct me to a new page and instead I get the same popup with no data in the grid.
I have this exact functionality in use on another webpage, the only difference is that I don't use multiviews. However, I at least know that the linkbutton in the grid inside the update panel fires the code behind.
I have tried an image buttons it reacts the same. The hyperlink field does work and can be used, but I want to see if I can get the linkbutton to work so that I have a little more flexibility.
You can add Click event on your LinkButton, you add this when you iterate on your Grid in RowCreated.
void GridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton button= (LinkButton)e.Row.FindControl("IdButton");
button.Click += YourDelegate; //Adjust here your called delegate
}
}
Nota : you must ensure that you don't rebind your Grid every time that you post datas
You can use
if( ! IsPostBack)
{
//Bind your grid
}
You presist with ViewState
lick on my linkbutton that should direct me to a new page and instead I get the same popup with no data in the grid.
By that I suppose you click a link inside your modal Popup multiview's current view to navigate to another view. You can skip this altogether & use ASP.NET AJAX toolkit's tab control instead. You can place each gridview inside a separate tab & remove your link buttons.

UserControl events not working for first time

I am calling my user control in a web page dynamically.
For the first time when I click the button on user control, the event is not firing. When I click the same for a second time, the events are firing..
Can anyone help me?
Sounds like the hookup to the button OnClick event is not occurring on every page load, which is required.
Can you guarantee that the hookup is being performed when you add the control and after a poastback to the page? I always put my event hooks in the Page_Init or Page_Load event handlers and outside of any Postback check. Try putting a breakpoint on the Handler hook up and see if the breakpoint gets "hit" twice.
An event hookup for a button would look similar to:
protected void Page_Load(object sender, EventArgs e)
{
btnSearch.Click += new EventHandler(btnSearch_Click); // breakpoint on this line
}
The client id of the control is derived from the ID of all containing controls that are marked with the INamingContainer interface. So, make sure that ALL controls in the hierarchy have a fixed ID.
For example if you have a Button control inside a Repeater, the ID of the button and the repeater are concatenated to make the client ID for the button. So both the repeater and the button should have the same ID across postbacks.
You can compare the HTML for the first request with the HTML of the second request to quickly determine if this is the problem.
Assign an ID for your dynamically created control -- otherwise no events will be fired.
Are you waiting for the download of full page before pressing the button?
The events are javascript functions hidden by ASP.Net in the page, that maybe not present at the time of your clicking.

Resources