[英]How to pass a Div id as a parameter to a C# function?
I've created a div
with an id and i want to activate a function (code behind) with the div
id when someone click on the div
. 我创建了一个
div
有一个id,我想激活与功能(代码后面) div
ID当有人点击div
。 help please? 请帮助?
Project .aspx.cs 项目.aspx.cs
HtmlGenericControl Div1 = new HtmlGenericControl("div");
Div1.Attributes.Add("class", "ProjectItem");
Div1.Attributes.Add("id", p.groupCode);
Div1.Attributes.Add("runat", "server");
您需要阅读有关DIV的runat="server"
标签的信息
A nice way of doing it is adding a hidden server button to your page. 一种不错的方法是在页面上添加一个隐藏的服务器按钮。
<asp:Button runat="server" id="btnPostback" style="display:none" onclick="serverEventHandler" />
And then add this to your div init: 然后将其添加到您的div init中:
Div1.Attributes.Add("onclick","document.getElementById('" + btnPostback.ClientID + "').click()");
And then add this to your code behind: 然后将其添加到您的代码中:
protected void serverEventHandler(object sender, EventArgs e)
{
// Put code here
}
EDIT: 编辑:
I don't see any proper way of sending the Div id, but you can change the hidden button's text to the Div id and then read it on the click event: 我看不到任何发送Div ID的正确方法,但是您可以将隐藏按钮的文本更改为Div ID,然后在click事件中读取它:
Div1.Attributes.Add("onclick","document.getElementById('" + btnPostback.ClientID + "').value='Div1';document.getElementById('" + btnPostback.ClientID + "').click()");
And the click event: 和点击事件:
protected void serverEventHandler(object sender, EventArgs e)
{
string div_id=btnPostback.Text;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.