Solved

Passing a variable from one page to another

Posted on 2014-12-08
6
85 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
 

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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 …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

627 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