To find Control in Other page - c#

I'm creating a web application using C#.net
My webpage contains a Hidden Field Control. I need to use this control's value in another page. I declared a method and inside this method i passed the entire page as a parameter. In another classfile i defined this method. Inside the method i tried to access the Controls that was in the page created. I tried but i can't get the control.
Code:
HiddenField hdnTotal = page.FindControl("hdnTotal") as HiddenField;
Is anyother option to find this control in other class file.
Thanks In Advance!

Instead of passing a page as parameter did you try using PreviousPage property to find a control? On the page where you want the control from previous page you could find hidden file like this:
HiddenField hf = (HiddenField)PreviousPage.FindControl("hdnTotal");

The normal way to get data from one page to another is to POST the first page to the second page in hidden/form fields.
Is there a reason you're trying to pass the entire control instead of having the control (for example) just set the value of one or more hidden form fields?

You have several options: to use cross-page posting or to pass the value of that hidden field as a GET parameter to another page when redirecting. Also you can save the value in session or in cookie and access it later.

Related

Button on Master page when clicked get values from controls on content page

I have a button on a Master page and when it is clicked it calls a method on the master page which requires several parameters. The values for the parameters are stored in text boxes and drop down lists on both the master page and the content page. I can get the values from the controls on the master page, but I am not sure how to retrieve the values from the controls on the content page.
I have spent the last day looking at various answers on here and on other sites, but have nor found a solution that works for me.
I have also seen answers that suggest putting the buttons on the control page, but that would mean duplicating code on at least 12 pages and I really dont want to have to go down that route unless I have to.
I forgot to say that I am already using the Master reference tag in the content page.
EDIT
If I create a class and set values in the content page, what would be the best way to retrieve the values in the master page. Assuming that would be possible?
I suggest you add a public method like SetYourDropdownValue(string val) to your master page.
In the load-eventhandler from the control page you hand over the selected value from the dropdown to your master page.
MyMainPage m = (MyMainPage)Master;
m.SetYourDropdownValue("your value");
There is an alternative way tho: Master Pages Reference.
You can set following reference tag in your control page markup:
<%# MasterType virtualPath="~/MasterPage.master"%>
Then it will be even easier:
Master.SetYourDropdownValue("your value");
In the SetYourDropdownValue-Method you save the value to a variable.
On the click eventhandler in your master page, you can then retreive the value from your variable.
Instead of passing a string you could also pass one or more controls, so you would just have to save the reference to it, instead of saving a value.

Accessing properties of server controls of one pages using another in asp.net

I am creating an web application using asp.net I'd like how to alter properties of server controls of one page using another Eg: my Default1.aspx page has a textbox with id TextBox1 and its visibility=true, and my Default2.aspx consists of a button Button1. Now i need to set the visibility of TextBox1 to false on the click of Button1.
You cannot (and if you find some black magic way of doing it, SHOULD not) instantiate your pages from other pages/classes.
One thing you can do is to send a variable via HTTP (as a GET or POST variable) that you can access via the Request property in your codebehind. Use Request.QueryString[string] for GET variables and Request.Form[string] for POST variables.

How to Find UserControls Control value in Jquery Asp.net?

I am using jQuery and ASP.net
I have a User Control and wanted to Set the values of this User control to the database, for that I need the user control's value. In the same way, I want to show the data from the database in the user control, for that I need to Get the values by jquery.
In my user control I have 4 TextBoxes and 2 Buttons (SET/UPDATE)
AutoCompleteSearch_New is ascx user control
Here is my tried code:
var ID = $('#<%= ((HiddenField)AutoCompleteSearch_New.FindControl("hdnvalue")).ClientID %>').val();
But I dont wan't to use hidden fields.
Can I directly find the control's value without using hidden fields?
It is similar to use value from a webform.
Here is the code for the same.
Var TextBoxValue = $('#YourTextBoxID').val();
Inspect element and get the Textbox ID and replace it with YourTextBoxID.
Or
Var TextBoxValue = $('#<%= YourTextBoxID.ClientID').val();
where YourTextBoxID is your asp:Textbox ID.
When ever you load user control in your aspx page, jquery consider it as a whole page which is combined of user control and rest of the aspx controls exist in the form. So you can directly get textbox value in your jquery
you can do that by adding ClientIDMode="Static" to the Control
and then use
var txtvalue= $("#TextBoxId").val();
Provided storing the values in Session is not an option, other options I see are :
javascript variable declaration in the user control markup
client-side decryption of the view state
custom data attributes on html elements
I'm afraid HiddenField is your best option. That's somewhat the same idea behind ViewState.
If you don't want the client to tamper with the value, you may go for a combination of the value, and a hash of the value concatenated to a secret key ( value+separator+Hash(value+secretkey) )
If you don't want the client to access the value, you may rely on encrypting it in the HiddenField
hey u can directly find control of textbox or hiddenfield without mention its type
var value=$('AutoCompleteSearch_New_hdnvalue').val();
here AutoCompleteSearch_New is the name of user control u load on page and
hdnvalue is the id of hidden field in user control AutoCompleteSearch_New
enjoy.. :-)

How does Viewstate work internally

Scratch this!
I have googled my ass off looking for this.
1. Lets say that i have a webform that has a few textboxes, with ID's textbox1, textbox2, textbox3. They all have viewstate enabled.
I put some values in these textboxes and push a empty postback button and all the values are still there after postback. How can i access them in the viewstate after postback ?
I would think that they were saved under the ID name of the textboxes but i dont get it to work like so.
String s = ViewState["textbox1"].ToString();
I'm trying to get this to work because I want to be able to save the viewstate into the session so i can retrieve the info after i visit another webform.
2. Isn't it right that i can only use the viewstate on the same page that it was made on ?
I could not use the viewstate on default.aspx in editor.aspx ?
3. And one more thing, isnt it right that the viewstate saves how a treeview nodes are expended ? I would like save the state on the treeview between two webforms that use the same masterpage.
EDIT:
Ok, this wasn't clear enough, thats a given.
Basicly i'm trying to understand the viewstate and what i can do with it.
I dont usually use viewstate to store values.
What i'm trying to do, or figure out if its possible with viewstate.
I have a masterpage and on the masterpage is a treeview. I have two pages that i use with the masterpage, Default.aspx and editor.aspx.
I do my navigations and everything in the Default.aspx. When i have expanded the nodes in the treeview and selected one of the treenode, the navigateurl on that treenode send me to editor.aspx?navID=3. The editor.aspx uses the same masterpage and i want that page to show the SAME state on the treeview as the Default.aspx did before i clicked on the node.
Take a look at this article to learn more about viewstate. I found it helpful
Truly understanding viewstate
The reason your code does not work is because ASP.NET uses a different name (I think it prefixes the control name with the form name and the master page name , if there is one). But even if you could pull it using that method, you shouldn't. You should manually add a property yourself to the viewstate. So if your trying to preserve the text in a text box, use the following code:
ViewState["TextBoxText"] = textbox1.ToString();
And to retreive this later, use:
String s = (String)ViewState["TextBoxText"];
To answer your questions:
You are right. The viewstates are sacred to each individual page and cannot be accessed
Treeview will automatically save the expanded nodes. Just make sure you are doing your initialzation to the treeview inside a if (!Page.IsPostBack) block.
The Viewstate collection in System.Web.UI.Control only allows you to access the viewstate bag for that control, not child controls. So basically you can't do what you want to do through ViewState.
You can get the values that a control posted through the Request.Form parameters. For example, if you have a control call textbox1 you could get its posted value through
Request.Form["textbox1"]
Depending on the control you may have to do some processing on the value you get out of there. For a treeview you can get the posted value of its expanded state using
Request.Form[TreeView1.ClientID + "_ExpandState"]
The value is a string with either an e (expanded) or an n (not expanded) for each node. So if the value was "eennene", nodes 1 2 5 and 7 would be expanded while the others would not be

how to get text box value in page init?

I am using asp.net 2.0
I set a hidden text box from javascript and on postback, i want to check the value stored on the text box in page init event. Here is what i tried
string t = Request.Form["currentMode"].ToString();
but i get an error saying " Object reference not set to an instance of an object."
Any idea?asp.
In init the postback values have not been loaded into the controls yet, you could in theory fish the values from Request.Form variables, but they are named as the client ID of the controls. If your controls are in any container like a contentplaceholder or detailsview they will have various guff prepended on the ID. e.g. ctl00$cphContentBody$txtMyTextbox could be the id of a control that has a server side id of txtMyTextbox.
Init is the page life cycle where everything is initialized. You can't thrust that anything is availble. In order to do that you have to use the Page_Load event...
I came across this the other day while trying to build->submit->retrieve values from a dynamically created form (assembled during the Init() event of a postback). As Ben stated, you can use the Request.Form collection to pull out the values of the page's controls manually, using the UniqueID of the desired control (which will contain the "guff" Ben is talking about).
Of course, this is only necessary when trying to discover the value of a page's control before it is populated from viewstate (before LoadComplete). Otherwise, you could simply do something like TextBox1.Text.

Categories