how to get text attribute from a asp:panel - c#

Probably a simple question, but I've been browsing now for 30 mins and STILL cant find a solution!
i have a panel and it has an attribute text="something". but the panel class does not seem to have a getAttribute method... Which personally, I think is STUPID!
Code follows:
foreach (Control c in clientGrid.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.Panel"))
{
/*Something*/ textInsidePanel = ((Panel)c)./*Somthing*/
}
}
Now i've tried AttributeCollection text = ((Panel)c).Attributes;
and
string text = ((Panel)c).Attributes.toString();
and other useless things...
This should be really simple! when i inspect element on chrome, I can see the panel, (well the div) and i can see the text attribute right there. and i can see its value! but i want my c# code to have the value to!!
Please Help!
Alex

if I get you question right - you can use next code
asp part
<asp:Panel runat="server" ID="pnl" Text="hello world"></asp:Panel>
c# part -
string s = pnl.Attributes["Text"];

Have you tried using an accessor?:
string val = YourPanel.Attributes["Text"];
// ^ that's your attribute name
That should get the attribute's value BUT I'm pretty sure what you are doing isn't possible as attribute values are not persisted between postbacks (at least not when set via a client script). To do that you should use hidden inputs or some other form element.

The Panel control itself doesn't have a text property. But if you access the inner text as a LiteralControl it will work:
var panelContent = ((Panel)c).Controls[0] as LiteralControl;
var text = panelContent.Text;

Related

Change text in HTML that I'm grabbing from a text file

What I'm doing is grabbing the HTML code for my header from a text file.
But once a User logs in I want it to say Welcome "Username" at the top, which is a dropdown to account settings, cart, etc...
So since I'm inserting the HTML into a DIV on page load, I don't actually have access to any of the elements inside in C#.
How would I go about doing this? Is there any way to access something like a (p id="name")'s inner text, after its loaded in from the text file?
Would like to do this with C# not JS please.
Edit: I have a work around for now, but I am still interested in better answers.
headerText = headerText.Replace("::Username::", Session["Username"] as string);
Here is my code for grabbing the HTML and pasting it in.
string headerText = File.ReadAllText(Server.MapPath("~/global/header.html"));
string footerText = File.ReadAllText(Server.MapPath("~/global/footer.html"));
headerText = headerText.Replace("::Username::", Session["Username"] as string);
divHeader.InnerHtml = headerText;
divFooter.InnerHtml = footerText;
To be more clear, is there anyway to access something like
<asp:Panel ID="panelAccount" runat="server">
which is stored in another HTML file.
I have done similar work in C# earlier, I use HTMLAgilityPack for doing this kind of works. Later I start using Anglesharp since it has a very good CSS selector based support.
Try anglesharp and you can modify the HTML tag like you do in jQuery.
As much as I liked Adrians response, I found out a better way to do what I needed to do, I could do with a MasterPage. I definitely be using Adrians answer for a load of other things though so it still holds valid.
Then in the C# file associated with the master page you create a property like this.
public Panel PanelAccount
{
get { return panelAccount; }
set { panelAccount = value; }
}
Then from the regular WebForm, you can call "PanelAccount" to access that property.
Here is a tutorial for how to do that.
Thanks for everyones downvotes with no inputs, you guys are stars!

Selenium: Get Element, which is only Text

I'm trying to find a Text via Selenium, which is directly in the HTML.
This can look something like this:
<br>
Uploaded.net
<img class="bbCodeImage LbImage" />
<br>
I found the Image after the Text, but even now, I can't navigate to the text:
I I went to the img-Element, then tried:
var des2 = ele.FindElement(ByProxy.XPath("preceding-sibling::*"));
Interesting enough, this already returns the br-element and not the text, which is right above it. I also tried to brute force it and get all Elements, with this text:
var des2 = thread.FindElements(ByProxy.XPath("descendant::*[contains(text(), \"Uploaded.net\")]")).SelectMany(f => f.FindElements(ByProxy.XPath("descendant::*")));
foreach(var ele in des2)
{
Debug.WriteLine(ele.Text);
}
So I read all Descendants with the mentioned Text and iterate over all of them, but none of them has a Text set.
Am I missing something crucial here?
Selenium doesn't support a text node. You could however get the text with a piece of JavaScript:
string text = (string)((IJavaScriptExecutor)driver).ExecuteScript(
"return arguments[0].previousSibling.textContent.trim();", ele);
I dont think there is any obvious solution to this. Can offer a very very round about solution.
Get the pagesource of the page -- driver.getPageSource();
Split the pagesource by the img tag. Then split the first element of previous split by br tag. The last element of the array should now be the text.
If you have control over development of this, someone should fix the page.

Text property on selenium web element is empty even though I can inspect it with Visual Studio

Without posting pages of C# code and markup, has anyone got a reason why this code
var link = _driver.FindElement(By.Id(field + "Field"));
var id = link.GetAttribute("id");
var text = link.Text;
given this markup
<a id="ForenameField" href="/MyUrl/MyFolder/MyId">3 errors</a>
Assigns an empty string to the text variable, but if I put a breakpoint on the second or third line and inspect the link variable, I can see the inner text of the element against the Text property on the inspector, it reads "3 errors", but the value of text is an empty string. It is not hidden, I can see the text if I add a watch or use quickview, any ideas?
Ok, it's my bad. Using jquery to toggle class on the div that contains the html in the question, meant that although users see the div appearing, the class that hides the div is still in the tag. A bit like this
<div class="hideThis showThis"><!-- my elements /--></div>
This makes it so that Selenium is right not give me a text value. It is strange however that the Visual studio debugger thinks that there should be a value. Visual Studio seems to go with what I can see, but Selenium is more pedantic about the hideThis class being there.
I go with the idea that if you can't see it you can't interact with it, so it is worth looking up the html graph from the element you expect to have a value to see if any class is present which would hide your element.
Feel free to recommend that I delete this rather obvious wisdom.
I know this was posted over a year ago, but I had this exact problem too and came across this thread. I was able to solve it by just waiting for the DOM to load--some elements aren't visible until the DOM is updated. So just putting Thread.Sleep(6000) or whatever after navigating to the page got it to work for me.

reading form value of a control (TextBox) in ASP.Net

I would like to read the Form value of a control (e.g TextBox), i.e 'Request.Form["[Control_Name_Here]"]. The problem with using say TextBox.Text is because if you explicity set it yourself in the Page_Load, there is no way you can get back the 'original value' submitted in the form.
As you know, Asp.Net generates a unique ID/Name for the control. The Request.Form is based on the name attribute of a control. Each webcontrol has a ClientID property, however this does not match the name. The name seems to be almost like the ClientID, having $ instead of _. Is there a way to easily get the value from the form, without resorting to having to replace the _ to a $?
And this should also cater for other naming-conventions, because as from Asp.Net you can also choose to have a control's id statically-generated, rather than dynamically.
It sounds to me like you're not looking for the .ClientID property, but the .UniqueID property of the Control.
See: MSDN
Edit: Also, is there a reason you're always setting the .Text property within the page load? Instead of for example check the Page.IsPostBack property instead and only set the .Text if it's false?
I personally use jquery to find the control and read and set the value
GetFormValue = function (idName) {
var srchP = '[id='+idName+']';
var ctrl = $(srchP);
if (ctrl != null)
return ctrl.val();
return null;
}
so assume th id name in server part is txMytext
and it will be .....$txMytext in client
and by calling GetFormValue('txMytext') in client side you cna get the value of control
dont foget to use jquery library

Dynamically create controls using stringbuilder

i have been trying to create controls dynamically on my web page using the StringBuilder class..and i dont quite seem to get through...
any help would be appreciated.
i am trying to do this...
StringBuilder sbTest = new StringBuilder(string.Empty);
sbTest.Append("<input type=\"text\" id=\"txt1\" runat=\"server\" />");
Response.Write(sbTest.ToString());
The page for sure displays a TextBox on the browser which is easily accessible through JavaScript...but what i want is the control to be available on the Server Side too...so that when the page is posted back to the server i can easliy obtain the value that has been entered by the user into the textbox.
Can any 1 please help me with this....
thank you so much....
Like Torbjörn Hansson says, if you just add a name attribute (and maybe remove runat="server" from your original snippet) you'll be able to access the submitted value but you'll only have a client-side HTML <input /> element.
If you are wanting to dynamically create server-side controls then you'll have to do something like this:
TextBox textbox = new TextBox {
/* take care to create unique ID's if you're adding more than 1 TextBox */
ID = "foo",
Text = "bar"
};
Controls.Add(textbox);
In an answer almost about the something I answered this
You should do the things properly and not trying to reinvent the wheel.
Creating controls Dynamically you can choose 2 ways, the .NET way, or the Javascript way
Both are seen by any of the other, in other words, creating controls using the .NET way, javascript can see and use it and vice versa.
.NET way
in your HTML file add something like
<body>
<form id="form" runat="server">
<asp:PlaceHolder id="ph" runat="server" />
</form>
</body>
in your script part
TextBox txt = new TextBox();
txt.ID = "myTxt";
ph.Controls.Add(txt);
you can easily get that TextBox in javascript using:
var myTxtValue = $("#myText").value();
Javascript Way
var txt = $("<input />", {
id : "myTxt"
});
txt.AppendTo("body");
in .NET you get the value using
string value = Request["myTxt"];
NOTE All javascript lines uses jQuery for simplify results
Provide a name-attribute and access it with:
Request.Form["txt1"]
You can get the value from
Request["txt1"]

Categories

Resources