Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 92
  • Last Modified:

Passing a variable from one page to another

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
princeservice
Asked:
princeservice
1 Solution
 
princeserviceAuthor Commented:
That is no different than the 40,000 hours of video online. It does not specifically tell me how to fix my problem.
0
 
Dustin HopkinsCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ronak PatelCommented:
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
 
princeserviceAuthor Commented:
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
 
Dustin HopkinsCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now