2012-04-06 4 views
0

양식 작업 URL을 재정의하는 데 문제가 있습니다.재정의 Asp 양식 작업 URL 동적으로

양식 작업을 재정의 할 클래스가 있습니다.

<html> 
<head> 
</head> 
<body> 
     <form id="form1" runat="Server" action="https://www.test.com"> 
     <div style=""> 
      <asp:button runat="Server" onclick="PostMe" Text="Post"/> 
     </div> 

     <asp:HiddenField runat="server" ID="test" value="0" /> 
     <asp:HiddenField runat="server" ID="test2" value="0" /> 

    </form> 
</body> 
</html> 





<script runat="server"> 

    void PostMe(Object sender,EventArgs e){ 

     RemotePost2 myremotepost = new RemotePost2(); 
     myremotepost.Url = "https://www.test.com"; 
     myremotepost.Add("field1","Tom"); 
     myremotepost.Add("field2","Sawyer"); 
     myremotepost.Post(); 
    } 


    public class RemotePost2{ 
      private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection(); 


      public string Url = ""; 
      public string Method = "post"; 
      public string FormName = "forms"; 

      public void Add(string name,string value){ 
       Inputs.Add(name,value); 
      } 

      public void Post(){ 
       System.Web.HttpContext.Current.Response.Clear(); 

       System.Web.HttpContext.Current.Response.Write("<html><head>"); 
       System.Web.HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">",FormName)); 
       System.Web.HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >",FormName,Method,Url)); 
       for(int i=0;i< Inputs.Keys.Count;i++){ 
        System.Web.HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">",Inputs.Keys[i],Inputs[Inputs.Keys[i]])); 
       } 
       System.Web.HttpContext.Current.Response.Write("</form>"); 
       System.Web.HttpContext.Current.Response.Write("</body></html>"); 

       System.Web.HttpContext.Current.Response.End(); 
      } 
    } 

문제는 내가 필드 1과 FIELD2 대신 양식 게시물 테스트 및 TEST2 값을 게시 할 수 없습니다입니다. postBackUrl 특성을 사용할 때 여전히 test 및 test2를 게시합니다.

내가 원하는 것은 양식 작업이 비어 있지 않으면 field1과 field2를 게시하는 것입니다. 왜냐하면 아무런 행동이 없으면 잘 작동하기 때문에 코드의 행동을 수정할 수 없기 때문입니다.

당신의 도움이 많이 건배

을 이해할 수있을 것이다

답변

3

당신은 뒤에로드 기능

myButton.Attributes.Add ("onclick을", "자바 스크립트 코드를 아래에 추가 할 수 있습니다 예 : document.forms [0 ] .action = 'http : //otherserver.com/theirpage.html'; ");

여기서 myButton은 버튼의 ID입니다.

+0

실수를 수정했습니다. – Ravia

+0

감사합니다. 말씀 드린 것처럼 했는데도 여전히 테스트 및 tes2 양식 필드를 게시합니다. 나는 field1과 field2를 게시하고 싶습니다. 어떻게해야한다고 생각합니까? – Pinchy

+0

field1 & field2를 숨기시겠습니까? – Ravia