How To Pass Dynamically created value into another page..?

i am dynamically create contents and buttons into my code behind “tree.aspx.cs” page

sbmainTbl.AppendFormat("<tr><td><div class=\"a\">{0}<div class=\"details\"<p id=\"p\">{1}</p><ul><li>Parent-UnitId:{2}</li><li>Address :{3}</li><li>City: {4}</li><li>EmailId: {5}</li><li>PhoneNo: {6}</li><li>FaxNo: {7}</li><li><input type=\"button\" value=\"ADD\"  onclick=\"f2();\" /></li></ul></div></div></div></td></tr>",
            CellText(dtparent.Rows[0]["UnitName"].ToString()),
            CellText(dtparent.Rows[0]["UnitName"].ToString()),
            CellText(dtparent.Rows[0]["ParentUnitId"].ToString()),
            CellText(dtparent.Rows[0]["Address"].ToString()),
            CellText(dtparent.Rows[0]["City"].ToString()),
            CellText(dtparent.Rows[0]["EmailId"].ToString()),
            CellText(dtparent.Rows[0]["PhoneNo"].ToString()),
        CellText(dtparent.Rows[0]["FaxNo"].ToString()));

Now i have another page called “Add.aspx Page
in that page i have textbox

<asp:TextBox ID="txtunitname" runat="server" CssClass="textbox"></asp:TextBox>

now i just want on click on dynamically create button on “tree.aspx” page the “unitname(dynamically create value)” will pass “Add.aspx” page and textbox should fill with unitname.

kindly help me,
Thanks.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

To pass pass values using asp.net you need to follow the asp.net way.

First on the first page you place that value on a page control, I prefer on your case a hidden field, that is also post back to any page.

Then you add your value to that field

ValueHiddenField.value = CellText(dtparent.Rows[0]["UnitName"].ToString())

and you make also a public field so the second page can read it

public string ValueHiddenField
{
    get
    {
        return ValueHiddenField.value;
    }
}

And you say to the button to post back to the second page using the PostBackUrl field of the button.

Now this is the way you get your data from second page:
On the redirect page declare what is the previous page on aspx as:

<%@ Reference Page ="~/PreviousPageName.aspx" %>

and on code behind on second page you get the value as:

if (Page.PreviousPage != null)
{
    if (Page.PreviousPage is PreviousPageClassName)
    {
        Label1.Text = ((PreviousPageClassName)Page.PreviousPage).ValueHiddenField;
    }
    else
    {
        Label1.Text = "no value";
    }
}
else
    Label1.Text = "no value from previous page";

Similar answers:
cross page posting
Cross-page posting. Is it a good pratice to use PreviousPage in Asp.net?

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply