How to add class in parent li using child link button from code behind in asp.net c#?

时间:2016-04-25 09:15:26

标签: c# jquery css asp.net

Here is the html code for the page :

<ul class="list quicker">
    <li><asp:LinkButton runat="server" ID="education_news" OnClick="education_news_Click" >Education</asp:LinkButton></li>
    <li><asp:LinkButton runat="server" ID="immigration_news" OnClick="immigration_news_Click">Immigration</asp:LinkButton></li>
    <li><asp:LinkButton runat="server" ID="visa_news" OnClick="visa_news_Click" >Visa</asp:LinkButton></li>
</ul>

Here is the code behind the page i've used:

 protected void education_news_Click(object sender, EventArgs e)
    {
        string edu = education_news.Text;
        using (SqlConnection _con = new SqlConnection(_Data.CS))
        {
            SqlCommand _Cmd = new SqlCommand("select * from tbl_brochures where type=@type", _con);
            _Cmd.Parameters.AddWithValue("@type", edu);
            _con.Open();
            dl_brochures.DataSource = _Cmd.ExecuteReader();
            dl_brochures.DataBind();

        }

    }

I have to do when i click on any link button the parent li of that link button should change or add the class to li(this). I have also query but got no solution.

1 个答案:

答案 0 :(得分:2)

您需要在aspx中添加runat="server" li

<ul class="list quicker">
    <li runat="server"><asp:LinkButton runat="server" ID="education_news" OnClick="education_news_Click" >Education</asp:LinkButton></li>
    <li runat="server"><asp:LinkButton runat="server" ID="immigration_news" OnClick="immigration_news_Click">Immigration</asp:LinkButton></li>
    <li runat="server"><asp:LinkButton runat="server" ID="visa_news" OnClick="visa_news_Click" >Visa</asp:LinkButton></li>
</ul>

并在代码隐藏中添加这样的类:

protected void education_news_Click(object sender, EventArgs e)
{
    ((HtmlGenericControl) education_news.Parent).Attributes["class"] = "some-class";
}