GET different url based on inputbox - c#

I'm using the asp.net MVC pattern of having a url like controller/action/id. This works fine if I navigate to the url directly.
I want to have a page where the user can input the id and click a button (or link) which takes them to the correct url.
Do I need to somehow make an action link that points to a varying url based on the input using some client-side script?
Or can I render a Form that GETs the correct url?
Is what I'm doing unusual; should I be doing something else? (Note that the number of ids is too long to list).

Assuming you have the default route set up, you can do the following client-side script (make sure it's in the same file though):
var yourLink = '#Url.Action("controller", "action")';
//should output controller/action/
Then assuming your button has the id myButton and the textbox has the id myTextBox, you can do this jQuery:
$("#myButton").click(function () {
location.href = yourLink + $("#myTextBox").val();
//should output controller/action/5 for example, although you might
//want to make sure they've put a "correct" value in here
});

Related

Open URL from JSON via C# and ASP.Net

I have variable URL strings that are read from an external JSON block into the C# code behind.
I am then creating clickable buttons in a table which need to open a new window and launch those URLs. These are held in object's String variables.
However, I cannot find a way to make a function on the aspx side that opens a window on click and uses the URL string.
Currently I am adding a attribute to the button
Button b = new Button();
b.Attributes.Add("onClick", "OpenURL()");
bCell.Controls.Add(b);
With this I can open a window, but I can't seem to get the URL I deserialized from the JSON string over to the OpenURL()
function OpenURL(url) {var x = window.open(url, 'mynewwin');
function on the front end.
Since the url varies, I cannot hard code it anywhere.
All of the buttons, rows, and cells are generated dynamically from the JSON strings. So no hard coding can happen on these.
//First time poster. Tried to look for solutions but failed
If you know what the url is at the time you're creating the button, you can do:
Button b = new Button();
var url = "some url";
b.Attributes.Add("onClick", string.Format("OpenURL({0})",url));
bCell.Controls.Add(b);
If you don't know the url until after the page is loaded, you can store it in a variable on the page and retrieve it when you click the link.
<script>
var url;
//have whatever you use to set the url call this function
function setUrl(inputUrl){
url = inputUrl;
}
function OpenURL(){
var x = window.open(url,'some window');
}
</script>

Code in C# that creates redirect buttons in HTML

I am trying to create a C# string builder that will later build an html page based on information in a database and user input.
What I am having trouble with is creating two buttons in the string where one button redirects to page a.asp and the other button redirects to b.asp
I have tried multiple methods but none seem to work. Here is my latest version of code but I might be way off track:
in my page.asp.cs file:
responseString +=
"<div>"
+"<table><tr>"
+"<td><button id='submitSave' type='submit'>Save</button></td>"
+ "<td><button id='continueBatch' onserverclick=\"OnClickButton\" type='submit' runat'server'>Continue Batch</button></td>"
+ "<td><button id='submitDelete' onclick='confirmDialog()' type='button'>Delete</button></td>"
+ "</tr></table></div>";
and it points to the method also in page.asp.cs:
public void OnClickButton()
{
//Redirect to New
Response.Redirect(String.Format("New.aspx?fmtypeP={0}&formverP={1}", FormTypeS, FormVersionS));
}
and last but not least I have in the page.asp page the following:
<form action="save.aspx" method="POST">
I know the form action will need to change but I just wanted to let you know I currently had it in place.
Am I working in the right direction? Is there an easier way to accomplish my task? If not what am I doing wrong?
I would rely on ajax to solve this problem if possible. One solution would be to use onclick and call an existing javascript method like you seem to have done for the confirmDialog() on the delete button. Maybe use jquery's wrapper for the ajax call: http://api.jquery.com/jquery.ajax/. Let the server method return the redirect link and use window.location = the return value in the success-method.
Or if you know what the redirect links should be when building the string you can make the redirect directly: onclick='window.location = "your redirect link"'
You can not use server-side controls like that. In your code they just added to response stream and not being compile and stuff. You need to add controls to your aspx page or add them in code like actual controls via new(), not like strings.

Is that possible to append the querystring in the controller for MVC

I'm trying to append values to the URL in the server side action. Is that possible? Looks like we have to do that from the browser side when submit the request. Is there an generic way to add querystring to the URL?
Yes it is possible ...
Use JavaScript to append the selected value to the domain before it submits. Change the button to have an onclick attribute as oppose to making it submit the form.
Add this JavaScript to your head section (or wherever you want, but convention is typically the HEAD section or bottom of the body)

How can I get the part of the URL that is getting changed whenever I clicked on some link?

My home page is :
http://localhost:8089/AFPWeb4.0/Project/ProjectList.aspx?StartPage=1
I have a link ProjectListInProjectGroups . When a user clicks on that link, my URL is changed to
http://localhost:8089/AFPWeb4.0/ProjectListInProjectGroups.aspx?
How can I get part of the URL that is changed ?
I need this value ProjectListInProjectGroups. I want to get this value in a ascx page.
Can someone help on this?
You can pass the current URL to the next page as a parameter and then you can find the difference between the two URLs using string operations.
If in your console you type window.location.pathname you would get the route after host name.
In console just type window.location and you can see what properties that has.

URL and Query management Asp.Net C#

Ok so while back I asked question Beginner ASP.net question handling url link
I wanted to handle case like this www.blah.com/blah.aspx?day=12&flow=true
I got my answer string r_flag = Request.QueryString["day"];
Then what I did is placed a code in Page_Load()
that basically takes these parameters and if they are not NULL, meaning that they were part of URL.
I filter results based on these parameters.
It works GREAT, happy times.... Except it does not work anymore once you try to go to the link using some other filter.
I have drop down box that allows you to select filters.
I have a button that once clicked should update these selections.
The problem is that Page_Load is called prior to Button_Clicked function and therefore I stay on the same page.
Any ideas how to handle this case.
Once again in case above was confusing.
So I can control behavior of my website by using URL, which I parse in Page_Load()
and using controls that are on the page.
If there is no query in URL it works great (controls) if there is it overrides controls.
Essentially I am trying to find a way how to ignore parsing of url when requests comes from clicking Generate button on the page.
Maybe you can put your querystring parsing code into IsPostBack control if Generate button is the control that only postbacks at your page.
if (!IsPostBack)
{
string r_flag = Request.QueryString["day"];
}
As an alternative way, at client side you can set a hidden field whenever user clicks the Generate button, then you can get it's value to determine if the user clicked the Generate button and then put your logic there.

Categories

Resources