Multiple Pages of same Website through QueryString from IIS - c#

I have faced an issue while deploying a web application(asp.net c#, in IIS). What I have is single application for two DBs. So, Through querystring, I am determining the connectionstring in the master page.
My problem,is that I have two links each with separate QSs and when clicked, should lead to the corresponding version of the same application.Which is fine, for now when used one at a time. But when used side by side, they are producing the same DB application, as a replica of the first opened URL (which I guess is based on the server session).
Any suggestion on how to achieve this? Appreciate your help. Thanks!

I'm assuming that the idea at the moment is user will click the link on a page and this will redirect them to another page and on this page create the database connection based on the querystring.
Why not store the link clicked in a session variable, which you can then access on the page you're redirected to and create the DB link from that. If you're using a link button or something like that you could do:
Main Page
ASP.NET:
<asp:LinkButton ID="lnkButton1" Text="Link Button 1" OnClick="lnkMyButton_Clicked" runat="server" />
<asp:LinkButton ID="lnkButton2" Text="Link Button 2" OnClick="lnkMyButton_Clicked" runat="server" />
C#
protected void lnkMyButton_Clicked(object sender, EventArgs e)
{
LinkButton lnkButton = (LinkButton)sender;
Session["LinkID"] = lnkButton.ID;
Response.Redirect("MyNewPage.aspx");
}
On your new page:
protected void Page_Load(object sender, EventArgs e)
{
string LinkID = Session["LinkID"];
if(!String.IsNullorEmpty(LinkID))
{
if(LinkID == "lnkButton1") //create connection if first link clicked
else if(LinkID == "lnkButton2") //create connection if second link is clicked
else //handle error
}
else //handle null or empty string
}
I've not tested this, but let me know if it doesn't work.

Related

Pag.Previous page returns null

I have tried many times and in different ways to see why "Page.PreviousPage" returns null but I did not get anywhere. I checked most of posts here in stackoverflow and some other website. None of them worked. Anybody has any idea? Following is the asp code in the source page which I named it "sourcePage.aspx".It shows the button that does the cross page post:
<asp:Button ID="btnCrossPagePostBack" runat="server" Text="Cross page post back navigation" PostBackUrl="~/targetPage.aspx" Width="270px"/>
The following is the code in the target page which I named it "targetPage.aspx":
protected void Page_Load(object sender, EventArgs e)
{
Page previousPage = Page.PreviousPage;
if (previousPage != null && previousPage.IsCrossPagePostBack)
{
lblName.Text = ((TextBox)previousPage.FindControl("txtName")).Text;
lblEmail.Text = ((TextBox)previousPage.FindControl("txtEmail")).Text;
}
else
{
lblStat.Text = "You loaded this page using a technique other than Cross Page Post Back";
}
}
"txtName" and "txtEmail" are two text boxes in the source page. I know how to do this by using "Server.Transfer" or Strongly typed reference. I am looking for doing by Corss page post back and not any other way. Somebody said I should use Empty page and then add web forms and not template web forms but there is not empty page and visual studio only provides web forms. Any clue?
Thanks!
The point was correct. When you want to start a new project you should choose "Empty" and not "Web Form" or etc.

Enter data in web form, output to a new page

I am using a simple <form> to collect data from a user. The user clicks a simple button to enter the data: <input type="submit" name="cmd" value="OK">. Currently the application does a simple post back, displays the filled in form, and under the form, displays the results.
The users now want the results to go to another page. Basically they want to change a variable and compare the results in different tabs. My first proposal was to keep the post back and then add a hyperlink using target="_blank" to push the results to a different tab but they do not like the two-clicks: OK button then hyperlink.
Is it possible to send the results of a form input to another page?
I am programming in C# using ASP.NET.
you can do this by postbackurl property in c#. thus helps you to access your previous page controls and there output on next page. you can also do this by using hidden field and post or get method. both options are good and reliable.
reference
Seeing that you are using ASP.Net, I would recommend you utilise the power of the code-behind process. One option that you can do in addition to the above responses is use a QueryString in your URL as you do a re-direct if that is available to you as a requirement. Example 1. Use a ASP Button
protected void btnOriginalPage_Click(object sender, EventArgs e)
{
string url = "NextPageViewer.aspx?result=" + resultText;
//You can use JavaScript to perform the re-direct
string cmd = "window.open('" + url + "', '_blank', 'height=500,width=900);";
ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindow", cmd, true);
//or you can use this - which ever you choose
Response.Redirect(url);
}
///On the next page - the one you have been redirected to, perform the following
protected void Page_Load(object sender, EventArgs e)
{
//extract the query string and use it as you please
int result = Convert.ToInt32(Request.QueryString["resultText"]);
}
Example 2. Use a Session variable and store your dataset/result in an user defined object or DTO - this is just a class with a bunch of setters and getters
Perform the click event on the ASP button except this time you would do something like this:
protected void btnOriginalPage_Click(object sender, EventArgs e)
{
ObjectWithInfo.Result = result;
Session["friendlyName"] = ObjectWithInfo;
Response.Redirect("NextPageViewer.aspx");
}
//On the next page - the one you have been redirected to, perform the following
//The good thing with Session variables is that you can access the session almost anywhere in you application allowing your users to perform the comparison they require.
protected void Page_Load(object sender, EventArgs e)
{
//extract the data from the object to use
int result = ((ObjectWithInfo)(Session["friendlyName"])).Result;
}

Clear Fields Button (redirect not working)?

I have a webform with a "clear all fields" button, but it's not working properly. When I click the button on the rendered page, it directs me to another page on the local server, but not the one I have listed here.
protected void btn_Clear_Click(object sender, EventArgs e)
{
Response.Redirect("~/ContentRequest/PR_Event.aspx", true);
}
I'm not sure what I can do...
You can simply use Request.RawUrl to redirect back to current page.
Response.Redirect(Request.RawUrl, true)
However, if you want to just clear fields, you might want to use jQuery or javascript at client-side to avoid a round trip to server.

Maintained state of a web page

In my web app I has a GridView and some other control. User is allow to sort and filter the grid view.They also allow to click the link and go to other page.Then they also can go to other more pages. But when they cum back to the first gridview page. The grid is same as what they left the page. For example paging ,sorting and other .
I found a solution about this . But I not really understand.
http://www.codeproject.com/Articles/7655/Persisting-the-state-of-a-web-page
Here is my coding WebForm1.aspx
protected void Button2_Click(object sender, EventArgs e)
{
PersistentStatePage abc = new PersistentStatePage();
abc.RedirectSavingPageState("WebForm2.aspx");
}
WebForm2.aspx
protected void Button1_Click(object sender, EventArgs e)
{
PersistentStatePage.RedirectToSavedPage("WebForm1.aspx", true);
}
Can anyone guild me some example?
That codeproject article is not really meant for this sort of scenario. It is about saving viewstate to a secure, on server location rather than sending it all to the user in a hidden input field which leads to a lot of bloat.
The best way to go about this would be session.
Create a custom object to store the info you want to persist and put it in session.
Check out the following for an article about using session http://asp.net-tutorials.com/state/sessions/
Then you don't have to worry about passing the details around all the other pages.

asp:LinkButton code to open new browser window/tab after other code

For my internal webpage at work, I display a DataGrid based on entries in a SQL table (not directly, but with some processing on entries).
Each row in the DataGrid has a button that the user clicks. I need this button to open a new window or tab (I believe I can't decide as this is based on browser config) and also change a value in the SQL table to say the button was clicked.
If I use an asp:Hyperlink then the page opens nicely, but I don't know how to update SQL. Vice-versa if I use an asp:LinkButton I can get the SQL updated but can't get a new page to open.
Is what I am trying to do impossible?
Thanks
EDIT:
I've tried both these in my .cs file, but neither worked:
ClientScript.RegisterStartupScript(GetType(), "openwindow", "window.open('" + url + "','_preview'");
Response.Write("<script type='text/javascript'>detailedresults=window.open('" + url + "');</script>");
<asp:TemplateField HeaderText="Action"> <ItemTemplate>
<asp:LinkButton ID="lbtnConfigureSurvey" runat="server" CausesValidation="False"
CommandName="ConfigureSurvey" CommandArgument='<%# Eval("intSurveyID") %>'
OnClientClick="aspnetForm.target ='_blank';">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void gvSurveyList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ConfigureSurvey")
{
Response.Redirect('Your page url');
}
}
You can use LinkButton and use OnClientClick property to set the java-script that would open the page in new browser window. This script must return true so that post-back can happen and you can handle click event on server side to update your database.
IMO, better way would be to use hyper-link and open the page in new browser window. However, you must pass a query-string parameter while opening the page (say Page2) that would tell the new page that it should update the database. So url for opening the page will be something like "page2.aspx?recordId=xyz". And within page2.aspx.cs, you will have code such as
protected void page_load(object sender, EventArgs e)
{
if (!IsPostback)
{
var recordId = Response.QueryString["recordId"];
if (!string.IsNullOrEmpty(recordId))
{
// update database
...
}
}
...
}
From security perspective, you may want to include time-out within parameter value and encrypt the entire thing.
You could use your LinkButton and call RegisterStartupScript on postback to execute something like this on page load
.aspx:
<asp:LinkButton OnClick="OnButtonClick" Text="Update" runat="server" />
.aspx.cs:
protected void OnButtonClick(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "openwindow", "<script type=text/javascript> window.open('About.aspx'); </script>");
}
You need to supply the whole script element to RegisterStartupScript.
try this
Response.Write("<script type='text/javascript'>detailedresults=window.open('PAGE NAME.aspx');</script>")
EDIT:
you can set command name for link button and execute code in rowcocommand event
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("commandname for linkbutton"))
{
//update code
Response.Write("<SCRIPT language=\"javascript\">open('Yourpage.aspx','_blank','top=0,left=0,status=yes,resizable=yes,scrollbars=yes');</script>");
}
}
Hyperlink will not call server side event on click, so you won't get post back to perform the SQL task, and by using the LinkButton you can open the page in the new window or tab. To do so you need to add a little code below in the Grid_RowBoundEvent.
Ex: LinkButton.OnClientClick="window.open('http://www.google.com');";
The url you can change as per your requirement.
Again, in the Code behind you will get a server side event. There you can do your sql task.
Hope it will be helpful to you.

Categories