簡體   English   中英

如何使用asp.net C#中后面的代碼使用子鏈接按鈕在父級li中添加類?

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

這是頁面的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),我必須這樣做。 我也有查詢,但沒有解決方案。

您需要在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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM