Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

asp.net - issue regarding post data programmatically to another aspx page

basically i have to pages like test.aspx and test1.aspx. test.aspx has one button and when button click then a routine is being called which will post data programmatically to test1.aspx page and test1.aspx page will show the data.

so here is the code for test.aspx page

protected void Button1_Click(object sender, EventArgs e)
{
    PostForm();
}

private void PostForm()
{

    WebRequest request = WebRequest.Create("http://localhost:14803/PaypalCSharp/Test1.aspx");
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    string postContent = string.Format("parameter1={0}&parameter2={1}", "Hello", "Wow");
    byte[] postContentBytes = Encoding.ASCII.GetBytes(postContent);
    request.ContentLength = postContentBytes.Length;
    Stream writer = request.GetRequestStream();
    writer.Write(postContentBytes, 0, postContentBytes.Length);
    writer.Close();


}

so here is the code for test1.aspx page

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        string str1 = Request.Form["parameter1"];
        string str2 = Request.Form["parameter2"];
    }
}

i just do not understand what is wrong in my code as a result data is not posting to that page. basically i want that when user click on button in test.aspx then data will post from test.aspx page to test1.aspx page programmatically and test1.aspx will show in the browser with posted data. please guide me what i need to change in my code as a result my requirement will be fulfill. thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you've made things harder than they need to be. Here are some other options:

Why don't you Response.Redirect to test1.aspx in the Button1_Click method and pass the two parameters in the query string?

Or, if you don't want the work done in a GET request, why don't you move the code in test1.aspx into the Button1_Click method and then redirect to test1.aspx after completion?

Alternatively, why not do a cross page post back to test1.aspx (although cross page posting is dependent on javascript)?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...