【问题标题】:How to add class in parent li using child link button from code behind in asp.net c#?如何使用asp.net c#中代码后面的子链接按钮在父li中添加类?
【发布时间】:2016-04-25 09:15:25
【问题描述】:

这是页面的html代码:

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

这是我使用过的页面背后的代码:

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

        }

    }

当我单击任何链接按钮时,该链接按钮的父 li 应该更改或将类添加到 li(this)。我也有疑问,但没有解决方案。

【问题讨论】:

    标签: c# jquery css asp.net


    【解决方案1】:

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

    <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";
    }
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 2023-04-03
    • 2021-01-13
    • 2014-01-18
    • 2014-09-29
    相关资源
    最近更新 更多