shashank shashank - 7 months ago 138
Javascript Question

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

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.

Answer

You need to add runat="server" for li in your aspx:

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

and add class like this in code-behind:

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