Reference items within placeholder control - c#

Trying to reference the following text nested within a placeholder control. I'm able to get the "text" textbox but the IF(childCnt is textbox) condition fails on the "title". I'm guessing it's due to the first textbox being nested within a DIV but now sure how to resolve. (See screenshot)
foreach (Control cnt in phInitiatives.Controls)
{
foreach(Control childCnt in cnt.Controls)
{
if (childCnt is TextBox)
{
string controlID = childCnt.ClientID.Substring(3);
TextBox tb = (TextBox)childCnt;
//string title = tb2.Text;
string text = tb.Text;
//updateInitiatives(controlID, title, text);
}
}
}
-- UPDATE --
Not sure if this will help? Here is the output HTML. I'm able to get textarea (txt1) but not the input(lbl1)
<div class="initiative">
<div>
<input name="lbl1" type="text" value="Try" id="lbl1" class="labelCSS">
<a onclick="javascript:return confirm('Confirm Delete?');" id="btn1" href="javascript:__doPostBack('btn1','')"><i class="fa fa-times"></i></a>
</div>
<textarea name="txt1" rows="3" cols="20" id="txt1">Test text **</textarea>
<br><br>

Was able to fixed by just removing the nested div.

Related

Selenium webdriver get value from data-bind on span tag

I need to get the value from this tag
<span>
<a class="jsk-sa-dialog link-lightbox-valores" data-bind="attr: { href: '#sa-valor-' + $root.types().id }" href="#sa-valor-2">
Link to click that pop-ups a new window
</a>
</span>
After look at the "html" code in debugger, I saw the text that I want to get into HTML...
To get the text that is generated into the popup window, I wrote this code:
case "someBaseText":
{
details.Click(); // Simulate the click on <a> tag
var tx = details.FindElement(By.XPath("//div[#class='overview-material']"));
var dv = tx.FindElement(By.XPath("//div[#class='sa-valor']/h3"));
var ttText = dv.Text; // Empty :(
}
The HTML that i got from debugger:
<div class="overview-material">
<div class="valores" data-bind="foreach: $root.types">
<div class="sa-valor" data-bind="attr: { id: 'sa-valor-' + id }" id="sa-valor-2">
<h3 class="titulo">Mensalidade</h3>
<div class="texto" data-bind="html: apresentacao.valor">
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
So, I want to get the text inside of div class "texto"
Based on the XPath of dv, you are getting the <h3> element:
<h3 class="titulo">Mensalidade</h3>
therefore dv.Text should return "Mensalidade".
Try updating your XPath to the <div> with class='texto':
//div[#class='sa-valor']/div[#class='texto']
Try CssSelector:
var text = driver.FindElement(By.CssSelector("div.texto")).Text;

How to create and insert HTML at run time?

Would someone give a code example how to create and insert HTML at run time?
The HTML is like this:
<div class="row">
<div class="col-md-3">
<img src="example.png" class="profileImage" /> <br />
<span class="name">Name</span>
<div class="ver"></div>
<img class="flag ver" src="star.png" />
<div class="horizontalBar"></div>
</div>
</div>
The close I get was:
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Header.DataBind();
ContentPlaceHolder contents = Page.Master.FindControl("MainContent") as ContentPlaceHolder;
Panel row = new Panel() { CssClass = "row" };
Panel col = new Panel() { CssClass = "col-md-3" };
Image profile = new Image()
{
CssClass = "profileImage",
ImageUrl = "example.jpg"
};
row.Controls.Add(col);
col.Controls.Add(profile);
contents.Controls.Add(row);
}
}
It doesn't work (see below error) and isn't full code, for example, what class is equivalent to generate <span>?
I get this error:
The Controls collection cannot be modified because the control
contains code blocks
What's the reason of that error? which are those code blocks and how do I fix this?
I've tested the code here and it's working.
The control equivalent to span is Label, but I think there must be better ways of doing this.
If you really need to dynamically insert HTML code, you can inject it using the LiteralControl, like this:
var html = new LiteralControl(#"<div class=""row"">
<div class=""col-md-3"">
<img src=""example.png"" class=""profileImage"" />
<br />
<span class=""name"">Name</span>
<div class=""ver""></div>
<img class=""flag ver"" src=""star.png"" />
<div class=""horizontalBar""></div>
</div>
</div>");
contents.Controls.Add(html);

Prevent <span> tags ASP.NET server control

I have an issue with this span tags. they are generated but i dont want them. how can i remove them ?
I have the following code
<asp:RadioButtonList ID="rbl_items" runat="server" RepeatLayout="Flow" Visible="true" ClientIDMode="Static" >
</asp:RadioButtonList>
Renders this
<span>
<input>
<label>
<br>
<input>
<label>
<br>
(...)
< /span>
And i wanted something like
< label>< input (radio)>< /label>
< label>< input (radio)>< /label>
In other words how can i remove the < span> tags and if possible structure the render html to be like the client wants(as i showed).
Thanks
One approach to get rid of span is to follow the technique mentioned Here
Another would be, to use the input control of html and place runat="server" and ID attribute and use in code behind.
Another would be to use something like this:
<ul ID="rbl_items" runat="server" clientidmode="Static" style="list-style: none;"></ul>
Code Behind:
int count = 0;
foreach (var yourObj in YourList)
{
HtmlGenericControl li = new HtmlGenericControl("li");
HtmlInputRadioButton radioButton = new HtmlInputRadioButton
{
Value = yourObj,
Name = "controlGroup",
ID = "controlID" + count
};
li.Controls.Add(radioButton);
Label label = new Label { Text = yourObj, CssClass = "YourCSSClass", AssociatedControlID = "controlID"+count };
li.Controls.Add(label);
rbl_items.Controls.Add(li);
count++;
}
You can set style for ul and li later.

How to get the "ID" of element by the name with asp.net

I have a generic code that create a loop on the post values. and i need to get the id of the element that posted the data.
For Example, if the Html source is:
<form id="form1" runat="server">
<input id="Name" type="text" name="Full Name" runat="server" />
<input id="Email" type="text" name="Email Address" runat="server" />
<input id="Phone" type="text" name="Phone Number" runat="server" />
</form>
C# code:
for (int i = 1; i < Request.Form.Count; ++i)
{
string Value = Request.Form[i];
if (Value != "")
{
string ControlName = Request.Form.Keys[i];
string ControlId = ""; // Here I need to find the Id
}
}
If the ControlName is: "Full Name", In ControlId I need to get: "Name" etc.
Any advice is welcome.
Request.Form just returns NameValueCollection.
If you want to retrieve controls inside the form as Server controls, you need to use Page.Form.
foreach (var control in Page.Form.Controls)
{
if (control is HtmlInputControl)
{
var htmlInputControl = control as HtmlInputControl;
string controlName = htmlInputControl.Name;
string controlId = htmlInputControl.ID;
}
}

Wrap automatically inserted TextBoxes with HTML in ASP.NET

I want to generate TextBoxes in my ASP.NET webpage. It works fine
foreach (var Field in db.A_Settings)
{
TextBox t = new TextBox();
t.ID = Field.ID.ToString();
t.CssClass = "smallinput";
t.Text = Field.Text;
LabelPlaceHolder.Controls.Add(t);
}
And it nicely generates something like this:
<input name="ctl00$ContentPlaceHolder1$1" type="text" value="ValueA" id="ContentPlaceHolder1_1" class="smallinput">
<input name="ctl00$ContentPlaceHolder1$2" type="text" value="ValueB" id="ContentPlaceHolder1_4" class="smallinput">
<input name="ctl00$ContentPlaceHolder1$3" type="text" value="ValueC" id="ContentPlaceHolder1_5" class="smallinput">
It is correct, but in fact I want to wrap it with some HTML, like
<p>
<label>Label for the first TextBox obtained from database</label>
<span class="field">
<input name="ctl00$ContentPlaceHolder1$1" type="text" value="ValueA" id="ContentPlaceHolder1_1" class="smallinput">
</span>
</p>
I couldn't found how to do it this way, so I was thinking about putting it into List<TextBox>, but I'm stuck here either (the same problem - no idea how to wrap the object with HTML).
Is there any way to do this?
For any posts like "Why don't you add those TextBoxes manually?" I'll send a photo of me hitting my head at keyboard, while there will be a dump of SQL with dozens of fields that needs to be handled displayed on the screen :)Or a photo of a lemur. Lemurs are okay, too
Not the cleanest solution, but should work...
foreach (var Field in db.A_Settings)
{
TextBox t = new TextBox();
t.ID = Field.ID.ToString();
t.CssClass = "smallinput";
t.Text = Field.Text;
//add literal control containing html that should appear before textbox
LabelPlaceHolder.Controls.Add(new LiteralControl("html before"));
LabelPlaceHolder.Controls.Add(t);
//add literal control containing html that should appear after textbox
LabelPlaceHolder.Controls.Add(new LiteralControl("html after"));
}
I would probably use a Repeater control:
<asp:Repeater ID="SettingsRepeater" runat="server">
<ItemTemplate>
<p>
<asp:Label ID="ItemLabel" runat="server"></asp:Label>
<span class="field">
<asp:TextBox ID="ItemTextbox" runat="server"></asp:TextBox>
</span>
</p>
</ItemTemplate>
</asp:Repeater>
And bind the list to the repeater:
SettingsRepeater.DataSource = db.A_Settings;
SettingsRepeater.DataBind();
Then write your ItemDataBound code to set the existing values.
You want HtmlGenericControl controls.
foreach (var Field in db.A_Settings)
{
TextBox t = new TextBox();
t.ID = Field.ID.ToString();
t.CssClass = "smallinput";
t.Text = Field.Text;
var label = new HtmlGenericControl("label");
label.Controls.Add(new LiteralControl("LABEL TEXT"));
var p = new HtmlGenericControl("p");
p.Controls.Add(label);
var span = new HtmlGenericControl("span");
span.Attributes.Add("class", "field");
span.Controls.Add(t);
p.Controls.Add(span);
LabelPlaceHolder.Controls.Add(p);
}
You can create custom ASP.NET Controls that can render any HTML you need. Have a look at this:
Developing a Simple ASP.NET Server Control. This way you can create your a control called CustomTextBox which will render a Textbox inside a paragraph.

Categories

Resources