Reputation: 525
I have a dynamic data that has to be populated on UI from .cs file. Currently I am doing it this way:
File.aspx
<div id="divLinks" class="w3-content w3-display-container" style="max-width:100%;" runat="server">
</div>
File.aspx.cs
StringBuilder str = new StringBuilder();
str.Append("<a target=\"_blank\"><img class=\"mySlides\" src=\"" + imageLink + "\" style=\"width:100%; height:350px;\" runat=\"server\" onServerClick=\"MyFuncion_Click("+link+")\" ></a>");
divLinks.InnerHtml = str.ToString();
protected void LinkButton_Click(object sender, EventArgs e)
{
//Do something when clicked a link with parameter from that link as identifier
}
After doing this, I am not able to reach LinkButton_Click function from onServerClick.
Can someone help me with this siince I want a link with unique value to be passed to function for further processing.
Upvotes: 1
Views: 497
Reputation: 35564
You are trying to add aspnet Controls to the page as a string. That is not gonna Work. You need to add "real" Controls to the page.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
//do not add dynamic controls here
}
//create a new ImageButton instance on every page load
ImageButton ib = new ImageButton();
//set some properties
ib.CssClass = "mySlides";
ib.ImageUrl = "~/myImage.png";
//add the click method
ib.Click += LinkButton_Click;
//add the button to an existing control
PlaceHolder1.Controls.Add(ib);
}
Note that you need to add dynamic controls on every Page Load, and that includes a PostBack.
Upvotes: 1