How to access TextArea from code-behind from a MasterPage - c#

I have a TextArea in my Content page:
<textarea id="taskNotes" runat="server" class="taskNotes"></textarea>
Also a Label in my Content page:
<asp:Label runat="server" ClientIDMode="Static" ID="lblStartDate" Text="TEST"></asp:Label>
In my MasterPage I can access the label like this:
lblStartDate = (System.Web.UI.WebControls.Label)ContentMain.FindControl("lblStartDate");
How can I access the TextArea the same way I am accessing the Label?

var txtTaskNotes = (System.Web.UI.HtmlControls.HtmlTextArea)ContentMain.FindControl("taskNotes");
That should do it. But it's probably wiser to populate protected properties in the master page from the content page. That way you're not tying the master page to a single content page layout.

In ASP.Net, you want to use TextBox with TextMode="MultiLine". It'll render as TextArea.
<asp:TextBox id="TaskNotesTextBox" TextMode="MultiLine"
Columns="10" Rows="5" runat="server" />
Then you can access it like you did your label -
var taskNotesTextBox = (TextBox)ContentMain.FindControl("TaskNotesTextBox");

Related

Added tag HyperLink in aspx page without GridView in c#

On MarkUp in my aspx page form I have these two TextBox :
<asp:TextBox ID="Mtl" runat="server" ReadOnly="true" Enabled="false"></asp:TextBox>
<asp:TextBox ID="ps" runat="server"></asp:TextBox>
The HTML view for these two TextBox is :
<input name="Mtl" type="text" value="901" readonly="readonly" id="Mtl" disabled="disabled" />
<input name="ps" type="text" id="ps" />
Now I need insert next to the TextBox with id ps the HyperLink where passed in querystring the value of TextBox with id Mtl, the value is 901.
I need pass this value for working in another aspx page.
I have tried this solution but the HyperLink is not clikable :
<asp:HyperLink ID="HlLink" runat="server"
NavigateUrl='<%# String.Format("~/box.aspx?v={0}&e={1}&l={2}", "y", "IC", HttpUtility.UrlEncode(Eval("Mtl").ToString())) %>'
ImageUrl="~/Images/edit_icon.gif" Target="_blank" Text="Mtl"></asp:HyperLink>
In this aspx page I don't have GridView, maybe it does not work for this reason ?
How to do resolve this ?
Please help me, thank you so much in advance.
Yes you are correct since your control is not inside a gridview (or any databoundcontrol for that matter) that's why it will not work.
Actually, <%# %> is called data bind expressions and they are evaluated for data bound controls only. For your HyperLink control to work with this code nugget you will have to explicitly call the DataBind method on that control like this:-
protected void Page_Load(object sender, EventArgs e)
{
HlLink.DataBind();
}
You can bind a jQuery 'Change' event on your textbox. And in this event you can set correct navigation url by picking up the value from the textbox and appending it in appropriate place in your query string. Its a fairly easy solution. If you don't know how to do it I can provide you a sample.

How ASP.NET will render Hidden field and a textbox with visibility property as false?

<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:TextBox ID="Textbox1" runat="server" Visible = "false"></asp:TextBox>
How ASP.Net will render asp:HiddenField and asp:TextBox with visibility property as false?
In context of you html
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:TextBox ID="Textbox1" runat="server" Visible = "false"></asp:TextBox>
text-box will not be render on the browser.
But the hidden field will be rendered.
The control visibility which you set from the server side code will not be render on the browser. You you want to use the use css display:none
It will render both the same as
<input id="_controlId" type="hidden" value="" name="_controlId">
You can open your page in browser and look at page source.
If you want server not to render your controls at all put them into PlaceHolder and set property Visible = "false"
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible = "false">
<asp:TextBox ID="Textbox1" runat="server" ></asp:TextBox>
</asp:PlaceHolder>
You can find this out yourself. Run your .aspx page and click View Source on the page.
The hidden field will be included in the markup, but you won't be able to visually see it on the page.
The TextBox is set as invisible from the server side. It won't turn up at all unless you change the Visible property to true.
The term "render" refers to ASP.NET's act of creating the HTML for the .aspx Page,When you set Visible = false to a control, it is not rendered. That means there is no HTML representation of that control sent to the page. Set the style only.
You can set the style as display: none from server side code like this:
FromDate.Style.Add(HtmlTextWriterStyle.Display, "none")
This way the element is still there for JavaScript to manipulate.

get asp:textbox value in client side using javascript function doesn't work

This code doesn't display the value, I don't know why?
I have server control:
<asp:TextBox ID="txtTest" runat="server" Visible="false" TextMode="MultiLine"
Rows="3" Columns="23" CssClass="white-scroll" />
in javascript function:
var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);
I enter text then click on button that call the javascript function, but the alert box doesn't display the entered text.
EDIT: when I initialize the text with Text="some text", it is displayed in alert, I want to enter text in client side and get the value of it in the Javascript function.
Thanks
Using label or textbox visible set false so it can access the value in JavaScript
Sol
1)
Make a div and set style="display:none;" so label is not display at UI(browser) but value can access in JavaScript.
This is because you server Control is called "txtTest" not "txtEventDescription"
change your javascript function to :
var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);
EDIT: ok, I see you've now changed the post to show the code and renamed the js control, so above is no longer relevant (for those who are confused by my answer) :-)
The problem is the Visible="false" - this control will not render into the client and will therefore not be accessible via javascript (as the HTML element does not exist client side)
So, hide the element using CSS and then call alert on it. Sample snippet
CSS
.hide-element {
display: none;
}
HTML Markup
<asp:TextBox ID="txtTest" runat="server"
Columns="23"
CssClass="white-scroll hide-element"
Rows="3"
TextMode="MultiLine"/>
JavaScript
var eventText = document.getElementById('<%=txtTest.ClientID%>').value;
alert (eventText);
This way you will definitely get an alert.
You alert is empty because you have not set the property Text for your asp:Textbox
Make it Visible="true" to your textbox and than test.
<asp:TextBox ID="txtTest" runat="server" TextMode="MultiLine"
Rows="3" Columns="23" CssClass="white-scroll" />
if "txtTest" textbox has visible="false" in that case its not render on html code on client machine and if it hasn't on client's html code then how javascript calls this textbox. Because when javascript search this textbox by its id it doesn't find and it gives an error.
you can assign any other custom attribute to the control i.e.
<asp:TextBox ID="txtTest" runat="server" Visible="false" TextMode="MultiLine"
Rows="3" Columns="23" CssClass="white-scroll"
clientID="myClientID" />
and then access control using jquery like
var txtVal = $('textbox[clientID=myClientID]').val();
hope it helps.

How to print a LinkButton to page?

Say I have a dynamically generated LinkButton in my ascx.cs code-behind. How could I go about "printing" this control to my page? Obviously I can't do something like just print the Text property as I need the button to retain its hyperlink. I'm guessing I want to use the WebControl.Render method, but I'm not familiar with it at all and haven't been able to find a good example of its use.
This article http://www.tomot.de/en-us/article/3/asp.net/create-a-control-in-the-codebehind-and-retrieve-its-rendered-output should explain the basics of what you are looking for.
You could provide a label at the right location in the page: <asp:Label id="myLinkButtonPlace" runat="server"></asp:Label>, and in code, you could add the linkbutton to the controlcollection of the label: this.myLinkButtonPlace.Controls.Add(aLinkButton);
Following on from #Joachim VR, there are many other asp.net controls with which you can add a dynamically created control to.
<asp:Label id="Label1" runat="server" />
<asp:PlaceHolder id="Placeholder1" runat="server" />
<asp:Panel id="Panel1" runat="server" />
The above would render HTML different.
So the Label would render as a <span id="Label1"><a></a></span>
Panel as <div id="Placeholder1"><a></a></div>
The Placeholder would simply render as the <a></a>

ASP.NET Master page DefaultButton override

I have a master page with a form element and the defaultbutton attribute set to a server-side ImageButton. On one of my pages I want to "override" the masterpage defaultbutton attribute by setting the Forms DefaultButton in the Page_Load event.
i.e
On mater page:
<form id="form1" runat="server" defaultbutton="btnSearch">....</from>
On the page Page_Load event that "override" the master page attribute:
this.Form.DefaultButton = this.ibRecalc.ID;
It errors with :
The DefaultButton of 'form1' must be the ID of a control of type IButtonControl
I am using image buttons which implements IButtonControl
Any ideas of what I might be doing wrong or a different way to approach the problem?
Thanks
Use UniqueId. Since you can have multiple server controls with the same server id, ie, in a GridView, the framework needs to the unique id to match up to.
this.Form.DefaultButton = this.ibRecalc.UniqueID;
You could try using the "DefaultButton" property of a Panel...
Place your button or whole page or div in asp:Panel
// start panel
asp:Panel ID="pnlOpsCallSummay" runat="server" DefaultButton="btnSearch"
............
//Controls of your requirement
..........
asp:Button ID="btnSearch" runat="server" Text="Search"
close the pannel
No need of Overriding the master page button
If you move the panel inside the login's template:-
<asp:login id="Login2" runat="server" loginbuttontype="Image">
<layouttemplate>
<asp:`enter code here`panel id="Panel1" runat="\
server"defaultbutton="LoginImageButton">
</asp:Panel>
</LayoutTemplate>
</asp:Login>
Then it will work without code.
You can set loginbuttontype="Image" or Link or button according to your requirement.

Categories