简体   繁体   English

ASP.NET通过后面的代码将onclick添加到div

[英]ASP.NET add onclick to div via code behind

I'm using ASP.NET and I want to be able to click on a <div> and set the background to a color (prefably an image later on). 我正在使用ASP.NET,我希望能够单击<div>并将背景设置为一种颜色(以后可能是图像)。 This <div> is generated by code: <div>由代码生成:

public void generate (int[] _blocks, int _width, int _height, int _distance) {
        int HorizontalBlocks = _blocks[0];
        int VerticalBlocks = _blocks[1];
        int top = 0;
        int left = 0;

        for (int i = 0; i < VerticalBlocks; i++) {
            for (int x = 0; x < HorizontalBlocks; x++) {
                System.Web.UI.HtmlControls.HtmlGenericControl Div = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

                Div.ID = "Box_" + x + "_" + i;
                Div.Style.Add(HtmlTextWriterStyle.Width, _width.ToString() + "px");
                Div.Style.Add(HtmlTextWriterStyle.Height, _height.ToString() + "px");
                Div.Style.Add(HtmlTextWriterStyle.Top, top.ToString() + "px");
                Div.Style.Add(HtmlTextWriterStyle.Left, left.ToString() + "px");
                Div.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#848484");
                Div.Style.Add(HtmlTextWriterStyle.Position, "absolute");
                Div.Style.Add(HtmlTextWriterStyle.Cursor, "pointer");
                Div.Attributes.Add("runat", "server");
                Div.Attributes.Add("onclick", "setBackground();");

                grid.Controls.Add(Div);

                left += _width + _distance;
            }

            left = 0;
            top += _height + _distance;
        }
    }

I have tried creating the method setBackground(); 我尝试创建方法setBackground(); but I can't seem to figure out how to set the background of the currently clicked <div> . 但我似乎无法弄清楚如何设置当前单击的<div>的背景。

The Panel Control is a Control which wraps it's contents inside a <div> . Panel控件是将其内容包装在<div>的控件。 And you can change it's properties from code behind. 您可以从后面的代码中更改其属性。

<asp:Panel ID="Panel1" runat="server">

    <%-- html content here --%>

</asp:Panel>

Code behind. 后面的代码。

protected void Button1_Click(object sender, EventArgs e)
{
    Panel1.BackColor = Color.Red;
}

And if you want to change background colors you can do this for example. 如果要更改背景颜色,则可以执行此操作。

<style>
    .myDiv {
        width: 100px;
        height: 100px;
        border: 1px solid black;
        background: inherit;
    }
</style>

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click"><div class="myDiv"></div></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton1_Click"><div class="myDiv"></div></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton1_Click"><div class="myDiv"></div></asp:LinkButton>

And the click event in code behind. 而click事件在代码后面。

protected void LinkButton1_Click(object sender, EventArgs e)
{
    LinkButton linkButton = sender as LinkButton;
    linkButton.BackColor = Color.Green;
}

Got it working faster than I thought. 它的工作速度比我想象的要快。 This is what I did. 这就是我所做的。

I used this: 我用这个:

                Div.Attributes.Add("onclick", "div_click(this.id)");

And then I use this via script: 然后我通过脚本使用它:

        <script>
            function div_click(clicked_id) {
                alert(clicked_id);
            }
        </script>

Thanks for all the replies 感谢所有的答复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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