Set attribute with JS and grab it by controlID in code-behind - c#

After postback, attributes are cleared.
So how can I send an attribute applied to controlID via JS to the next request page?
Thanks in advance!

Javascript and ASP.Net controls can have a rather strained relationship. The main thing to remember is that ASP.Net needs to be able to get a value/see a change. Custom attributes may not be kept during a post back.
Have you tried storing the info using a hidden control?
<asp:HiddenField ID="id" Value="value" />
MSDN ref

Related

Showing button on jquery page load event is not working

I am displaying button on jquery page load event but it is not working. After the page is rendered, it is not visible.
It works fine if i set the visibility in code behind Page Load event.
Jquery
function pageLoad() {
$('#btnSwitchDistributor').css({ 'visibility': 'visible', 'display': 'inline-block' });
}
Html
<asp:Button ID="btnSwitchDistributor" runat="server" Text="Switch Distributor" Visible="false" />
According to MSDN, when you set Visible="false" in your server-side code then the element is not rendered to the client at all. Which means your button isn't "invisble" on the resulting page, it doesn't exist on the resulting page. No JavaScript code can interact with an element that isn't present.
It sounds like instead you want to apply a style to the control:
<asp:Button ID="btnSwitchDistributor" runat="server" Text="Switch Distributor" style="display:none;" />
Aside from that, there are two two potential issues here which I can't necessarily confirm from the content of the question, but which you'll want to check...
The ID value you're looking for is the server-side object's ID. This may not necessarily be the client-side id of the resulting HTML element. Examine the page source to see the client-side id. If it's different then you may need to set it explicitly using the ClientID property.
Do you ever call pageLoad() in your JavaScript code? If not then, well, you'd need to do that. I assume in the document.ready event handler? For jQuery that may be as simple as: $(pageLoad)
Just remove the visible html attribute and the JQuery css.

Redirecting to a page hiding query string parameters asp.net

I am working for a project which has web garden scenario and cannot keep any data in session/inmemory.
The asp.net page opens from Appian(bpm tool) and we pass id through query string.
Now, client is asking me to hide the query string parameter after reading it. But, in that case say, landing page is http://a.aspx?id='123' and after reading that value we have to redirect to b.aspx without exposing the id(query string).
Please suggest me a suitable way to achieve this. I am not really getting any idea for this.
you can add key/value pairs to the header, it won't be visible in the querystring.
HttpContext.Current.Response.Headers.Add( key, value);
and
string headerVal = HttpContext.Current.Request.Headers[key];
You can use session variables on the server side or http Post instead of GET.
Session["id"] = id;
And on load of b retrieve it.
To use Post you can use a hidden field and a form.
//You can set it like this
<form name='IdForm' action='b.aspx' method='post'>>
<asp:HiddenField id="WhateverId" runat="server" value='<%= Request.QueryString["whateverID"] %>' />
</form>
On redirect use javascript to post
function Redirect() {
document.forms["IdForm"].submit();
}
You must use this js script wherever the redirection happens.
And finally on b.aspx code behind
HttpContext.Current.Request.Form["WhateverId"]

Pass data to server side from Javascript code

I'm trying to send data from client-side to server-side (asp.net c#), if you really want to know, I want to send the window.name property.
First I thought about having a asp:HiddenField and on the OnSubmit event have some JS write the value in the hidden field. The only problem is that I can access the hidden field value (according to this) only from PreLoad event to PreRenderComplete event. The project that I'm working on has a lot of code in the OnInit event, and unfortunately I cannot move it and I need to use the window.name value here.
The other ideas that I have is to add a custom HTTP Header windowId or on the OnSubmit event have a JS that appends a parameter to the document.location.href.
I managed to write to the header from JS with the XMLHttpRequest setRequestHeader, but, maybe I did something wrong in my implementation, this generates 2 requests, the first one is the normal, expected one(clicking a button/link ...) and the second is from the XMLHttpRequest. I find this behavior very unnatural. Do you have any sugestions? (see code snippet below). I do not what to use AJAX.
var oReq = new window.XMLHttpRequest;
oReq.open('POST', document.location, false);
oReq.setRequestHeader("windowId", window.name);
oReq.send(null);
For the OnSubmit hook idea, i haven't spent to much time on it, but i think I have to append the # character before i append my windowId parameter with it's value, so that the page doesn't reload. I might be wrong about this. Any way, I have to remove this from the URL after I take the value, so that the user doesn't see the nasty URL. Do you have any sugestions?
Ok so what are your ideas?
Thank you for reading all my blabbering, and thank you, in advance, for you answers.
I would recommend the <asp:HiddenField /> (e.g., <asp:HiddenField ID="hfWindowName" runat="server" />. In OnInit you can still access its value by using Request.Form:
string windowName = Request.Form(hfWindowName.UniqueID);

Hidden input types get's created in ASP.net at RUN time

For some reason I have noticed that at run time when looking at my source of my ASP.NET page there are tags being created.
<input type="hidden" name="_VIEWSTATE" id="_viewstate" value="..lots of text.." />
and
<input type="hidden" name="_EVENTVALIDATION" id="_EVENTVALIDATION" value="..lots of text.." />
Why is this and what is it for?
How can I make sure that these are not created?
Thanks in advance!
Chances are that you don't want to get rid of either one of them.
_VIEWSTATE hidden field is used to store the encoded value of ASP.NET WebForms ViewState. If a normal WebForm-style development (as opposed to MVC) you use ViewState all the time when you do things like string someText = TextBox1.Text in your code behind; or when you execute a PostBack and all the textbox, checkbox, dropdown values are preserved without you having to do anything - that's all features of ViewState. It's very convenient and pretty much a standard practice for ASP.NET WebForms. You can disable ViewState per page using EnableViewState property inside the '#Page` directive. I would assume you don't want to do it, though as you will probably notice a lot of things not working all of a sudden.
_EVENTVALIDATION is part of ASP.NET Event Validation scheme - this also can be disabled in the #Page directive (I believe the property is EnableEventValidation) but I can't imagine why you'd want to do it.
The _EVENTVALIDATION control validates postbacks to reduce the risk of unauthorized postback requests and callbacks. You can disable this by setting
<pages enableEventValidation="false">
setting in web.config (or setting
EnableEventValidation="false" in #Page directive) but is not recommended!

Adding Controls using Javascript and get Their Value from codebehind

I wanna add server controls by using javascript. The main purpose of why I want is to add controls without any postback and get them in code-behind.
You can check the Request.Form collection for all form values (client side controls) on the server. Each control will need to have a unique ID to access it in the request.Form collection.
For example, if you had the following control
<input type="text" id="testBox" value="blah" />
On the server you would access the value as Request.Form["testBox"].
Try to access the html inputs with Request.Form
Request.Form["inputName"]
You will have to set the name attribute on your inputs like this:
<input type="text" value="blah" name="inputName" />
if its a form post you can get the value with request.form["control"] one of the properties will help you do it,
if its a new control in some page you can do something with ajax , i did not try it, its just a theory,
you can make an ajax request that will create a textboox control in the server and then render the html to your page.
now when you will call text1.text you will get the value ..
but its a bit of an hack to me..
That is not generally possible. You must add server side controls on the server.
If you want to avoid the visibility of a postback, use AJAX and an UpdatePanel. Otherwise you're out of luck.

Categories