Solved

Passing a variable from one page to another

Posted on 2014-12-08
6
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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 …

734 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