Solved

Passing a variable from one page to another

Posted on 2014-12-08
6
72 Views
Last Modified: 2014-12-16
I'm trying not to give up on converting from asp to asp.net but even the simple things are a nightmare. I am trying to create a button in a column on a datagrid that takes me to another page and passes the asset_id. Here is the column code:

<asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="" Text="Select" OnClick="LinkButton1_click"/>
                    </ItemTemplate>
                </asp:TemplateField>

Open in new window


Here is the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class computer_assets_assets : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinkButton1_click(object sender, EventArgs e)
    {
        Response.Redirect("~/computer_assets/edit_assets.aspx?asset_id="
    }

}

Open in new window


How in the world do I finish the response.redirect?
0
Comment
Question by:princeservice
6 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 40487411
0
 

Author Comment

by:princeservice
ID: 40487481
That is no different than the 40,000 hours of video online. It does not specifically tell me how to fix my problem.
0
 
LVL 14

Accepted Solution

by:
Dustin Hopkins earned 500 total points
ID: 40487742
If you are just providing a hyperlink to another page. Why not use a HyperLinkField?
 <asp:HyperLinkField  ItemStyle-HorizontalAlign="Center" Text="Edit" HeaderText="" 
                     DataNavigateUrlFields="asset_id"
                     DataNavigateUrlFormatString="~/computer_assets/edit_assets.aspx?asset_id={0}" />

Open in new window


DataNavigateUrlFields takes a comma separated string representing the name(s) of the fields you want to use. The UrlFormatString works like using string.format with a url.

There are ways to do what you want (using a template field and buttons), however it will be slower and more complicated.

Hope this helps,
Dustin
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:Ronak Patel
ID: 40488283
I am guessing that you are trying to fetch the asset_id from the selected row of the Grid, and trying to pass it as a query string parameter to other page.

Below is the simple demo to perform above requirement:
Note: I have taken "asset_id" as a text box, in your case it might be a hidden field or else, so you have to implement accordingly.

On ASPX Page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title></title>
</head>
<body>
	<form id="form1" runat="server">
	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
		<Columns>
			<asp:TemplateField>
				<ItemTemplate>
					<asp:TextBox ID="asset_id" runat="server" />
				</ItemTemplate>
			</asp:TemplateField>
			<asp:TemplateField>
				<ItemTemplate>
					<asp:Button ID="Button1" runat="server" CausesValidation="false" Text="Select" OnClick="LinkButton1_click" />
				</ItemTemplate>
			</asp:TemplateField>
		</Columns>
	</asp:GridView>
	</form>
</body>
</html>

Open in new window


In Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
	if (!IsPostBack)
	{
		DataTable dt = new DataTable();
		DataRow dr = dt.NewRow();
		dt.Rows.Add(dr);

		GridView1.DataSource = dt;
		GridView1.DataBind();
	}
}

protected void LinkButton1_click(object sender, EventArgs e)
{
	GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);

	TextBox asset_id = (TextBox)gvr.FindControl("asset_id");

	if (asset_id != null)
	{
		Response.Redirect("~/computer_assets/edit_assets.aspx?asset_id=" + asset_id.Text);
	}
}

Open in new window


Whatever you type in asset_id textbox, it will be available in "asset_id.Text".


And on the edit_assets.aspx page, you can access the asset_id as below:

protected void Page_Load(object sender, EventArgs e)
{
	if (!IsPostBack)
	{
		if (Request.QueryString["asset_id"] != null)
		{
			string asset_id = Request.QueryString["asset_id"].ToString();
		}
	}
}

Open in new window


I hope this is what you are looking for.

Regards,
Ronak Patel
0
 

Author Comment

by:princeservice
ID: 40488729
This is what I ended up doing before I read that last few post.

<asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandArgument='<%# Eval("asset_id") %>' CommandName="" Text="Select" OnClick="LinkButton1_click"/>
                    </ItemTemplate>
                </asp:TemplateField>

Open in new window


and

  protected void LinkButton1_click(object sender, EventArgs e)
    {
        Response.Redirect("~/computer_assets/edit_assets.aspx?asset_id=" + ((Button)sender).CommandArgument);
    }

Open in new window


It seems to have worked. Does anyone see any issues with this?
0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 40489394
There shouldn't be a problem doing it that way if you are doing more processing on that page. Though in this case, since you are just redirecting, you are adding an extra unneeded post back to the server. You could use the hyperlink column like I described above or you could use the same eval in a hyperlink in your template. This would half the server load for this task.

HTH,
Dustin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calling web service and its methods dynamically 2 41
Trouble with References... 5 44
Error in query expression 3 36
Not showing JavaScript in the list 5 38
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now