我在gridview的行上添加了超链接。我想要做的就是当我点击特定div内容显示的任何行时。 这低于我的.CS文件代码......`
protected void BindGridView(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=VAIO\\SA;Uid=sa;pwd=ans23@#;database=Lepz;");
conn.Open();
string p = "SELECT content FROM content";
SqlDataAdapter da = new SqlDataAdapter(p, conn);
DataSet ds = new DataSet();
da.Fill(ds, "SampleTable");
GridView2.DataSource = ds.Tables["SampleTable"].DefaultView; ;
GridView2.DataBind();
string q=Request.QueryString["content"];
foreach (GridViewRow row in GridView2.Rows)
{
if (row.RowIndex == GridView2.SelectedIndex)
{
HtmlGenericControl P_div = (HtmlGenericControl)row.FindControl("abcd");
P_div.Visible = true;
}
}
}
答案 0 :(得分:0)
在这种情况下,请使用“LinkButton”而不是“HyperLink”。并为每个LinkButton控件添加属性“CommandName”。之后在GridView RowCommand事件中添加以下代码:
if (e.CommandName == "divCom")
{
WebControl wc = e.CommandSource as WebControl;
GridViewRow row = wc.NamingContainer as GridViewRow;
HtmlGenericControl P_div = (HtmlGenericControl)row.FindControl("p_div");
P_div.Visible = true;
}
else
{
WebControl wc = e.CommandSource as WebControl;
GridViewRow row = wc.NamingContainer as GridViewRow;
HtmlGenericControl P_div = (HtmlGenericControl)row.FindControl("p_div");
P_div.Visible = false;
}
这里“divCom”是LinkButton控件的CommandName属性值,“p_div”是div控件的ID。