在代码后面创建链接按钮,使其在代码后面调用另一个方法

时间:2014-01-15 15:53:00

标签: c# asp.net .net

我一直在寻找一种方法来在我的代码中创建一个链接,后面将在我的代码中调用一个方法,其ID为参数。它基本上是从数据库中获取内容并显示它,然后在需要时能够删除一行。这是我的代码:

    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)
{

}

然而,如果永远不会进入DeleteRow方法。 有线索吗?

2 个答案:

答案 0 :(得分:0)

你的代码有点混乱 - 你需要清理它一点。

首先,使用GridView并使用DataSource属性将数据绑定到TemplateField,而不是使用<table>元素。

之后,您可以向网格视图列添加{{3}}。在该模板中,您可以声明链接按钮:

<asp:GridView runat="server" ID="gridView" OnRowDataBound="OnGridRowDataBound">
    <Columns>
        ...
        <TemplateField>
            <ItemTemplate>
                <asp:LinkButton runat="server" ID="linkButton" Click+="OnDeleteButtonClick"/>
            </ItemTemplate>
        </TemplateField>
    <Columns>
</asp:GridView>

然后,在OnRowDataBound的{​​{1}}事件的处理程序中,将GridView的{​​{1}}设置为行标识符的值:

CommandArgument

现在,当用户点击您的LinkButton时,您所要做的就是解析protected void OnRowDataBound(object sender, GridViewRowEventArgs e) { var button = e.NamingContainer.FindControl("linkButton") as LinkButton; button.CommandArgument = e.Row.RowIndex; } (当然,然后根据您的应用逻辑要求使用它):

LinkButton

答案 1 :(得分:0)

试试这个。

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();
    }