You're not using standard ASP.NET Web Forms programming constructs. If you take a moment to look at the resulting HTML you'll see the code is located at the top of the page. The best I can recommend is going through a few Getting Started tutorials to learn
the basics.
According to your description, it seems you are trying to create html markup and an asp button with an event handler in c#.
The reason why the button won't work is because this <asp:Button /> is a weboform aspx control, this control will be compiled to html <input type="submit" />, so if you directly write this <asp:Button /> in html with no compile progress, it won't work as
you wished.
If i didn't misunderstanding anything, then you can achieve this requirement with a Literal control and a Placeholder control.
As @mgebhard said, you will need a data bound control to bind all these dynamic data from your database.
The strange thing is that why would you want to create all these html markup and asp controls in the back code, this will make your logic quite complex.
Even though, i will provide a demo which create all the controls in the back, please refer to below code:
SQL:
create table Item
(
id int identity(1,1),
title varchar(50),
[image] varchar(200)
)
insert into Item values('T1','Images\\FCF.jpg')
insert into Item values('T2','Images\\FCF2.jpg')
insert into Item values('T3','Images\\CC.jpg')
insert into Item values('T4','Images\\FCF2.jpg')
insert into Item values('T5','Images\\CC.jpg')
insert into Item values('T6','Images\\FCF.jpg')
select * from Item
Member
1 Points
6 Posts
Onclick Event Doesn´t work when writed from Response.Write
Oct 07, 2019 12:38 PM|Days.galante|LINK
Hi,
I am loading content from Database and Writing the code with Response.Write.
I have a button that has an Onclick event and when clicked it says :
what should i do ?
With the best regards,
Days.galante.
All-Star
53021 Points
23599 Posts
Re: Onclick Event Doesn´t work when writed from Response.Write
Oct 07, 2019 02:02 PM|mgebhard|LINK
You're not using standard ASP.NET Web Forms programming constructs. If you take a moment to look at the resulting HTML you'll see the code is located at the top of the page. The best I can recommend is going through a few Getting Started tutorials to learn the basics.
https://docs.microsoft.com/en-us/aspnet/web-forms/
Contributor
3140 Points
983 Posts
Re: Onclick Event Doesn´t work when writed from Response.Write
Oct 08, 2019 05:45 AM|Yang Shen|LINK
Hi Days.galante,
According to your description, it seems you are trying to create html markup and an asp button with an event handler in c#.
The reason why the button won't work is because this <asp:Button /> is a weboform aspx control, this control will be compiled to html <input type="submit" />, so if you directly write this <asp:Button /> in html with no compile progress, it won't work as you wished.
If i didn't misunderstanding anything, then you can achieve this requirement with a Literal control and a Placeholder control.
You can also refer to below demo:
ASPX:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Literal ID="Literal1" runat="server"></asp:Literal> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> <asp:Literal ID="Literal2" runat="server"></asp:Literal> </div> </form> </body> </html>
CS:
Here's the result of this demo:
Hope this could help.
Best Rgeard,
Yang Shen
Member
1 Points
6 Posts
Re: Onclick Event Doesn´t work when writed from Response.Write
Oct 08, 2019 03:53 PM|Days.galante|LINK
Hi Yang,
I really appreciate your help!
This method kinda worked.
Iam using a cicle do write content ,
and it was supposed to appear one button on each Row, but it appears like this!
what should i do ?
All-Star
53021 Points
23599 Posts
Re: Onclick Event Doesn´t work when writed from Response.Write
Oct 08, 2019 05:13 PM|mgebhard|LINK
You should use one of the data bound controls like a list view or a form view.
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listview?view=netframework-4.8
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.formview?view=netframework-4.8
Contributor
3140 Points
983 Posts
Re: Onclick Event Doesn´t work when writed from Response.Write
Oct 09, 2019 03:27 AM|Yang Shen|LINK
Hi Days.galante,
As @mgebhard said, you will need a data bound control to bind all these dynamic data from your database.
The strange thing is that why would you want to create all these html markup and asp controls in the back code, this will make your logic quite complex.
Even though, i will provide a demo which create all the controls in the back, please refer to below code:
SQL:
ASPX:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" RepeatLayout="Table" OnItemDataBound="DataList1_ItemDataBound"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>' Visible="false"></asp:Label> <asp:Literal ID="Literal1" runat="server"></asp:Literal> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> <asp:Literal ID="Literal2" runat="server"></asp:Literal> </ItemTemplate> </asp:DataList> </div> </form> </body> </html>
CS:
And the result of this demo:
Below i will also provide another demo with less code and much more simple logic, please take a look:
ASPX:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" RepeatLayout="Table"> <ItemTemplate> <div class='tick' style='background-color: #fff'> <h5><%#Eval("title")%></h5> <hr /> <div> <img src='<%#Eval("image")%>' height="120px" width="150px" /> <br /> <label for='qtd_tik'>Qtd.</label> <input type="number" min="0" max="10" name="qtd_tik" id='<%#"qtd_tik"+Eval("id")%>' /> <asp:Button ID="btn_Venda" runat="server" Text="ADD" OnClick="btn_Venda" Width="150px" Height="50px" /> <small id="id_item" style="visibility: hidden"><%#Eval("id")%></small> </div> </div> </ItemTemplate> </asp:DataList> </div> </form> </body> </html>
CS:
And the result of this one is just the same as the first demo:
Best Regard,
Yang Shen