this is MyFirstRow in my .aspx file:
<ul class="dropdown-menu" id="MyFirstRow" runat="server">
I created the control with this Code in CodeBehind :
Control Con = FindControl("MyFirstRow");
LiteralControl LCx = new LiteralControl();
LCx.Text = #"<li class='dropdown' id='MyFirstRow34' runat='server'>
<a href='#'>Sample<span class='caret'></span></a></li>";
Con.Controls.Add(LCx);
so far everything is ok, but after next line when I use this code, this not able to find any control In the event that this added Correctly:
Con = FindControl("MyFirstRow34");
where is my mistake?
tnx for your response.
Edited :
My question is how to access this control with id=MyFirstRow34 in Code-Behind?
The <li class='dropdown' id='MyFirstRow34' runat='server'> string content assigned to the LiteralControl doesn't get interpreted by ASP.NET; this string is only part of the html output.
In order to find the control by the given Id, you must assign the Id to the controls ID property, as shown below.
LiteralControl LCx = new LiteralControl { ID = "MyFirstRow34" };
The full code of your example will look like this:
Control Con = FindControl("MyFirstRow");
LiteralControl LCx = new LiteralControl { ID = "MyFirstRow34" };
LCx.Text = #"
<li class='dropdown'>
<a href='#'>Sample<span class='caret'></span></a>
</li>";
Con.Controls.Add(LCx);
Control myFirstRow34 = FindControl("MyFirstRow34");
Related
Textbox1.text is user can enter html page name, so that its is appending to panel through literal.(loading html page to pannel).
string val = TextBox1.Text;
string location = Server.MapPath(".");
string path = location + "\\HTML\\" + val + ".html"; // HTML IS FOLDER NAME IN MY PROJECT
string readText = System.IO.File.ReadAllText(path);
Panel1.Controls.Clear();
Literal lit = new Literal();
lit.Text = readText;
Panel1.Controls.Add(lit);
Actually in Html page few controls which are in format of input (<input style="position: relative;" id="T0" onmouseup="mUp(this.id)" class="ui-draggable" onmousedown="mDown(this.id)" value="" type="text">)
I have to find those id's and text to save in database.
how to find the controls in panel now?
Give an ID to the control when you add it.
Literal lit = new Literal();
lit.Text = readText;
lit.ID = "myLiteral";
Panel1.Controls.Add(lit);
Then you can get it back as follows:
Literal lit = (Literal)Panel1.FincControl("myLiteral");
Remember that dynamically added controls must be created added again on every PostBack that follows as long as you want to have access to them.
Give your Literal an ID and then you can access it via FindControl...
Literal myLiteral = Panel1.FindControl("litId") as Literal;
if (myLiteral != null)
{
// ... do something
}
EDIT: (Missed the last part of your question)
You need to use ParseControl([string value]) on the HTML content which returns a control and then add that control (containing all child controls) to the Panel. Then you can use FindControl to locate child controls. For this method, the controls must be .NET controls (ie.
Since you did not give id to the control, u can find them by Panel1.Controls[index], since it is the first control added u can access at Panel1.Controls[0]
My master page contains this:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<asp:Label ID="User" runat="server" Text="Welcome, " Visible="false"></asp:Label>
</asp:ContentPlaceHolder>
I'm trying to access the label like this:
Label welcomeLabel;
ContentPlaceHolder cPlaceHolder;
cPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if (cPlaceHolder != null)
{
welcomeLabel = (Label)cPlaceHolder.FindControl("User");
if (welcomeLabel != null)
{
welcomeLabel.Text = "Welcome, " + u.Name;
welcomeLabel.Visible = true;
}
}
I've checked that ContentPlaceHolder1 is actually found and using HasControls() on it returns true but I can't seem to access the label.
Help me?
I also having same prblem.
what i have done is
MasterPage ctl00 = FindControl("ctl00") as MasterPage;
ContentPlaceHolder cplacehld = ctl00.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
Label label1= cplacehld.FindControl("User") as Label;
try with:
((Label)this.Page.Master.FindControl("User")).Text = "Welcome, " + u.Name;
((Label)this.Page.Master.FindControl("User")).Visible = true;
This issue dogged me for about two hours until I realized that if I have a asp:Content tag on my page with the contentplaceholderID set to the ID for the contentplaceholder in the master page that I will never be able to access any controls in the contenplacholder. The asp:Content page is always merged with the MasterPage content, even if the asp:Content tag is empty on your content page. To provide for the default content, I moved my label outside of the contentplaceholder tag and set the visibility to false. If I then dynamically determined that I didn't have any content for my asp:Content tag I would then just set the label visibility to true to display my default content. Not exactly elegant, but it works.
I'm trying to create a set of tabs at runtime and they were, for a time, working correctly- however now they just seem to display the contents of all tabs underneath the previously rendered tab. As silly as it seems, as far as I can tell, in terms of code nothing has changed since the code was working so I am a little confused as to what is going on...
For the purpose of this example I will not use a loop, although this seems irrespective as the loop doesn't appear to be the problem.
<asp:Panel runat="server" ID="pnlTabs">
<ul runat="server" id="ulSections" />
</asp:Panel>
I've come to the conclusion that my code must be at fault so here is the C#:
HtmlGenericControl liTab = new HtmlGenericControl("li");
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = Tab1;
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);
var pnl = new Panel();
pnl.ID = Tab1;
pnlTabs.Controls.Add(pnl);
I place my controls and what not on the "pnl". Can anyone please tell me what my mistake is?
//THIS ADDS THE <A> to the <LI>
liTab.Controls.Add(anTab);
//THIS ADDS THE <LI> to the <UL>
ulSections.Controls.Add(liTab);
//THIS CODE Appear to have nothing to do with anything.
var pnl = new Panel();
pnl.ID = Tab1;
pnlTabs.Controls.Add(pnl);
Can you confirm if the HTML is even being rendered in the html when you View Source.
Where in your code behind is this code... in a button click, on Page Load, On Init ?
I discovered what the problem was, it was basically that the InnerText of "anTab" cannot be the same as the href attribute of "anTab" as this causes confusion and results in the content from all tabs to appear only in the first (selected) tab, while the remaining tabs don't function.
For example, the following code works correctly as the InnerText of "anTab" is "Tab 1" and the href is looking for a control with the ID "Tab1", which is the ID of the panel.
HtmlGenericControl liTab = new HtmlGenericControl("li");
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = "Tab 1";
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);
var pnl = new Panel();
pnl.ID = "Tab1";
pnlTabs.Controls.Add(pnl);
Below we can see that the InnerText of "anTab" is the same as the href and the ID of the panel we wish to use as the tab, this causes the confusion as the href now assumes you want the "tab click" to point to itself instead of the actual panel.
HtmlGenericControl liTab = new HtmlGenericControl("li");
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = "Tab1";
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);
var pnl = new Panel();
pnl.ID = "Tab1";
pnlTabs.Controls.Add(pnl);
I have a dynamic number of data elements that need to be added to my asp.net page. I am currently trying to do this by looping through a result data set like this:
protected void Page_Load(object sender, EventArgs e)
{
UserDB db = new UserDB();
string employeeIDOrName = Request.QueryString["employeeID"];
DataSet ds = db.GetUserSearch(employeeIDOrName);
DataTable table = ds.Tables[0];
foreach (DataRow row in table.Rows)
{
string employeeID = row[0].ToString();
string emailAddress = row[1].ToString();
string phone = row[2].ToString();
string details = emailAddress + " | " + phone;
UserControl uc = new Controls_EmployeeSearchResults(employeeID, details);
placeholder1.Controls.Add(uc);
}
}
Then in the employeeSearchResults code, I have:
<div class="returnDataBoxSup" onclick="insertAddedSup('1091912','Shawna Burns');">
<div id="searchResults" runat="server">
</div>
</div>
for the ascx page and
public Controls_EmployeeSearchResults(string name, string details)
{
/*<div class="returnDataBoxSup" style="overflow:hidden;" onclick="insertAddedSup('1091912','Shawna Burns');">
<span ID="spanName" runat="server" style="text-transform:capitalize; white-space:nowrap"></span><br />
<span ID="spanDetails" runat="server" style="font-size:0.7em; white-space:nowrap"></span>
</div>*/
//Div
HtmlGenericControl div1 = new HtmlGenericControl("div");
div1.ID = "returnDataBoxSup";
div1.Attributes["style"] = "overflow:hidden;";
div1.Attributes["onClick"] = "insertAddedSup('1091912','Shawna Burns');";
div1.Attributes["class"] = "returnDataBoxSup";
//Span Name
HtmlGenericControl span1 = new HtmlGenericControl("span");
span1.ID = "span_" + name;
span1.Attributes["style"] = "text-transform:capitalize; white-space:nowrap";
span1.InnerHtml = name;
//Span Details
HtmlGenericControl span2 = new HtmlGenericControl("span");
span2.ID = "spanD_" + name;
span2.Attributes["style"] = "font-size:0.7em; white-space:nowrap";
span2.InnerHtml = details;
div1.InnerHtml = span1.ToString();
div1.InnerHtml += span2.ToString(); ;
//div1.Controls.Add(span1);
//div1.Controls.Add(span2);
//searchResults = new HtmlGenericControl("div");
searchResults.InnerHtml = div1.ToString();
}
in the code behind. I first tried to add the span controls to the div, but I was receiving an exception. When I tried to add the controls to the inner html, it said that it could not convert the control type to string. Is there any way that I can do what I am trying to do?
I receive employee data in a data set, then I want to loop through those and create a stylized div for each employee record and display it on my page. Any thoughts?
Using the user control isn't a bad idea, using a ListView could be a lot better/easier though.
But lets say you stick with a div and a user control for now. Firstly, you don't create usercontrols the same way you create other controls because you are part way through the page life cycle and they need to be caught up. Instead use LoadControl (see: https://stackoverflow.com/a/4786121/328968).
Second, don't pass the values into the constructor. Create a function in your usercontrol called LoadData and pass the values to that. After creating your usercontrol using LoadControl you call LoadData and pass in the values. LoadData takes care of loading the data into the various controls which, unlike your example, should probably already exist in your usercontrol.
Try the InnerText to see if it works.
I want to add something like this in the code behind:
<ul>
<li><a>A</a></li>
</ul>
However, the ListItem of ASP.NET seems to allow text only:
BulletedList UserSubMenuList = new BulletedList();
ListItem EditUserItem = new ListItem("Edit Profile");
Is there other way to add content between <li></li> tag with code behind rather then using HtmlGenericControl?
Front :
<ul id="test" runat="server">
</ul>
Back:
Label label1=new Label();
label1="test";
test.Controls.Add(new LiteralControl("<li>"));
test.Controls.Add(label1);
test.Controls.Add(new LiteralControl("</li>"))
Try this:
BulletedList UserSubMenuList = new BulletedList();
UserSubMenuList.Items.Add(new ListItem(HttpUtility.HtmlEncode(#"Edit Profile"));
Note that you'll be clearing whatever was initially displayed if you do the first line (the constructor).
The Text property is simply the string of text to put in the <li> tags - if it's text (like all HTML markup), it should be able to go there.