c# - Stuck with httpwebrequest usage -
i bit confused in using httpwebrequest. tried articles not able doing first time. below code trying work on , have few questions,
a) aspx page has few controls defined , in code-behind create few controls. when httpwebrequest post, need consider controls , values? need post 1 of controls only. can control?
b) url should specified in "(httpwebrequest)webrequest.create"? assume same page shown user. example in example below ("http://localhost/myidentity/confirm.aspx?id=new&designer=true&colors=yes");
c) there else need modify or take care of either in markup or code achieve httpwebrequest?
private void onpostinfoclick(object sender, system.eventargs e) { asciiencoding encoding = new asciiencoding(); string postdata = ""; //read stored array , print each element array loop here. byte[] data = encoding.getbytes(postdata); // prepare web request... httpwebrequest myrequest = (httpwebrequest)webrequest.create("http://localhost/myidentity/confirm.aspx?id=new&designer=true&colors=yes"); myrequest.method = "post"; myrequest.contenttype = "application/x-www-form-urlencoded"; myrequest.contentlength = data.length; stream newstream = myrequest.getrequeststream(); // send data. newstream.write(data, 0, data.length); newstream.close(); }
you typically use server-to-server communication (not page page data transfer).
if i'm understanding post , question correctly, seem want send post data other page in asp.net application. if so, 1 way can change postbackurl of submit button (form target) instead of normal postback (to same page).
there other ways, should simplest.
<asp:button id="button1" runat="server" text="button" postbackurl="foo.aspx" /> in above, instead of posting itself, post sent foo.aspx can examine/use/process posted data.
update based on comment:
you don't have code way through httpwebrequest that. normal asp.net webforms model you.
given simple asp.net web forms page:
<form id="form1" runat="server"> coffee preference: <asp:radiobuttonlist id="coffeeselections" runat="server" repeatlayout="flow" repeatdirection="horizontal"> <asp:listitem>latte</asp:listitem> <asp:listitem>americano</asp:listitem> <asp:listitem>capuccino</asp:listitem> <asp:listitem>espresso</asp:listitem> </asp:radiobuttonlist> <asp:button id="button1" runat="server" text="button" onclick="button1_click" /> .... </form> your inline or code behind this:
protected void page_load(object sender, eventargs e) { //do whatever want on page_load } protected void button1_click(object sender, eventargs e) { //handle button event string _foo = coffeeselections.selectedvalue; ... }
Comments
Post a Comment