CMSEditableRegion inside a code loop - c#

I am trying to add a cmseditableregion to my Kentico webpart that exists inside of tabbed content, now the amount of tabs are flexible so I would like to generate this dynamically. I have tried the method that follows (stringbuilder) but it just renders it as html and not as a control when passed to a literal.
for (int i = 1; i <= TabCount; i++)
{
sb.AppendLine("<li class=\"htab-list__item--fininfo active\">");
sb.AppendLine("<a href=\"#financial-result\" class=\"htab-list__link tab-link\">");
sb.AppendLine("<cms:CMSEditableRegion runat=\"server\" id=\"ttl" + i.ToString() + "\" RegionType=\"Textbox\" RegionTitle=\"" + i.ToString() + " Title\" />");
sb.AppendLine("</li>");
Is there a way to make the CMSEditableRegion be able to be set dynamically in the code so that when the loop builds the page code it will be in the right spot as a control and not just html.
The full code has more html and 3 editable regions per loop but it wont even work with just one.

Adding control dynamically is done as follows:
// Let's assume that 'plc' is a placeholder. But it can be any control.
plc.Controls.Add(new LiteralControl("<li class=\"htab-list__item--fininfo active\">"));
plc.Controls.Add(new LiteralControl("<a href=\"#financial-result\" class=\"htab-list__link tab-link\">"));
plc.Controls.Add(new CMSEditableRegion { ID = "someid", RegionType = CMSEditableRegionTypeEnum.TextBox, RegionTitle = "sometitle" });
plc.Controls.Add(new LiteralControl("</li>"));
Also check out MSDN.

I suggest you to create an ad hoc PageType (called DocumentType in Kentico 7) where you can put the HTML text needed. Then you can display it using a Repeater web part or a ASP.NET repeater if, like me, you prefer to work in code.

Related

Get clientID for multi userControl with same Server ID

I have ASP.Net application that have multiple User Controls in the same page every one have its hidden field that holds a value, and every one have button that calls pop-up and through this value from hidden field to it.
The problem that when i try to access the hidden field and get the value inside , the program always get the last one (which created last).
How can i get the value of the inner hidden field in the current UserControl (Which i'm clicking the button from)?
Attempts:
var hdnRegion = "<%=hdnRegionId.ClientID%>";
var regionIdVal = $("#" + hdnRegion).val();
methodName(regionIdVal);
another one:
var currentControl = "<%=this.ClientID%>";
var hdnRegion = currentControl + "_" + "hdnRegionId";
var regionIdVal = $("#" + hdnRegion).val();
methodName(regionIdVal);
I also tried to call a property from code behind that returns the value and one that returns the whole control with no correct result.
Any suggestions would be appreciated...
Accourding to your comment under the question, your btnUpdate and hdnRegionId controls are in the same container (for instance in the same div) so try this:
$('input[id*="btnUpdate"]').click(function(){
var regionIdVal = $(this).parent().children('input[id*="hdnRegionId"]').val();
methodName(regionIdVal);
});
This is a JSFiddle Demo that simulate your HTML code that is rendered by ASP.NET.

Dynamicly creating imageButton that calls a JavaScript function in c#

I have the following c# code that creates image buttons which should call the javaScript method below
var imageButton = new ImageButton();
imageButton.ImageUrl = "Styles/unClicked.png";
imageButton.ID = "btn" + questionName;
imageButton.OnClientClick = "buttonPressed('" + questionName + "')";
Its outputting & # 3 9; instead of the ' in the html code and there for is causing an error (Ive had to add spaces tp prevent SO changing it to a comma)
onclick="buttonPressed('question0')
how to I prevent this?
Please try this one HtmlString (not working).
imageButton.OnClientClick = new HtmlString("buttonPressed('" + questionName + "')");
If it will not work, than you will need to create custom control or use javascript (JQuery) to add handler for click event.
<script>
$("<%=imageButton.ID%>").click(function() {
buttonPressed('<%=questionName%>');
})
</script>
The OnClientClick attribute is very limiting. Not totally sure this will work, but I have use similar code before to handle ghost text in textboxes...
imageButton.Attributes.Add("onclick", "buttonPressed('" + val + "')");
The attribute should not be HTML encoded.

creating RadEditor dynamically asp.net

I want to add RadEditor dynamically. It is getting added to the page but as soon as postback occurs i get Multiple controls with the same ID 'EditorRibbonBarResourcesHolder' were found. Below is the code that i am using to add control dynamically.
RadEditor editor = new RadEditor();
editor.ID = "editor_" + itemTypeattribute.ItemAttributeID + rand.Next();
cellAttributeValue.Controls.Add(editor);
editor.DialogOpener.ID = "editor_dialopOpener_" + itemTypeattribute.ItemAttributeID;
editor.DialogOpener.Window.ID = "editor_dialopOpener_window_"+ ItemTypeattribute.ItemAttributeID;
editor.ClientIDMode = ClientIDMode.AutoID;
editor.EnableEmbeddedScripts = true;
editor.Height = 200;
Any help is appreciated. Thanks
Whenever I need to use a radeditor "dynamically" I have it on the page from the start with visible=false and then show it when I need it.

C# dynamically created control issue

i'm having issues retreiving the values out of a dynamically created dropdownlist. all controls are created in the Page_Init section. the listitems are added at that time as well from an array of listitems. (the controls are named the same so should be accessable to the viewstate for appropriate setting.)
here is the function that attempts to retrieve the values:
protected void Eng98AssignmentComplete_Click(object sender, EventArgs e)
{
String myID = "0";
Page page = Page;
Control postbackControlInstance = null;
// handle the Button control postbacks
for (int i = 0; i < page.Request.Form.Keys.Count; i++)
{
postbackControlInstance = page.FindControl(page.Request.Form.Keys[i]);
//Response.Write(page.Request.Form.Keys[i].ToString());
if (postbackControlInstance is System.Web.UI.WebControls.Button)
{
myID = Convert.ToString(
postbackControlInstance.ID.Replace("button_", ""));
}
}
String txtholder = "ctl00$ContentPlaceHolder$Eng098Instructors_" + myID;
Response.Write("MYID: " + myID + "<br/>");
DropDownList ddInstructorCheck = (DropDownList)Page.FindControl(txtholder);
Response.Write("Instructor Selected: "
+ ddInstructorCheck.SelectedValue + "<br/>");
}
here is the output I get, no matter which instructor was selected.....
MYID: 1_1
Instructor Selected: 0
ctl00$ContentPlaceHolder$Eng098Instructors_1_1
the name of the control is correct (verified via view source)....
ideas?
You're going to a lot of work to build this fancy string:
ctl00$ContentPlaceHolder$Eng098Instructors_1_1
That is the client ID of your control, not the server id. This code is running on the server side, and so you need the server id. To get that control using the server id, you need to do this:
ContentPlaceHolder.FindControl("Eng08Instructors_1_1");
Notice I didn't look in the page, because your content place holder created a new naming container.
Also, the way your loop is set up the myID variable will always end up holding the last button in the Keys collection. Why even bother with the loop?
Based on your comments, a better way to find the id of the dropdownlist is like this:
string id = ((Control)sender).ID.Replace("button_", "Eng098Instructors_");
why not just save the control in an instance in your class so that you don't have to use FindControl?
Do you also re-create the controls during the postback? Dynamically generated/added controls must be re-created with every request, they are not automatically re-created.
Why don't you cast the sender? This should be the button that caused the postback:
string myId = "0";
Button btn = sender as Button;
if (btn != null)
myId = btn.ID
...
You need to perform something like this because the UniqueID property is the key in Request.Form.
List<Button> buttons = new List<Button>();
List<DropDownList> dropdowns = new List<DropDownList>();
foreach (Control c in Controls)
{
Button b = (c as Button);
if (b != null)
{
buttons.Add(b);
}
DropDownList d = (c as DropDownList);
if (d != null)
{
dropdowns.Add(d);
}
}
foreach (String key in Request.Form.Keys)
{
foreach (Button b in buttons)
{
if (b.UniqueID == key)
{
String id = b.ID.Replace("button_", "");
String unique_id = "ctl00$ContentPlaceHolder$Eng098Instructors_" + id;
Response.Write("MYID: " + id + "<br/>");
foreach (DropDownList d in dropdowns)
{
if (d.UniqueID == unique_id)
{
Response.Write("Instructor Selected: " + d.SelectedValue + "<br/>");
break;
}
}
}
}
}
I'm not sure why you are generating the control in code (you can still add items dynamically if you do), but the code that generates the controls would probably be a huge help here. I'm guessing you are not setting the list item value, and instead just setting the list item text. Try seeing what you get from the SelectedText field and post your control creation function.
EDIT:
In response to your comment on #Martin's post, you said "yes I recreate the controls in the Page_Init function each time the page is created (initial or postback)". Are you also setting the selected value when you create them?
You can also use controls on the page even if your data comes from a database, the controls themselves don't have to be dynamically generated.
How about this?
((Button)sender).Parent.FindControl(myid)
Edit:I misunderstood your question. But i think you should follow page lifecycle. it is common issue for dynamically created controls.
I did some research and here is some info about Dynamically Created Controls may help you...
I had 2 catches.... here's what they were.
1. I didn't clear the table I was adding to before re-creating the controls.
apparently my attention to detail was off yesterday, i'm pretty sure the ctlXX frontrunner of the control was some different number upon postback due to how I was recreating the controls.
2. I was assigning the same list to all the dropdownlist controls.
once I called the lookup upon each creation a dropdownlist control, all works well.
anyway for what it's worth....

On which Page life cycle are the Client IDs generated?

i am programatically adding Webcontrols in to a User Control i am also adding a javascript event passing the controlID as a parameter but the clientID is the one i assigned a it does not contain the one that asp.net generates
var txt = new TextBox();
txt.ID = "MyID"+Number;
chkBox.Attributes.Add("onClick", "EnableTxtBox('" +txt.ClientID + "');");
i can workAround this by adding the parent control ID
chkBox.Attributes.Add("onClick", "EnableTxtBox('" + this.ClientID+"_"+txt.ClientID + "');");
On which Page life cycle are the Client IDs generated?
You need to add the control to the control hierarchy before you add the attribute.
var txt = new TextBox();
txt.ID = "MyID"+Number;
Controls.Add ( txt );
chkBox.Attributes.Add("onClick", "EnableTxtBox('" +txt.ClientID + "');");
ControlCollection is no ordinary collection; it notifies the owner control when controls are added, and the control can take appropriate actions.
You should be able to add the attribute during OnPreRender(). INamingContainer is so painful sometimes...

Categories

Resources