我在我的搜索项目自动化工具之一中做了类似的事情。您可以尝试以下方法:
protected void grdKeywords_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton linkDeleteButton = e.Row.FindControl("lnkdel") as LinkButton;
Label lblGridKeyword = e.Row.FindControl("lblGridKeyword") as Label;
TextBox txtGridBox = e.Row.FindControl("txtGridKeyword") as TextBox;
if (lblGridKeyword != null)
{
if (lblGridKeyword.Text.Contains("'"))
{
lblGridKeyword.Text = lblGridKeyword.Text.Replace("'", "'");
}
}
if (txtGridBox != null)
{
if (txtGridBox.Text.Contains("'"))
{
txtGridBox.Text = txtGridBox.Text.Replace("'", "`");
}
}
if (txtGridBox == null)
linkDeleteButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure about deleting keyword: " + lblGridKeyword.Text + " ?')");
else if (lblGridKeyword == null)
linkDeleteButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure about deleting keyword: " + txtGridBox.Text + " ?')");
}
}
lblGridKeyword是保存包含单引号的数据的标签。我在 RowDataBound 时使用 ' 替换了它。这对我有用。