Reputation: 1
I get DataTable from Access DB of suggestions and when i create foreach loop i create how it will look in the web here is the code:
public partial class suggestions : System.Web.UI.Page{
protected string str = "";
protected string str1 = "";
protected int id = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = myadohelper.ExecuteDataTable("YonatanDB.mdb", "select * from suggestionsTable");
foreach (DataRow row in dt.Rows)
{
int like = int.Parse(row["likeCount"].ToString());
int dislike = int.Parse(row["dislikeCount"].ToString());
int score = like - dislike;
str += "<div class='sug'>";
str += "<div class='txt'>";
str += "<h3>" + row["suggestionName"] + "</h3>";
str += "<p>" + row["suggestion"] + "</p>";
str += "</div>";
str += "<div class='like'>";
str += "<button type='button' id='btnLike" + row["ID"] + "' runat='server' onserverclick='Action_Click' ><img src='img/like.png' alt='Italian Trulli'/></button>";
str += "<button type='button' id='btnDisLike" + row["ID"] + "' runat='server' onserverclick='Action_Click' ><img src='img/dislike.png' alt='Italian Trulli'/></button>";
str += "<div class='likeNumber'>ציון: " + score + "</div>";
str += "</div>";
str += "</div>";
str += "<br />";
}
}
}
protected void Action_Click(object sender, EventArgs e)
{
HtmlButton b = sender as HtmlButton;
string s = "";
if (b != null)
{
s = b.ClientID;
}
str1 = "<p>" +s + "</p>";
}
}
and here is the HTML code:
<form id="my_form" method="post" runat="server">
<h1>Main Suggestion</h1>
<%=str1 %>
<br /><br />
<%=str %>
</form>
I'm trying to activate a function in Asp.net named "Action_click" from the buttons (the names of the buttons are "btnLike" and "btnDisLike" each id i added the id from the DB) i created,
for for some reason the function don't work and i dont know why?
p.s. for now the function only add a paragraph to the HTML page, but when i see it's warking i'll change it.
Upvotes: 0
Views: 40
Reputation: 1664
You should use something like GridView control then you can use ButtonField with Command properties to achieve what you need. You just have to set its data source and it will generate all the HTML code and events for you.
<asp:sqldatasource id="CustomersSource"
selectcommand="SELECT CustomerID, CompanyName, FirstName, LastName FROM
SalesLT.Customer"
connectionstring="<%$ ConnectionStrings:AWLTConnectionString %>"
runat="server"/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="False"
emptydatatext="No data available."
allowpaging="True"
runat="server" DataKeyNames="CustomerID">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID"
InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:ButtonField Text="Select" CommandName="Select" />
</Columns>
</asp:gridview>
Upvotes: 1