ASP.NET: Can I read the ViewState to restore a control in the client site? - asp.net

I am curious: is it possible to read the initial state of a DropDownList control using JavaScript?
Let’s say that when the page is loaded in the browser, the dropdown has ten options. Then, using JavaScript I remove all the options.
Can I read the ASP.NET ViewState to get the initial ten options and restore them?

The short answer is yes you can use JavaScript to read the viewstate values as they are stored in a field called __viewstate, which is rendered in the browser as an input field like this:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="..." />
The problem you will run into is the __viewstate value is encrypted.
My suggestion is to use a hidden field to store the values of the dropdown or whatever else you want to store and then access the value like this:
<input type="hidden" id="hiddenField" runat="server" value="" />
Then in your code-behind, since the input has the runat="server" you can set the value to whatever you wish, like this:
hiddenField.Value= ViewState["dropdownvalues"].ToString();
Finally, you can use JavaScript to get the values from the hidden field, like this:
<script type="text/javascript">
function test()
{
var name = document.getElementById('hiddenField').value;
alert(name)
}
</script>

Related

How to stop asp.net from auto generating ids?

I have a asp.net page that has several form elements.
<input type="hidden" id="myID" runat="server"/>
I am running these at the server level because I need to assign values to them.
The Asp.net engine is auto assigning ids and names, overriding my specified ID.
For purposes of my application I have to have specific IDs on these fields. Since this is an older version of .Net <4 I can not use the ClientIDMode=Static value.
Is there anyway that I can force static IDs?
The ability to assign static values is new to .net version 4.0 but you can get the value assigned to controls and pass that to other bits of JavaScript.
Create your own hidden controls with the IDs and Names that you want. Use some simple JavaScript on your form submission to copy values from the server side controls to your controls.
<form id="MyForm">
<asp:TextBox ID=MyTextBox" runat="server" />
<input type="hidden" id="MyFixedID" name="MyFixedName" />
<asp:Button ID="MyButton" runat="server" Text="Go!" />
</form>
function fred() {
$("#<%=MyButton.ClientID %>").Click(function() {
$("#MyFixedID").val($("#<%=MyTextBox.ClientID %>").val());
$("#MyForm").submit()
});
}
I have also used ScriptManager.RegisterStartupScript() or ScriptManager.RegisterClientScriptBlock() to emit JavaScript and pass the server side Control.ClientID as a parameter to a function defined within a <script> block.

How do i use two Images (as buttons) within one form (asp classic)

I have a form within a repeating region that I would like to have two buttons on.
I currently have one button (an image) that submits the form and generates a preview of the record. I would like to add a second button to the same form that performs a different function. I have assigned different names and values to each of the image buttons but how do I retrieve that value and either do a corresponding response.redirect or perform an additional function?
I know in PHP you can use the $_GET["getvar"]; or $_post["getvar"]; but i am using classic asp.
Any help would be greatly appreciated!
example for you
<input type="hidden" name="PageAction" value="" />
<input type="image" name="button1" src="imgage1.jpg" onclick="this.form.PageAction.value='function1';this.form.submit();" />
<input type="image" name="button2" src="imgage2.jpg" onclick="this.form.PageAction.value='function2';this.form.submit();" />
then you can retrive the value of request("PageAction") to seperate diffrent actions,like:
if request("PageAction")="function1" then
call function1()
elseif request("PageAction")="function2" then
call function2()
end if
Request.QueryString("getvar") is the equivalent of $_get and Request.Form("getvar") is the equivalent of $_post.
Where getvar is the name of the button, it will return the value.

Control.UniqueID different after cross-page postback

I have a simple ASP.NET page with a MasterPage. Within the MasterPage, I have two login fields:
<input type="text" runat="server" id="txtUserName"/>
<input type="text" runat="server" id="txtPassword"/>
When the controls are rendered to the page, ASP.NET renders the following:
<input type="text" runat="server" id="ctl00_txtUserName" name="ctl00$txtUserName"/>
<input type="text" runat="server" id="ctl00_txtPassword" name="ctl00$txtPassword"/>
If I understand correctly, the name attribute corresponds to the UniqueID property of a control. However, when I'm debugging Page_Load and attempt to view the UniqueID of these fields, they have different values (ctl0$txtUserName and ctl0$txtPassword respectively)!
Note that this does not seem to be an issue on all pages using this MasterPage. Most of them work correctly and use ctl0$txtUserName and ctl0$txtPassword in both rendering and Page_Load.
Any idea what might cause ASP.NET to render a different UniqueID for a control than it uses in Page_Load?
I'm still not sure what was causing the generated UniqueIDs in the MasterPage to be different in Page_Load than when rendered to the page. However, I was able to get around the issue by storing the UniqueIDs of these fields in hidden fields. I was then able to access the values directly in the Request.Form collection.
In other words, I did this:
In the MasterPage -
<input type="text" runat="server" id="txtUserName"/>
<input type="text" runat="server" id="txtPassword"/>
<input type="hidden" id="txtUserNameUID" value="<%=txtUserName.UniqueID%>"/>
<input type="hidden" id="txtPasswordUID" value="<%=txtPassword.UniqueID%>"/>
During Page_Load of the child page -
string username = Request.Form[Request.Form["txtUserNameUID"]];
string password = Request.Form[Request.Form["txtPasswordUID"]];
Hope this helps anyone else struggling with UniqueID weirdness in ASP.NET!
Weird quirk I just became aware of: any wrapping controls that are runat server must also have IDs. For instance, if you have a panel around the control, i.e. whatever "ctl00" is, it must be assigned an ID. If it is not set, it will be allocated one and this can change.

What happens to an "input" on "submit"? And how can that be done by ASP.Net? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Read Post Data submitted to ASP.Net Form
I have a google checkout "buy now" button, and am trying to add dynamically created content to send when it's clicked. Using the original html is proving a bit difficult so I want to create an ASP.Net ImageButton Instead of that.
I've succeeded in creating the button with the right image, and hooking it up to an event handler in the codebehind.
However, I'm not sure what exactly happens when the original button is clicked, in order to try and emulate it in the new ImageButton.
The original code is:
<form action="https://sandbox.google.com/checkout/..." id="Form1" method="post" name="..." target="_top">
<input name="item_name_1" type="hidden" value="..." />
...
<input alt="" src="https://sandbox.google.com/checkout/buttons/buy.gif?merchant_id=..." type="image" />
</form>
And I want to place a dynamically created item_name_1.
What do I have to do in the Button1_Click method for that?
The short, concise and usefull version:
Html:
<form id="__parent" action="..." method="post" runat="server">
<input id="__child0" name="type" type="hidden" value="button" runat="server" />
<input id="__child1" name="name" type="hidden" value="teh_button" runat="server" />
<input id="__child2" name="value" type="hidden" value="Hello?" runat="server" />
</form>
tehfile.cs:
<%# Page Language="C#" CodeFile="tehfile.cs" %>
String
_type = __child0.Value,
_name = __child1.Value,
_value = __child2.Value,
_element = String.Format(
"<{0} {1}=\"{2}\" {3}=\"{4}\" {5}=\"{6}\" />",
"input",
"type", _type,
"name", _name,
"value", _value );
Literal _lit = new Literal( );
_lit.Text = _element;
__parent.AddControl( _lit );
To post that data to another server on the ASP.NET server-side, you are going to need to use something like the WebRequest class.
Or also in order to post a form, you can use a remote post class like any of the ones here: Remote HTTP Post with C# , the answer by #BobbyShaftoe is the one i've used in many projects.
Same question/POST here. I would have commented instead of answered, but seems this is a better/formatted way to get to bottom of it all:
In your comment to #MarcusHansson's answer:
I fail to see how this addresses the question of how to use the codebehind to send the information
You are mixing server side with client side submission methods.
If you want to submit using "code behind" you must implement server-to-server HTTP Post. In the context of Google Checkout, I've provided that link in your other post.
Your client side is using an HTML FORM which in, and of itself is how you "send the data". You can try all sorts of client-side submission processes, but at the end of the day, it is a client-side (Javascript) method.
What is "dynamic" about your buy now button? It's meant for a single item (at a time) purchase. Why can't you construct all the variables you need at the same time you create the button? What are you adding (that requires another redirect or postback)?

ASP.Net using multiple form tag and post them to another page

I don't know it's even possible but I try to achieve to post data from one page to another using the second form.
The problem is I need a form tag for the user interface containing callback panels etc. I want to put a second form with some hidden-field:
<form id="postForm" method="post" action="target.aspx">
<input type="hidden" id="id" />
</form>
I submit the form from javascript with jquery:
$("#id").val(id);
$("#postForm").submit();
Is there a way access the value of the hidden field on the target page?
You should give the hidden field a name attribute:
<input type="hidden" id="id" name="id" />
You'll then be able to use Request.Form in your target page to access the posted data:
string postedId = Request.Form["id"];

Resources