I have been looking for a way to create a link in my code behind which will call a method in my code behind with its ID as parameter. It is basicly a getting content from the database and show it, then being able to delete a row if needed. This is my code:
protected void showDBContent(object sender, EventArgs e)
{
String queryString = "";
if (showDBDropDown.Text == "Show all")
{
queryString = "SELECT * from dbo.Search";
}
else
{
queryString = "SELECT * from dbo.Search where Tag = '" + (showDBDropDown.Text).TrimStart().TrimEnd() + "'";
}
List<String> tags = new List<String>();
List<String> urls = new List<String>();
using (SqlConnection conn = new SqlConnection(info.connectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = queryString;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
tags.Add(reader.GetString(0));
urls.Add(reader.GetString(1));
}
}
}
HtmlTable tempTable = showDBTable;
int i = 0;
foreach (String tag in tags)
{
String url = urls[i];
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell1 = new HtmlTableCell();
HtmlTableCell cell2 = new HtmlTableCell();
HtmlTableCell cell3 = new HtmlTableCell();
LinkButton link = new LinkButton();
link.Click += new EventHandler(DeleteRow);
link.Text = "Delete";
link.ID = "deleteRow" + i;
cell3.Controls.Add(link);
row.Cells.Add(cell3);
cell1.InnerText = tag;
row.Cells.Add(cell1);
cell2.InnerText = url;
row.Cells.Add(cell2);
tempTable.Rows.Add(row);
i++;
}
}
protected void DeleteRow(object sender, EventArgs e)
{
}
However it if never getting in the method DeleteRow. Any clues?
Your code is kind of messy - you need to clean it up a little.
First of all, instead of using a <table>
element you'd be better by using a GridView and bind data to it using DataSource property.
After that, you can add a TemplateField to your grid view columns. In that template you can declare the link button:
<asp:GridView runat="server" ID="gridView" OnRowDataBound="OnGridRowDataBound">
<Columns>
...
<TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="linkButton" Click+="OnDeleteButtonClick"/>
</ItemTemplate>
</TemplateField>
<Columns>
</asp:GridView>
Then, in the handler for OnRowDataBound
event of the GridView
set the CommandArgument
of the LinkButton
to the value of row identifier:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
var button = e.NamingContainer.FindControl("linkButton") as LinkButton;
button.CommandArgument = e.Row.RowIndex;
}
Now, when user clicks on your LinkButton
all you have to do is to parse the CommandArgument
(and then of course, use it as your application logic dictates):
protected void OnDeleteButtonClick(object sender, EventArgs e)
{
var button = sender as LinkButton;
int rowIndex = int.Parse(button.CommandArgument);
// Delete the row with index rowIndex.
}
try this instead.
foreach (String tag in tags)
{
String urls[i];
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell1 = new HtmlTableCell();
HtmlTableCell cell2 = new HtmlTableCell();
HtmlTableCell cell3 = new HtmlTableCell();
LinkButton link = new LinkButton();
link.Text = "Delete";
link.ID = "deleteRow" + i;
cell3.Controls.Add(link);
((LinkButton)(cell3.Controls[0])).Click += Delete_Click;
row.Cells.Add(cell3);
cell1.InnerText = tag;
row.Cells.Add(cell1);
cell2.InnerText = url;
row.Cells.Add(cell2);
tempTable.Rows.Add(row);
i++;
}
protected void Delete_Click(object sender, EventArgs e)
{
//throw new NotImplementedException();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.