简体   繁体   中英

How to get the id of a div tag in code behind

I am having two div tags and in it there are linkbuttons. When I click on any of the linkbutton I need to get the id of div tag in code behind. I have no idea how to do this. Below is my code of one div tag:

<div id='qhse' class="qhse" runat="server"></div>

And C# code:

public void GetQHSEManual()
{
    DataTable dsMenu = new DataTable();
    gObj.category = "1";
    dsMenu = gObj.GetAllSubCategory();
    for (int i = 0; i < dsMenu.Rows.Count; i++)
    {
        LinkButton myLnkBtn = new LinkButton();
        myLnkBtn.ID = "lbtn1" + dsMenu.Rows[i]["Subcategory"].ToString();
        myLnkBtn.CssClass = "linkButton";
        myLnkBtn.Click += new EventHandler(Dynamic_Click);
        myLnkBtn.Text = dsMenu.Rows[i]["Subcategory"].ToString();
        myLnkBtn.Text += "<br>";
        qhse.Controls.Add(myLnkBtn);    
    }     
}

You should do it with JavaScript. If you have 1 element inside of your div holder this should be done easy. JavaScript lib allows to use parent id when you click on your button inside div. See jQuery for this purpose.

I would suggest you that you separate server side and client side logic well. Think about local storage, sessions and cookies if you need to pass some data to the backend. In your case I would use an AJAX call from the client and implement some logic on the backend.

$("a:contains('continue')").closest('div').attr('id');

在此代码中,我们找到了显示文本值为“ continue”的href的父div ID。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM