Pass value from DataList to another page


We can send a value from ButtonLink click within a DataList  by appending the value through CommandArgument as querystring. We can use a session variable to send the value without showing it in the URL. In ASP.NET 2.0, we can also use PostBackUrl property of the linkbutton.

Here is a sample code:

 <asp:Label ID="Label1" runat="server" visible="false"></asp:Label>
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="DataList1_OnItemCommand">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Runat=server Text="Go To Next>" CommandName="NextPage" CommandArgument='<%# Eval("userID") %>' PostBackUrl="~/Page2.aspx">

 …..

Code:

protected void DataList1_OnItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "NextPage")
        {
               string key1 = Convert.ToString(e.CommandArgument);
            this.Label1.Text = key1;
            }

    }
Page2: 
public void page_load()
    {
        if (!IsPostBack)
        {
            Label lbl1 = (Label)PreviousPage.FindControl("Label1");
            //Response.Write(lbl1.Text);
              //you can get lbl.Text as  the userID for your SelectParameter        
                //...............         


        }
    }

 

 

Advertisements

One Comment on “Pass value from DataList to another page”

  1. Here i am facing some problem to access previous page value
    Showing error ” Object reference not set to an instance of an object. ”
    for
    Label lbl1 = (Label)PreviousPage.FindControl(“Label1”);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s