简体   繁体   中英

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). This <div> is generated by code:

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(); but I can't seem to figure out how to set the background of the currently clicked <div> .

The Panel Control is a Control which wraps it's contents inside a <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.

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

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