I have created link button using c# code but these are not click able why?
This is c# code
<% {
List<string> PMlist = new List<string>();
PMlist = PManifacutrerList;
foreach (string PM in PMlist)
{
Response.Write(PM);
}
}
%>
And following code is used to add list li in the PMlist
PMList.Add(
"<li><asp:LinkButton ID=\"LinkButton1\" style=\"color: Blue;font-family: Microsoft New Tai Lue; text-decoration: none;\" runat=\"server\">" +
ds.Tables[0].Rows[i]["PM_name"].ToString() + "</asp:LinkButton></li>");
Your Response.Write(PM) is simply writing HTML to the response output. If you really want to use a LinkButton, you need to create an instance:
LinkButton lb = new LinkButton();
lb.Text = "click me";
lb.Click += new EventHandler(delegate (object s, EventArgs ev) {
// handle click event
});
form1.Controls.Add(lb);
If you don't need a server post back, then you can just use a simple link such as:
PMList.Add(
"<li><a href='#' style='color: Blue;font-family: Microsoft New Tai Lue; text-decoration: none;'>" + ds.Tables[0].Rows[i]["PM_name"].ToString() + "</a></li>");
Hope it helps!
You need to write OnClick event
In order to dynamically add a control there must be a container.
If you don't have a container on page you can a placeholder
control & add controls to it
You must create an instance of control to add it onto page
Label myLabel = new Label();
myLabel.Text = "Sample Label";
myPlaceHolder.Controls.Add(myLabel);
Adding Controls dynamically:MSDN
Related
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 />");
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.
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.
Here is my code for creating the textbox and label from code
int i = 0;
Label lbl = new Label();
lbl.Text = "Label - " + i.ToString();
lbl.ID = "Label - " + i.ToString();
TextBox txt = new TextBox();
txt.Text = "txt - " + i.ToString();
data.Controls.Add(lbl);
data.Controls.Add(txt);
Is it possible I add a new line after the data.Controls.Add(txt);?
I tried to use Environment.NewLine but its not working.
anyone have any idea how to do it?
data.Controls.Add(new LiteralControl("<br/>"));
Add this for new line
You should use CSS if it's purely for presentation.
Add .CssClass = "TextboxSpace" to the textbox
Add a css file to the project (or add a new style to an existing one) something like
.TextboxSpace {
margin-bottom: 20px;
}
Add a Literal Control in between your two controls for line space.
Programatically you can do it.
Set Literal Control's Mode property to PassThrough
For Example :
Literal myLiteral = new Literal();
myLiteral.Id="lit_newline"
myLiteral.Mode = "PassThrough";
data.Controls.Add(myLiteral);
Then Add this control in between your textbox and label control
Also you can do like this
data.Controls.Add(new LiteralControl("<br />"));
And If you want, you can apply css to your textbox without adding the literal control which will increase the height - as answered by user : bgs264
Are you saying the space between the two controls.. If this is the case, you can set the location for the next control such that there is a empty space after that text.
I'm listing the contents of my basket by adding fresh HTML to a panel so it lists it on the aspx page. I'm also trying to add an asp:button at the end which will allow users to remove the items, however the button isnt showing!
Code;
string sHTML = #"<div class='item_bar'>
<div class='item_id'>" + id + #"</div>
<div class='item_title'>" + name + #"</div>
<div class='item_price'>" + cost + #"</div>
<asp:Button class='button' Text='Remove' runat='server' CommandArgument='"+name+#"' OnClick='removeItem' />
</div>";
basketDiv.Controls.Add(new LiteralControl(sHTML));
thanks
If you are going to add controls such as the ASP:BUTTON, you would need to add them not via string but actually doing it
this.pnlFrame.Controls.Add(new Button() {
ID = "buttonId1",
Text = "Text for new button"
});
Keep in mind that this needs to be registered when the page is actually being compiled so that is why it is not working in your case.
Check this link also Adding button to panel dynamically and getting it's parent ID
If you do not want events being captured at runtime, and would like to do the actions via jquery, you can add it using string like you are doing, and add a normal HTML input with type submit
<input type='submit' value='New button'/>
However then when clicked, you need to create scripts on the client side to capture those events.
Create button and then set the properties u want then add in panel
// add the litenal control - DIVs
basketDiv.Controls.Add(new LiteralControl("<div class='item_bar'><div class='item_id'>" + id + "</div><div class='item_title'>" + name + "</div><div class='item_price'>" + cost + "</div>"));
// create button and Add it to basketDiv
Button button = new Button();
button.Name = "Button1";
// you can added other attribute here.
button.Text = "New Button";
button.Location = new Point(70,70);
button.Size = new Size(100, 100);
basketDiv.Controls.Add(button);
// close the parent Liternal "DIV class='item_bar'"
basketDiv.Controls.Add(new LiteralControl("</div>"));
I'm trying to generate some html programmatically in my code behind for a user control I'm designing.
I've been looking around, but can't seem to figure out how to dynamically generate some h1 tags for content I'll be displaying.
Is it just a Label with a special property set?
var h1 = new HtmlGenericControl("h1");
h1.InnerHtml = "header content";
You could also use:
<h1 runat="server" />
You can use label or literal controls as shown below:
Label1.Text = "<h1>HI All</h1>";
Or
string heading = "HI All";
Label1.Text = string.Format("<h1>{0}</h1>", heading);
You can do this easily by
literall.Text ="<h1>ABCD</h1>";
Let, you have a server control like this,
<div class="cls" runat="server" id="MyServerControlDiv"></div>
using HtmlGenericControl
var h1 = new HtmlGenericControl("h1");
h1.InnerHtml = "header content";
MyServerControlDiv.Controls.Add(h1);
using LiteralControl
MyServerControlDiv.Controls.Add(new LiteralControl("<h1>"));
MyServerControlDiv.Controls.Add(new LiteralControl("header content"));
MyServerControlDiv.Controls.Add(new LiteralControl("</h1>"));
Use Literal control, Literal1.Text = "<h1>" + texttodisplay + "</h1>";
ASP literals may help you.
http://ganeshmohan.wordpress.com/aspnet-and-c-generate-html-controls-dynamically/
Personally I prefer to make a class for it.
public class H1: HtmlGenericElement
{
public H1(): base("h1") { }
}
Or maybe.
public class H: HtmlGenericControl
{
public H(int size): base("h" + size) { }
}
Let us assume that you have an asp placeholder in your design:
PlaceHolder placeHolder1 = new PlaceHolder();
string yourText = "Header Text";
Label myLabel = new Label();
myLabel.Text = "<h1>" + yourText + "</h1>";
PlaceHolder1.Controls.Add(myLabel);
OR
PlaceHolder1.Controls.Add(new LiteralControl ("<h1>")) // and likewise
for </h1>.
Using a generic HTML control or a literal control is fine just so long as you don't want to insert any child controls. If you do need to append a child control such as a label for example, then you will want to create you header as a Web Control like follows:
WebControl H2Header = new WebControl(HtmlTextWriterTag.H1);
And then you can append a child control like this:
Label labHeader = new Label() { Text = "Some Header Text" };
H2Header.Controls.Add(labHeader);
What i always prefer to use:
In the front end put the desired html control like
<h1 id="title" runat="server">XXX</h1>
Then in the behind code declare the desired control like
protected HtmlGenericControl PublicTitle
{
get
{
return this.title;
}
}
And anywhere in the code play whith the control
PublicTitle.InnerHtml = state.Equals("win") ? "Win" : "Lost";
PublicTitle.Attributes.Add("src", "https://xxxx");
I tried to call click event but it does not call it
I tried this codes: like
objImage.Click += new ImageClickEventHandler(WebForm4.ImageButtons_Click);
objImage.Click += WebForm4.ImageButtons_Click;
my codes :
public HtmlGenericControl CreateDIV_OyVerme_Sub_Yildiz(string id, int subId)
{
HtmlGenericControl objDiv = new HtmlGenericControl("div");
objDiv.ID = strControlName_DivYildiz + id + "_" + subId;
objDiv.Attributes.Add("class", strClassName_DivYildiz);
//objDiv.Attributes.Add("runat", "server");
ImageButton objImage = new ImageButton();
objImage.Attributes.Add("runat", "server");
objImage.CommandArgument = id;
//objImage.Src = strImgSrc_yildiz;
objImage.Click += new ImageClickEventHandler(WebForm4.ImageButtons_Click);
//objImage.Click += WebForm4.ImageButtons_Click;
objImage.Attributes.Add("onclick", "WebForm4.ImageButtons_Click();");
objImage.ID = strControlName_ImageYildiz + id +"_" + subId;
objImage.ImageUrl = strImgSrc_yildiz;
objImage.OnClientClick = strOnClientClickFunc_yildiz;
// objImage.Attributes.Add("OnClick","WebForm4.amethod (o;");
objImage.Style.Add(HtmlTextWriterStyle.Height, "19px");
objImage.Style.Add(HtmlTextWriterStyle.Width, "20px");
objImage.Style.Add(HtmlTextWriterStyle.BorderWidth, "0px");
objImage.Style.Add(HtmlTextWriterStyle.Position, "relative");
objImage.Style.Add(HtmlTextWriterStyle.Top, "13px");
objImage.Style.Add(HtmlTextWriterStyle.Left, "6px");
objImage.Style.Add("float", "left");
objImage.ToolTip = subId + "/" + 5;
// calling the method
// objImage.Attributes.Add("OnClientClick", "return(GetRssID(objRssItem));");
// var duck = objRssItem;
// objImage.Click += (s, e) => { WebForm4.amethod(objRssItem); };
//objImage.Click += WebForm4.amethod (objRssItem);
objDiv.Controls.Add(objImage);
return objDiv;
}
You don't need to add the attribute: onclick.
remove this line:
objImage.Attributes.Add("onclick", "WebForm4.ImageButtons_Click();");
you can simply do it like this:
ImageButton img = new ImageButton();
img.Click+=new ImageClickEventHandler(img_Click);
img.ID = "img";
img.Attributes.Add("runat","server");
form1.Controls.Add(img);
There is no issue in registering the event handler - either of below lines would register the event handler:
objImage.Click += new ImageClickEventHandler(WebForm4.ImageButtons_Click);
objImage.Click += WebForm4.ImageButtons_Click;
It's a dynamically generated image button, so for event handler to work, you need to ensure that
The image button must be re-created and added to control hierarchy on the subsequent post-back. If control is not present on post-back then it cannot raise the event.
You must ensure that the control has same ID on the post-back. Not only that it must be in the same control hierarchy and its parents also need to have same ids. The essential point is that control gets located using its ID.
The control (image button) must get added to the page's control hierarchy as early as possible - Page_Init would be the best but if that's not possible then page_load may work. Creating the control in any subsequent event would not trigger the post-back event.