Query string in client side - query-string

I am a beginner in using ASP.NET and JavaScript. I've stumbled upon this problem:
In my page1.aspx, I use Response.Redirect("page1.aspx#download") using an anchor.
Now, I want to get the Request.QueryString upon loading on the client side using JavaScript.
Can someone help me with this?

page1.aspx#download
By the above declaration you will not be able to get this Download as a text on other page. You need to declare it like this
page1.aspx?Value=download
and on page1.aspx server side you need to get the value by calling
lblText.Text = Request.QueryString["Value"];
lblText will be asp:label on client side of this page and you can call it on page load so your value will be diplayed.

Related

Server controls and building HTML - ASP.NET

I render HTML returned from an ajax call and currently uses simple html controls and the string that is returned from the ajax call looks like this:
string s = "<tr><td>MyLink</td></tr>";
Now, on the click on these links I need to do some processing on the server side to determine where I need to navigate to and for other information. My understanding is that I cannot use <asp:linkbutton /> in this html string I am building.
How do I make a server side call if i don't want to use ajax.
You don't necessarily need to use ASP .NET server controls to enact server-side logic in a post-back. The link for which you manually build the HTML and return in the AJAX call can direct the user to an HttpHandler (or even an aspx page) and pass necessary values to that handler/page on the query string. Then that handler/page can perform its server-side logic and redirect as necessary.
Maybe I've misunderstood the problem? Is there a specific reason why you're looking to use a LinkButton in particular?
Write a javascript function and call it from each link's onlick like this:
My Link
someFunc can then set a hidden input field to the value you want the server to react to. This could be anything from the link element (the text of the link, the value of an attribute you specify in your AJAX response, etc.). At the end of the function, call form1.submit();
function someFunc(link){
var hiddenInput = document.getElementById("ClickedLink");
hiddenInput.value = link.innerText;
form1.submit();
}
Then on the server, in your On_Load in the code behind:
if(ClickedLink.Value != ""){
ProcessLinkClick(ClickedLink.Value);
}

Easiest way to simply display confirmation that a webservice worked?

I'm calling an asp.net webservice from an ASP clasic page basically just with:
<a href='http://domain/webservice.asmx/command'>Command</a>
and when users hit that button it works but they're just shown an xml page. The function will either work or not so I was wondering if it'd be possible to just have a pop up box appear to tell them if it worked or not after they clicked it rather than redirecting them to an xml page.
I'd prefer to not have to use jQuery or another javascript library.
If that's not possible, is there any way to dress up the XML page? Currently it says 'This XML file does not appear to have any style information associated with it. The document tree is shown below.' at the top.
Also, the domain that the webservice is on is different to the domain that the website that's call the webservice is on. Not sure if that matters.
Thanks
Check out this MSDN Link on Calling A WebService From Javascript Using AJAX. No JQuery is required and it boils down to having to use the ScriptService attribute on your WebService method and adding a ServiceReference in a ScriptManager control. You can then easily call your WebService from Javascript and it will call another Javascript function when it finishes. It is in that response function where you can add your confirmation display.
be aware that this is a bad idea to let the user handle directly - web services are almost always called by your code rather than a client browser session. One reason is that raw error information would be hown to the client if there were a problem.
If you really want to do this, you can either:
Use AJAX (No framework required - just JS) or
You can make the webservice non-standard so it returns user-friendly content - perhaps by wrapping it in a website which calls the API behind the scenes and formats the response in a meaningful fashion.

Getting server side javascript over client side

I have created custom control using asp.net 2.0. The control contains a textbox txtDate. I have also created a javascript file DateMask.js which contains a function maskDate(). I have attached the maskDate() with textbox using -
txtDate.Attributes.Add("onkeypress","maskDate()");
I have also registered the script using ClientScript.RegisterStartupScript.
When I execute the aspx page containing my custom control it is generating script error showing that maskDate() is undefined.
Could anybody tell me what exactly the problem is?
Thanks for your cooperation.
One way to do it would be to place a literal control above your textbox, and assign the script to it in the code behind:
literal1.Text = "<script>function maskDate() {...}</script>";
The benefit to this, is you would not need to have to reference the script file with some tricky relative paths depending on where your usercontrol is used.
Make sure you didn't forget <form runat="server" ID="Form1"></form> at the end of the <head> tag!
As you can read in Using JavaScript Along with ASP.NET 2.0 under "The Difference Between Page.ClientScript.RegisterStartupScript and Page.ClientScript.RegisterClientScriptBlock" they rely on the location of the form tag.
We have shown two different methods
for placing JavaScript functions on an
ASP.NET pageā€”so what is the
difference? The main difference is
that the RegisterStartupScript method
places the JavaScript at the bottom of
the ASP.NET page right before the
closing element. The
RegisterClientScriptBlock method
places the JavaScript directly after
the opening element in the
page. So what difference does this
make? It can make quite a bit of
difference, as we will see.

loading value to asp.net control using javascript

iam into problem of reading the value of the control which i alterd using javascript
the sequence goes like this
i got the text box control by using its id
cleared the value of the text box
make the control disabled.
when i tried to retrive the value of the textbox in aspx.cs
iam still getting the old value of the text box which i actualy cleared in the javascript
kindly suggest me to over come this issue
Thanks
Disabled input controls don't get their values posted back so ASP.Net doesn't know you changed the value. You need to enable the control.
With ASP.NET Web Forms, IDs for elements get assigned automatically by the Page on the server side. It is tricky, but possible, to manipulate element values using javascript. One way to avoid this would be to use ASP.NET MVC where the HTML would be rendered as is and javascript or jquery can be easily used to "play" with the HTML elements. We need more information to help you with the details of your request.

ASP.NET user control and accessing a property from Javascript?

This I can't work out
I have ASP.Net user control,which is basically two drop downs, that has a public property Index which is calculated from the drop downs, and works fine
I need to access the value of 'Index' from javascript, but accessing via getElementById was completely wrong, can anybody point me in a better direction
Cheers
The property you're talking about is a server side thing and has no presence in Javascript. Basically, whatever ASP.NET does on the server will generate a single HTML page containing a bunch of HTML tags. The browser is not aware of the user control being a single entity at all.
You should calculate the property on the client by looking up the ID of the drop down directly in Javascript. You can do find out the client ID of the drop down by getting its ClientID property.
Create javascript method as a string from server side and use index property to create script string.
register string on which event it is required.
you can take help from following link to register javascript:
http://msdn.microsoft.com/en-us/library/aa479011.aspx
If any server side control is used in javascript method, use ClientID property as ID of the control.
Hope, It helps

Resources