0

My Code is below which is not working for applied css for created control at client side when applying it,
my css class is : img-rounded

string strClass = "img-rounded", strColor="Green";
StringBuilder sb = new StringBuilder();
sb.Append("<asp:Button OnClick=\"Button1_Click\" runat=\"server\" ID=\"Btn1\" CssClass=\"" + strClass + "\"  BackColor=\"" + strColor + "\">" + cellData + "</asp:Button><br />");

2 Answers2

0

First of all you cannot add a <asp:Button as a string to the page and expect it to work. If you want to add true asp buttons dynamically you have to take a different approach:

        Button button = new Button();
        button.CssClass = strClass;
        button.BackColor = System.Drawing.Color.Green;
        button.ID = "Btn1";
        button.Text = cellData;
        button.Click += new EventHandler(Button1_Click);
        PlaceHolder1.Controls.Add(button);

Or you could still add a "normal" HTML button to the page, but then you have to use class= instead of CssClass=, which is an attribute of most asp controls (just as BackColor).

sb.Append("<input type=\"button\" onclick=\"Button1_Click()\" id=\"Btn1\" class=\"" + strClass + "\" value=\"" + cellData + "\"><br />");

Note that the OnClick is now a javascript call with () at the end. Of course it will not fire an event in code behind.

VDWWD
  • 35,079
  • 22
  • 62
  • 79
0

Manish that is not how you add an asp button, this article explains how to do it. http://www.aspsnippets.com/Articles/Dynamic-Controls-Made-Easy-in-ASP.Net.aspx Once you create the button in the code behind then you can access all its properties including cssclass.

snit80
  • 623
  • 7
  • 13