Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I am creating no of buttons dynamically in C#.I want to perfrom mouseover on dynamic button.my function is executing but I am not getting button id that I can perfrom some styles on that button,i am getting button id null.What should I do.

What I have tried:

C#
bt = new Button();
                       bt.ID = "button" + i;                       bt.Height=17;
                       bt.ClientIDMode = ClientIDMode.Inherit;
                      
                       bt.Attributes.Add("style", "display:block");
                       bt.Attributes.Add("OnMouseOver", "javascript:display('" + bt.ClientID + "')");
                       bt.Click += new EventHandler(Buttonclick);
                       e.Row.Cells[i].Controls.Add(bt);



JavaScript code:

function display(button) {
alert("hello funtion execting" + button);
var el = document.getElementById(button);
alert(el);
el.style.display = "none";

}
Posted
Updated 8-Dec-16 22:24pm
v2

You have to remember that javascript runs off of what is sent to the browser, so look at the source of the page and you'll see something like

<input type="submit" ID="GridView1_button1_0" OnMouseOver="javascript:display('button1')" />


So the id in your onmouseover doesn't match the id of the button you have created. The reason for this is because when you add a button to a container like a gridview it alters the ID to ensure the id is going to be unique on the page as you might have another gridview on the page also creating "button1".

The solution is to add the button first so that it gets it's unique ID and then use that ID in your js

C#
bt = new Button();
bt.ID = "button" + i; // add the button right away
MyPanel.Controls.Add(bt);
bt.Height = 17;
// the rest of your code here and now bt.ClientID will return the correct value
 
Share this answer
 
v3
Comments
Pragya Nagwanshi 10-Dec-16 2:45am    
Ohh yaa right..Thank
Hi Pragya,

Review below code, this will help you a lot

C# Server side code:
C#
protected void Page_Load(object sender, EventArgs e)
        {

            List<string> strCol = new List<string>();


            for (int i = 0; i < 10; i++)
            {
                strCol.Add("DataRow" + i.ToString());
            }

            GridView1.DataSource = strCol;
            GridView1.DataBind();
            
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Button bt = new Button();
            bt.ID = "button" + e.Row.RowIndex; bt.Height = 17;
            bt.ClientIDMode = ClientIDMode.Inherit;

            bt.Attributes.Add("style", "display:block");
            bt.Attributes.Add("OnMouseOver", "javascript:display(this);return false;");
            e.Row.Cells[0].Controls.Add(bt);
        }


Client side code:
ASP.NET
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField ></asp:TemplateField>
                    </Columns>
                </asp:GridView>

HTML
<script>

       function display(btnClicked)
       {
           alert($(btnClicked).attr('id'));
           $(btnClicked).hide();
       }

   </script>



Best of luck!! :)
 
Share this answer
 
Comments
Pragya Nagwanshi 10-Dec-16 2:46am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900