Calling asp function from div - c#

I have creates a side list of some item which is generated dynamically.Using
HtmlGenericControl div1 = new HtmlGenericControl("div");
div1.Attributes.Add("id", "div"+(index));
div1.Attributes.Add("class", "teacher-data");
div1.Attributes.Add("onclick", "getHighlight(this)");
Image img = new Image();
img.Attributes.Add("class", "teacher-image");
HtmlGenericControl div2 = new HtmlGenericControl("div");
div2.Attributes.Add("class", "teacher-data-container");
Label lbl1 = new Label();
lbl1.Text = name + "<br/>";
lbl1.ID = "lbl_"+ (index++);
Label lbl2 = new Label();
lbl2.Text = degree;
Label lbl3 = new Label();
lbl1.Attributes.Add("class", "teacher-info");
lbl2.Attributes.Add("class", "teacher-info");
lbl3.Attributes.Add("class", "teacher-info");
div2.Controls.Add(lbl1);
div2.Controls.Add(lbl2);
div2.Controls.Add(lbl3);
div1.Controls.Add(img);
div1.Controls.Add(div2);
teacher_containr.Controls.Add(div1);
What I tried is that when some one attempt a click over that div tag it gets selected but here what i also want is it should call a asp.net function so that after clicking, relevant data comes over right side of it.But nothing is working.
Can we add some event or anything in div so that it calls asp.net function. I tried ajax method but it did not work.

Related

find dynamically added controls from container

I am generating dynamic textbox controls on drop down selected index change event.
protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (Attributes attribute in getAllAttributes(Convert.ToInt32(ddlCategories.SelectedValue)))
{
Panel div = new Panel();
div.Attributes.Add("class", "form-group");
HtmlGenericControl lbl = new HtmlGenericControl("label");
lbl.Attributes.Add("class", "col-lg-2 control-label");
lbl.InnerText = attribute.Name;
Panel innerdiv = new Panel();
innerdiv.Attributes.Add("class", "col-lg-10");
TextBox txt = new TextBox();
txt.ID = attribute.ID.ToString();
txt.Attributes.Add("class", "form-control");
innerdiv.Controls.Add(txt);
div.Controls.Add(lbl);
div.Controls.Add(innerdiv);
CustomAttributes.Controls.Add(div);
}
}
Now after the user fill up the values in the form i want to get the values of the dynamically generated controls. But CustomAttributes.findControls("") doesn't work for me. it gives null all the time.
I also tried
var textBoxesInContainer = CustomAttributes.Controls.OfType<TextBox>();
but it also doesnt work.
Can any one please tell me what is going wrong here.
Thanks
Finally i found the reason after googling the issue. The issue in this question is a view state.
In asp.net when the page post back it loses the viewstate for the dynamically generated controls. So to get over this issue i recreated the controls in the page load event when it is post back. in that way controls will be added back to the current page and i can find those controls in the button on click event.
thanks all for your time and guidence.
Panel div = new Panel();
Panel innerdiv = new Panel();
TextBox txt = new TextBox();
innerdiv.Controls.Add(txt);
div.Controls.Add(innerdiv);
CustomAttributes.Controls.Add(div);
Your CustomAttributes holds Panel.
Try this :
var textboxes = CustomAttributes.Controls.OfType<Panel>()
.Select(p => p.Controls.OfType<Panel>().First())
.Select(p => p.Controls.OfType<TextBox>().First())

HyperLink text not rendered after controls are added

I have a HyperLink control with text in its Text property.
With the following code:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Text = "Test";
link.Controls.Add(img);
When I do this, the image is rendered inside a a tag, but the text is not rendred.
Is there a way to render both the image and the text inside the Text property without throwing a third control in to the mix?
When you put any controls into the WebControl.Controls collection, it will ignore what you have inside Text. So if you want to render both text and other child controls, you should add the text into Controls:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Controls.Add(new Literal{ Text = "Test"}); // this line will add the text
link.Controls.Add(img);
I feel this should work out for you.
var link = new HyperLink();
var img = new HtmlGenericControl("img");
var lbl = new Label();
img.Attributes.Add("src", "text.png");
lbl.Text = "Test";
link.Controls.Add(img);
link.Controls.Add(lbl);
this.Controls.Add(link);
According to the MSDN article "The HyperLink control can be displayed as text or an image." So the answer is no, I'm afraid.

How to Add a Div and text in side the div in aspx using C#

How to Add a Div and text inside a div in .aspx file using C#.
I want to add a textbox for typing comment or query and a button to post that text/query.
to the server.
After redirect that page should get data from DB and Show on that page.
same as(all Forms)
like stackoverflow, we type Question, and submit it, can show it
Taken from code project site - To create a div inside a div (By the way it works if you are using asp.net)
System.Web.UI.HtmlControls.HtmlGenericControl Panel = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
Panel.ID = "panel1";
Panel.Style.Add(HtmlTextWriterStyle.BackgroundColor, "White");
Panel.Style.Add(HtmlTextWriterStyle.Height, "680px");
Panel.Style.Add(HtmlTextWriterStyle.Width, "660px");
Panel.Style.Add(HtmlTextWriterStyle.BorderColor, "Black");
Panel.Style.Add(HtmlTextWriterStyle.BorderStyle, "Groove");
Panel.Style.Add(HtmlTextWriterStyle.BorderWidth, "2px");
System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
div.ID = "panel12";
div.Style.Add(HtmlTextWriterStyle.BackgroundColor, "White");
div.Style.Add(HtmlTextWriterStyle.Height, "70px");
div.Style.Add(HtmlTextWriterStyle.Width, "60px");
div.Style.Add(HtmlTextWriterStyle.BorderColor, "Black");
div.Style.Add(HtmlTextWriterStyle.BorderStyle, "Groove");
div.Style.Add(HtmlTextWriterStyle.BorderWidth, "2px");
this.Controls.Add(Panel);
Panel.Controls.Add(div);

Add simple text to panel control

I have a panel in C#:
Panel aspPanel = new Panel();
Button aspbutton = new Button();
aspbutton.Text = "Download PDF";
aspbutton.Click += initDownload;
aspPanel.Controls.Add(aspbutton);`
I have added Attributes and buttons and all kinds of cool dynamic stuff. But I want to just add simple text and so far am unsuccessful.
I'm looking for how to add text behind the button. In the end the HTML code would render something like:
<input type="button"/> Hello, this is a button, please click
Can someone point me in the right direction?
To add Literal text after the button:
Panel aspPanel = new Panel();
Button aspbutton = new Button();
aspbutton.Text = "Download PDF";
aspbutton.Click += initDownload;
aspPanel.Controls.Add(aspbutton);
aspPanel.Controls.Add(new LiteralControl("some more text!"));
Try this
Label lbl = new Label();
lbl.Text = "Hello, this is a button, please click";
aspPanel.Controls.Add(lbl);

Created LinkButton using C# code not working

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

Categories