Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 C# - Dynamically added button, Does not fire on first click

views
     
TSCounteReborn
post Dec 29 2016, 03:38 PM, updated 8y ago

Getting Started
**
Junior Member
205 posts

Joined: Nov 2012


Hello Gurus.

I am working on ASP C#. Behind my code I have dynamically created few buttons based on its need, the code will run while Page_Load. Based on the database input, it will create the needed buttons.

For example, the button work as a delete function.

---------------------------------------------------------------------------------------------------
Once I open the page, the first delete button works fine.
Then --POST BACK > Page Refresh--

When I click on 2nd button, it does not fire the delete function but it will still perform as below
Then --POST BACK > Page Refresh--

I reclick 2nd delete buton, it works fine now.
Then --POST BACK > Page Refresh--


For the next few buttons, it will happen exactly like how 2nd button did

[/U]Conclusion: I have to press twice to let the method fire[U]
---------------------------------------------------------------------------------------------------

Obviously this has something to do with the lifecycle events of my program but is there any alternative?

But I wonder why will this happens since everytime it reloads, it will call my function to create those button (in Page_Load). Like how it works for the first time!


alien3d
post Dec 29 2016, 09:40 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(CounteReborn @ Dec 29 2016, 03:38 PM)
Hello Gurus.

I am working on ASP C#. Behind my code I have dynamically created few buttons based on its need, the code will run while Page_Load. Based on the database input, it will create the needed buttons.

For example, the button work as a delete function.

---------------------------------------------------------------------------------------------------
Once I open the page, the first delete button works fine.
Then --POST BACK > Page Refresh--

When I click on 2nd button, it does not fire the delete function but it will still perform as below
Then --POST BACK > Page Refresh--

I reclick 2nd delete buton, it works fine now.
Then --POST BACK > Page Refresh--
For the next few buttons, it will happen exactly like how 2nd button did

[/U]Conclusion: I have to press twice to let the method fire[U]
---------------------------------------------------------------------------------------------------

Obviously this has something to do with the lifecycle events of my program but is there any alternative?

But I wonder why will this happens since everytime it reloads, it will call my function to create those button (in Page_Load). Like how it works for the first time!
*
Ajax + xshx

I assume you use webform.. I use webform also and not much like mvc era.. Razor kinda nice but still no like..
Please avoid using internal button and do manuall html button instead..

TSCounteReborn
post Dec 30 2016, 05:06 PM

Getting Started
**
Junior Member
205 posts

Joined: Nov 2012


QUOTE(alien3d @ Dec 29 2016, 09:40 PM)
Ajax + xshx

I assume you use webform.. I use webform also and not much like mvc era.. Razor kinda nice but still no like..
Please avoid using internal button and do manuall html button instead..
*
Never try Ajax before, and dont understand what do you meant by "xshx".
Will try later with Javascript myb?

Also, why do you suggest using HTML button instead of internal button? I have been working with Internal button since I work with C#.

Just to be clear, my understanding towards these buttons in aspx.
<input id="Button1" type="button" value="button" /> = HTML BUTTON
<asp:Button ID="Button2" runat="server" Text="Button" /> = INTERNAL BUTTON

Anyway, the button created behind my code was with
CODE
Button removeCartBtn = new Button();
.



alien3d
post Dec 30 2016, 05:20 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(CounteReborn @ Dec 30 2016, 05:06 PM)
Never try Ajax before, and dont understand what do you meant by "xshx".
Will try later with Javascript myb?

Also, why do you suggest using HTML button instead of internal button? I have been working with Internal button since I work with C#.

Just to be clear, my understanding towards these buttons in aspx.
<input id="Button1" type="button" value="button" />  =  HTML BUTTON
<asp:Button ID="Button2" runat="server" Text="Button" />  = INTERNAL BUTTON

Anyway, the button created behind my code was with
CODE
Button removeCartBtn = new Button();
.
*
Reason to long something id unside html output.
Just button runat server also can access same thing.
Is something like you update delete record same page without refresh page..ajax


TSCounteReborn
post Dec 30 2016, 05:24 PM

Getting Started
**
Junior Member
205 posts

Joined: Nov 2012


QUOTE(alien3d @ Dec 30 2016, 05:20 PM)
Reason to long something id unside html output.
Just button runat server also can access same thing.
Is something like you update delete record same page without refresh page..ajax
*
Guess how I resolve the issue I was facing?
I actually just redirect back to same page instead of clear then recreate all shits.

I doubt it will work well in production line when it keeps redirecting puke.gif shakehead.gif
alien3d
post Dec 30 2016, 06:09 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(CounteReborn @ Dec 30 2016, 05:24 PM)
Guess how I resolve the issue I was facing?
I actually just redirect back to same page instead of clear then recreate all shits.

I doubt it will work well in production line when it keeps redirecting  puke.gif  shakehead.gif
*
do keep in mind, refresh in page, sometimes client don't understand.. They love the redirection.. And the time to develop ajax may time consuming for you.Fixed to old style is still okay.. Unless the client said "Why facebook like that, you cannot mah " Then.. puke.gif
rthm93
post Jan 6 2017, 11:50 PM

Getting Started
**
Junior Member
109 posts

Joined: Jan 2011


I have been thinking about this lately.

1 of the way that i though of is using repeater.

Maybe the following idea may not solve ur problem, just an idea for u

ASPX:
CODE
<asp:Repeater ID="repeater" runat="server">
   <ItemTemplate>
       <asp:Button ID="btn" runat="server"/>
   </ItemTemplate>
</asp:Repeater>


ASPX.CS:
Page load
CODE
repeater.DataSource = new List<object>{ new object(), new object() };
repeater.DataBind();


This way, at page load it will show 2 buttons, the number of objects in the list will determine how many buttons you will have.

This post has been edited by rthm93: Jan 6 2017, 11:51 PM

 

Change to:
| Lo-Fi Version
0.0112sec    0.31    5 queries    GZIP Disabled
Time is now: 29th March 2024 - 06:35 PM