I have a MasterPage that contains a hidden field control. I want to get the current value of the hidden field and set the value of it from pages that use the MasterPage.
I have the following code so far: (in one of the pages)
//Get the textbox and set it's value
TextBox txt1 = new TextBox();
txt1 = (TextBox)this.Master.FindControl("txtHiddenField");
txt1 .Text = "true";
The above code does not seem to work. What code would I need to get the hidden field control and set it's value? (and get it's value)
I would recommend to provide a public property/method in your MasterPage, that you can use to set/get the HiddenField's value.
in your Master(assuming it's type is called SiteMaster):
public String HiddenValue {
get{return txtHiddenField.Value;}
set{txtHiddenField.Value = value;}
}
In your page:
SiteMaster master = (SiteMaster)Page.Master;
master.HiddenValue = "true";
This approach is straight-forward, less prone to errors and easily readable. You could even change the control in your master without needing to change the pages(f.e. if you want to replace the hidden-field with a TextBox).
Assuming that your "true" value indicates that you actually want to store a boolean, i would recommend to use a bool as data-type for the property and a self-explanatory name. Then you can store it in the hiddenfield but the client(the page) does not need to know.
HiddenField sets its text as VALUE, while TextBox has a TEXT property. Of course casting one to the other and setting text property won't help.
Do this instead:
HiddenField hiddenField = (HiddenField)Master.FindControl("txtHiddenField");
hiddenField.Value = "true";
Assuming you have added hidden field control like this ->>
<input type="hidden" ID="hiddenFieldID" runat="server" />
you can access it like -->>
HtmlInputHidden hiddenfield = (HtmlInputHidden)this.Master.FindControl(
May be you are missing ContentPlaceHolder
Try something like this
ContentPlaceHolder mpContentPlaceHolder;
TextBox mpTextBox;
mpContentPlaceHolder =
(ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
mpTextBox =
(TextBox) mpContentPlaceHolder.FindControl("TextBox1");
if(mpTextBox != null)
{
mpTextBox.Text = "TextBox found!";
}
}
Read more about Reference ASP.NET Master Page Content
Related
I'm create a web custom control with hidden field:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
hidden = new HtmlInputHidden();
hidden.ClientIDMode = System.Web.UI.ClientIDMode.Static;
hidden.ID = this.ID + "_hidden";
this.Controls.Add(hidden);
}
I change it value in javascript on the page:
$(textbox).text("some text");
Then try to get this value:
string str = Request.Form[hidden.Name];
I get a null.... Also I tryed:
string str = Request.Form[hidden.ClientID]
and still get null.
Thanks.
So when accessing it from JavaScript you need to use this syntax:
$('#field_hidden').val("some text");
further, with the edit, I just noticed that you're not giving it a name as karaxuna stated. And finally, you'd need to make sure it's inside the form tag for it to be part of the Request.Form.
One other way of accessing its value though, even if it's built dynamically inside the Load, is to grab its value with the Value property. However, make sure you do that after the Load, in something like PreRender, because ASP.NET needs to have a chance to bind its value with ViewState.
Give it a name if you want to get it with Request.Form
I have an input field that is appended to the HTML in a div.
I need to have this input field bound to the ctrlmenuItems in the system but I don't know how.
I have this function which is the one calling the object:
protected void btnSave_Click(object sender, EventArgs e){
int Id = int.Parse(Request.QueryString["edit"]);
DA.page itmToEdit = DA.page.StaticGetById(Id);
itmToEdit.title = ctrltitle.Text;
itmToEdit.menuItems = ctrlmenuItems.Text;
itmToEdit.content = ctrlcontent.Text;
itmToEdit.Update();
UpdateGUI();
pnlEditArea.Visible = false;
}
I tried using:
protected global::System.Web.UI.WebControls.TextBox ctrlmenuItems;
which just creates a null reference.
I tried calling this onInit():
TextBox ctrlmenuItems = new TextBox();
ctrlmenuItems.ID = "ctrlmenuItems";
ctrlmenuItems.Text = "Enter your name";
this.form1.Controls.Add(ctrlmenuItems);
with no luck.
The appending is done like this:
litMenu.Text += string.Format(#"<div>
<a class='button' href='?del={1}'>Slet</a>
<input name='ctrlmenuItems' type='text' id='ctrlmenuItems' value'{0}' />
</div>",
itm.menuItems,
itm.id);
I need to be able to click "save" and it passes on the value from the textField to dataAccess
but I can't figure out how to bind this "virtual" textbox correctly.
input tags with a name attribute will be stored in the Request.Form array when the page posts back.
Whether you add the TextBox control manually, or simply insert the corresponding HTML (which I would not recommend, building HTML via string concatenation makes it very easy to yield invalid HTML), you can retrieve the value when your button is clicked using the following:
string value = Request.Form["ctrlmenuItems"];
I am storing name and last name in two labels in main page. I also have those values in a class (class doesnt do much but i am using them for future expansion). I have a user control that will send an email with name and last name as body.
My question is that how can I transfer label or class variable values into user control's body variable?
Create a property on your user control with the datatype of the data you want to pass to it, and populate it in your page on creation of the control.
public class myUserControl : Control
{
...
public int myIntProperty {get; set;}
...
}
Later this in the code behind you can assign the value like
myUserControl cntrl = new myUserControl();
cntrl.myIntProperty = 5;
Instead of this you can pass the value through Markup also like
<uc1:myUserControl ID="uc1" runat="server" myIntProperty="5" />
You need to create properties on your control to hold these values; then from the page code, simply assign the values to the properties in the control.
On your control, you can have something like
public string FirstName
{
get {
if (ViewState["FirstName"] == null)
return string.Empty;
return ViewState["FirstName"].ToString();
}
set {
ViewState["FirstName"] = value;
}
}
You need to define public properties on the control and then when you use control on the page you can pass values to those parameters.
Something like:
<cc:mycustomControl runat="server"
MyProperty1=<%# label1 %>
MyProperty2=<%# label2 %>
/>
Step 1:
You can expost the values as property and than you can make use of that easily.
Step 2: To access your page from the user control you can make use of Parent property or may be some custome login to access the parent page and than write code to consume the property value.
you can do something like this in your user control
string x=((yourparentcontrol)this.parent).label1.text;
and use the string x.
I am using this javascript function to show different popups if location count varies. Here the txthiddenloccount value is null if the txtbox's visibility is false. If the visibility is true, it works fine. What strange is this??? Can someone help me out.
function isPageValid()
{
var validated = Page_ClientValidate('groupProfile');
var loccount = document.getElementById("ctl00_ContentPlaceHolder1_txthiddenloccount").value;
if(validated)
{
if(loccount == '1')
{
var mdlPopup = $find('<%= ModalPopupExtendersavechanges.ClientID %>');
if(mdlPopup)
{
mdlPopup.show();
}
}
else
{
var mdlPopup = $find('<%= ModalPopupExtenderMerchantUpdate.ClientID %>');
if(mdlPopup)
{
mdlPopup.show();
}
}
}
}
if txthiddenloccount is an asp:TextBox that has the Visible property set to false then it does not exist on the page that is readable by javascript. It will be stored in the ViewState.
For something like this you're probably better off using an asp:HiddenField and setting the value, that will create an input type='hidden' that will be accessible through javascript.
Here you are trying to get txthiddenloccount control's value which hasn't rendered on the page because its visibility is false.
so first you have to check if it is null i.e you can write code like this.
var loccount='';
if(document.getElementById("ctl00_ContentPlaceHolder1_txthiddenloccount") != null)
{
loccount = document.getElementById("ctl00_ContentPlaceHolder1_txthiddenloccount").value;
}
If the Visible property of the control is set as false via ASP.NET, it will be part of the control tree but will never actually get rendered to the page. If it doesn't get rendered to the page, JavaScript can't access it.
If you want to hide it using ASP.NET, you could do it this way in C#...
txthiddenloccount.Style.Add("display", "none");
That will not prevent the control from rendering on the page AND it will use CSS to hide it. Alternatively, you could do this, but it might not be what you want, visually...
txthiddenloccount.Style.Add("visibility", "hidden");
Hope that helps.
i have a custom control that renders a number of literalcontrols in the createchildcontrols method. as below (there are other lines of literalcontrols being added that i have not uncluded here)
this.Controls.Add(new LiteralControl(string.Format("<input type=\"text\" style=\"display:none\" value=\"{0}\" id=\"{1}\" name=\"{1}\" />", MediaId.ToString(), this.UniqueID)));
i am then trying to validate this textbox via adding the [ValidationProperty("myprop")] at the top of the class.
basically i need to validate against the value entered into the textbox. the myprop property is as follows
public string myprop
{
get
{
Control ctrl = this.FindControl(this.UniqueID);
string txt = ((TextBox)ctrl).Text;
try
{
MediaId = Convert.ToInt32(txt);
}
catch { MediaId = 0; }
return txt;
}
}
unfortunately the findcontrol does not find the textbox at all, i presume because as far as .net is concerned its a literalcontrol, not a textbox at all
now for sure i could change the createchildcontrols to do this
TextBox tb = new TextBox();
tb.Text = this.MediaId.ToString();
tb.ID = this.UniqueID;
this.Controls.Add(tb);
but because of other limitations of what i am doing i would have to change a great deal more stuff in other places..
is there anyway of getting findcontrol to find the textbox rendered inside the literal, or another method?
thanks
nat
unfortunately the findcontrol does not
find the textbox at all, i presume
because as far as .net is concerned
its a literalcontrol, not a textbox at
all
That is correct. You have to use some other control like a textbox.