load text into textarea from code behind in asp.net using C# - c#

I've one asp.net page and I want to load text into the textArea control which is in aspx page from into a variable in code behind (C#):
Code behind:
System.Web.UI.HtmlControls.HtmlTextArea Output1 =
(System.Web.UI.HtmlControls.HtmlTextArea)(FindControl("textarea1"));
Output1.Value = Output.ToString();
ASP:
<div style ="width: 78%; float: right; height: 85px; display: block;"
class="message_text_box_left">
<textarea id="textarea1" name="textarea1" cols="30" rows="3"
class="message_text_box" title="Share your Idias here..."
tabindex="1" onkeyup="addrow_fun();"></textarea>
</div>
but it is giving error like
Object reference not set to an instance of an object.

You should add the
runat="server"
attribute to the text area.
Or, preferable you should use the TextBox ASP.NET control and set the TextMode property to TextBoxMode.MultiLine. Example follows:
Code behind:
Output1.Text = Output.ToString();
ASP:
<div style ="width: 78%; float: right; height: 85px; display: block;"
class="message_text_box_left">
<asp:TextBox ID="Output1" Rows="3"
CssClass="message_text_box" ToolTip="Share your ideas here..."
TextMode="MultiLine" />
</div>

Add runat="server" in *.aspx file. Use Innertext property to set the text value.
E.g.
htmlTexarea.InnerHtml = "sample"

Add runat="server" to your control
Check your .designer.cs or codebehind .cs file for textarea/textbox declaration and fix it.
Do not use FindControl function (it is not recursive), get control by ID. textarea1.Value = xxx;

Try casting to HTML generic control and set it's value or change it to use an asp textbox textmode= multiline

If you add the runat="server" attribute you should be able to use the textarea1.innerText directly.

Add runat="server" and get value with InnerText from code behind

Related

How to set anchor for label in web form application in code? c#

I wanted to set anchor for label but in properties of label i could not find anchor field and how can set it by code maybe in style?
I tried to write in page_load label.Anchor but it cant find this class.
.labelName{float: left; text-align: center; margin-left: 810px ; margin-top: 20px;width: 150px; }
</style>
<div class="labelName">
<asp:Label ID="lblGameName" runat="server" Text="Huina Game"> </asp:Label>
</div>
You should use asp:HyperLink that render as anchor or asp:LinkButton if you need to do a postback with server side handler
<asp:HyperLink ID="labelWithLink" runat="server" NavigateUrl="https://stackoverflow.com" Target="_blank"></asp:HyperLink>
<asp:LinkButton ID="labelWithLinkAndPostback" runat="server" OnClick="ServerSideHandler_click" ></asp:LinkButton>

CSS Style not being set Dynamically in ASP.NET Code behind

I'm trying to change the CSS class and attributes of a set of asp.net controls via the code behind using this:
ASP.NET:
<span id="followbtn_mainbtn" runat="server" class="follow-btner" onclick="profile_followers_follow(this)">
<img id="img_followingbtn" runat="server" class="profile-single-profile-follow-btn-img" src="icons/profico/following.png" style="display: none;">
<img id="img_unfollowbtn" runat="server" class="profile-single-profile-unfollow-btn-img" src="icons/profico/unfollow.png" style="display: none;">
<img id="img_followicon" runat="server" class="profile-single-profile-follow-icon" src="icons/profico/followIcon.png">
<span id="span_following_text" runat="server" class="profile-single-profile-follow-btn-following">Follow</span>
<span id="span_unfollow_text" runat="server" class="profile-single-profile-follow-btn-unfollow" style="display: none;">Unfollow</span>
</span>
Code:
followbtn_mainbtn.Attributes.CssStyle.Add("className", "follow-btner-no-hoverer");
span_unfollow_text.InnerText = "Following";
img_followingbtn.Attributes.CssStyle.Add("display", "block");
img_unfollowbtn.Attributes.CssStyle.Add("display", "block");
span_unfollow_text.Attributes.CssStyle.Add("display", "block");
However when I run this, I do not see the desired results. If I hard code the appropriate classes to the controls, they work properly but the code doesn't do it dynamically.
What am I doing wrong?
Are you updating the css during a postback? First try adding the changes to your Page_Load method on the form and that will tell you that your code is working when setting the styles and classes. If the code works then I would make sure you have EnableViewState="false" on your page and/or parent control of the span.
It was actually a simple error. I've used the wrong code to change the css class of the span named "followbtn_mainbtn".
I replaced this
followbtn_mainbtn.Attributes.CssStyle.Add("className", "follow-btner-no-hoverer");
with this
followbtn_mainbtn.Attributes["class"] = "follow-btner-no-hoverer";
Voila! Done deal. Thanks for all your answers and comments though :)
You can use this code to change css in code behind
img_followingbtn.Style.Add("display", "block");

how do i change the width of my textbox in css?

i added a asp.net textbox into my webpage and cover it with css like shown below
<tr>
<td id="policeprofileachievement" colspan="2" align="center">
<b>Achievement :
<asp:TextBox ID="txtAchievement" runat="server" ReadOnly="True" TextMode="MultiLine"></asp:TextBox>
<br />
</td>
</tr>
In my source code i have added a width for the textbox. But in my css, i added this but it didnt resize my textbox width
#policeprofileachievement [type="text"] {
position:absolute;
margin-top:250%;
left:0%;
width:150px;
}
AND/OR
I removed the policeprofileachievement from my CSS and added this ( which is recommended by many )
#txtAchievement{
position:absolute;
margin-top:250%;
left:0%;
width:150px;
}
But there doesn't seem to have any changes on my textbox size either
Is there any other way to resize my textbox size?
Regards
.txtbox
{
position:absolute;
margin-top:250%;
left:0%;
width:150px;
}
#txtAchievement{
position:absolute;
margin-top:250%;
left:0%;
width:150px;
}
Why dont you simply create a css class and add it to your textbox's cssclass property, like so..
.tb_style
{
position:absolute;
margin-top:250%;
left:0%;
width:150px;
}
THEN ADD IT TO YOUR TEXTBOX LIKE SO
<asp:TextBox ID="txtAchievement" runat="server" ReadOnly="True" TextMode="MultiLine" Height="150px" Width="500px" CssClass="tb_style"></asp:TextBox>
This will also allow you to add same style to other TextBox(s) and create a constant look and feel to the webpage with minimal effort
Make sure you dont write some rule inline to your textbox thats also there in the cssclass...cuz your inline rule will override the rule in class.
The CSS selector #policeprofileachievement[type="text"] means "a tag with ID policeprofileachievement and type=text", which selects nothing because the td with id=policeprofileachievement has no type attribute.
You can use #txtAchievement to select the textbox because it has an id (no need to specify [type="text"]).
Alternatively, if for some reason you need to select multiple text input fields under policeprofileachievement, you could do
#policeprofileachievement input[type="text"]
CSS means cascading style sheet. So your css code block renders second but the text box that you have written already has a width component which will be 'cascaded' by the css you have written. Remove the css width property and your code should work.
With your present code try giving width below 150px. That may work.
Try this:
put space between #policeprofileachievement and [type="text"]
means style for inner all [type="text"] in #policeprofileachievement
#policeprofileachievement [type="text"] {
position:absolute;
top:250%;
left:0%;
width:150px;
}
Try this.It should work..For me its working..
.aspx
<asp:TextBox ID="txtAchievement" runat="server" ReadOnly="True" TextMode="MultiLine"> </asp:TextBox>
CSS
<style>
#txtAchievement
{
width:50px;
}
</style>

how to get wrap text using div tag?

I need to get the "wrap text" in div tag, here is what I'm using:
<div style="width:650px;height:200px;overflow-y:scroll;display: inline-block; writing-mode:lr-tb; word-wrap:break-word;white-space:normal;">
<asp:Label ID="bodyLabel" runat="server" Text=""></asp:Label>
</div>
Can any one please suggest me how to implement this.
Thanks in advance.
what about loosing the label tag , and place your text in the div directly :
<div ID="bodyLabel" runat="server" style="width:650px;height:200px;overflow-y:scroll;display: inline-block; writing-mode:lr-tb; word-wrap:break-word;white-space:normal;">
</div>
and the text will wrap by default in the div.
dont you think its easier ?
Have you tried CSS? See this link.

Selecting an element without an Id

I'm trying to select an element from my webpage...
I have inserted a control into my page and i need to set some values an element inside the control at pageload from c# code.. The thing is, as soon as I insert the control into the page... A prefix is appended to the id name... Because of that new name, my css definition won't be appended...
Is there any way to access this element from C# without the need to make it an Id?
Edit: To clarify what Im trying to do here. Im trying to make a generic control, which gets a width and height set to it's parameters. I need to set this manually to the element by adding a style attribute. I can't make the element an id, because this will stop the possibility of making it generic.
This is whats inside of the control... the fact is, I need the imageRotatorDiv to be a class instead of an id. Otherwise i can't use multiple image rotators on one page.
But how can I select a class in a page from c# code? Is it possible?
<div id="imageRotatorDiv" runat="server">
<div class="imageRotator">
<asp:Repeater ID="rprImages" runat="server">
<ItemTemplate>
<asp:Image ID="imgItem" runat="server" />
</ItemTemplate>
</asp:Repeater>
</div>
</div>
you can define your style to a class name instead of id.
<asp:TextBox ID="MyText" CssClass="someclass" runat="server" />
html output
<input type="text" id="Something_MyText" class="someclass" />
css
.someclass { border:solid 1px red; }
In JQuery :
$("div[id$=MyDiv]").addClass("myDiv")
And you just need to define the myDiv CSS class. Or, to modify directly the style :
$("div[id$=MyDiv]").css("font-size", "14px");
JQuery details

Categories