Solved

Passing a variable from one page to another

Posted on 2014-12-08
6
71 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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