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

故事扮演 提交于 2020-06-09 04:55:43

问题


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.


回答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?



来源:https://stackoverflow.com/questions/21044296/how-to-pass-dynamically-created-value-into-another-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!