Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
string customButtonControl = "<asp:Button ID="Button1" runat="server" Text="Button" />";

I am wrting this code in my c# code, but I am getting design time error. Can anyone suggest me where I am doing wrong? Actually I am creating a button by c#.
Posted
Updated 31-Jan-11 23:59pm
v2

There are two ways of escaping quote in a a string literal:

As in other Answers:
C#
string target = "word in \"quotes\"";


Or, alternatively the verbose syntax:
C#
string target = @"word in ""quotes""";


Also, the verbose syntax is designed to interpret source code lines of a multi-line strings with end-of-lines in the literal:

C#
string target = @"A multi-line
string,
verbosely";



—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 1-Feb-11 17:52pm    
Additional info the OP may find interesting 5+ :)
While the other answers are technically correct about how you escape quotes in a string, that will not help you dynamically create a button in C#. Here is an example of how to accomplish that:
C#
protected void Page_Load(object sender, EventArgs e)
{
    Button b = new Button { ID = "btnGenerated", Text = "Click Me!" };
    b.Click += new EventHandler(delegate(object mySender, EventArgs myArgs)
    {
        b.Text = "You Clicked Me!";
    });
    Page.Form.Controls.Add(b);
}
 
Share this answer
 
Comments
Henry Minute 1-Feb-11 20:49pm    
Does this still apply in ASP.NET? Genuine question!
AspDotNetDev 1-Feb-11 21:06pm    
Yep. In fact, it only applies to ASP.Net (see that page load event handler and that Page.Form property?). If the OP tries to toss a dynamic server-side control onto the form using a string, it's not going to work. They could toss HTML onto the page, but they can't us a string to create a dynamic server-side control.
Henry Minute 1-Feb-11 21:11pm    
Cheers! :)
Change your string as follows:
string customButtonControl = "<asp:Button ID=\"Button1\" runat=\"server\" Text=\"Button\"/>";


Hope this answer your question
 
Share this answer
 
v2
Comments
Estys 1-Feb-11 6:06am    
Problem with quotes right :)? Me as well.
Try it like this :

string customButtonControl = "<asp:button id=\"Button1\" runat=\"server\" text=\"Button\" />";


The "\" before the double quotes signify a special character is following.

(can't get the quotes right in my example :( )
Cheers
 
Share this answer
 
v3
use this string:


string customButtonControl = "<asp:Button ID=\"Button1\" runat=\"server\" Text=\"Button\" />";


thx
 
Share this answer
 

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