2013-02-19 2 views
0
StringBuilder str=new StringBuilder(); 
str.Append("<div style='font-style:oblique;font-size:24px;text-align:left;margin-bottom:25px;'>"); 
str.Append("Products"); 
str.Append("</div>"); 
str.Append("<div style='font-style:oblique;font-size:18px;text-align:right;margin-bottom:25px;'>"); 
str.Append("<asp:TextBox ID='txtSearch' runat='server'></asp:TextBox> &nbsp;&nbsp; <asp:Button ID='btbSearch' runat='server' Text='Search'/>"); 
str.Append("</div>"); 
str.Append("<table width='100%' border='1' cellpadding='0' cellspacing='0'>"); 

두 번째 <div>에서 나는 텍스트 상자와 단추를 만들고 자리 표시 자에 추가하려고합니다. 브라우저 콘솔로 디버깅 할 때 HTML 코드로 표시되지만 브라우저에 표시 할 수 없으며 브라우저에 표를 표시합니다. 이러한 사례를 수행 할 수있는 예가 있습니까?C에서 자리 표시 자에 양식을 만들려면

답변

2

<asp: 태그는 모두 서버 측 태그입니다. ASP 엔진에 의해 일반 HTML 태그로 변환됩니다.

이러한 태그를 응답에 직접 작성하면 ASP 엔진이 해당 HTML을 리팩토링 할 수 없게됩니다. 브라우저가 그것들을 얻었을 때 그것들을 렌더링하는 방법을 모른다. 그래서 그것들을 그냥 무시한다.

당신은 예를 들어, 오히려 문자열로보다는 객체로 컨트롤을 생성해야합니다 더 나은 아직

TextBox txtSearch = new TextBox(); 
txtSearch.ID= "txtSearch"; 
placeHolder1.Controls.Add(txtSearch); 

Button btbSearch = new Button(); 
btbSearch.ID = "btbSearch"; 
btbSearch.Text = "Search"; 
placeHolder1.Controls.Add(btbSearch); 

또는, 당신은 오히려 뒤에 코드를 사용하는 것보다 마크 업 파일에 텍스트를 배치 할 수 있습니다.

+0

감사합니다. Mr/Miss Servy –

3

대부분 서버에서 렌더링 할 ASPX 페이지를 생성하지 않고 브라우저에 StringBuilder의 결과를 보내는 것입니다. asp:TextBox 및 이와 유사한 ASP.Net 요소는 브라우저에서 렌더링 할 수 없으므로보기에는 아무 것도 표시되지 않습니다. 또한 HTML은 HTML을 완전히 무효로 만들지 않으므로 노드도 존재합니다.

브라우저에서 출력을 보려면 asp:TextBox 대신 <INPUT...> 요소를 생성하고 싶습니다.

참고 : 실제로 목표를 달성하는 더 좋은 방법이있을 수 있습니다 (예 : 클라이언트 측 템플릿 또는보기의 일반 렌더링). 그러나 실제 문제를 먼저 철자해야합니다.

0

당신은 반드시 StringBuffer로 구성되지 않은 HTML이

var tableBody=""; // here you have to define the table body yourself 

var form= 
    new { 
     [email protected]"get", // decide for your request method get/post 
     [email protected]"http://www.google.com" // the page processes the request 
    }; 

var [email protected]" 
<div style='font-style: oblique; font-size: 24px; text-align: left; margin-bottom: 25px'>Products</div> 

<div style='font-style: oblique; font-size: 18px; text-align: right; margin-bottom: 25px'> 
    <form method='"[email protected]"' action='"[email protected]"'> 
     <input id='txtSearch' name='txtSearch'> 
     &nbsp;&nbsp; 
     <input type='button' id='btbSearch' text='Search' onclick='this.parentNode.submit();'> 
    </form> 
</div> 

<table width='100%' border='1' cellpadding='0' cellspacing='0'>"[email protected]" 
</table> 
"; 

고 정부 코드와 같은 코드를 사용할 수 있습니다. 동적으로 HTML을 생성하려면 LINQ를 사용하는 것이 좋습니다.

자신이해야 할 일은 코드 주석에 있습니다. txtSearch에는 id뿐만 아니라 name도 지정되어 있습니다.

+0

ASP를 사용하는 전체 요점은 이와 같이 할 필요가 없습니다. 그것은 당신을위한 지루한 상용구 일의 대부분을 할 것이다. – Servy

+0

@Servy : sometimes –

+0

그래서이 컨텍스트에서 ASP를 사용하는 대신 HTML을 직접 생성하는 이점은 무엇입니까? ASP에서 해결할 수없는 문제는 무엇입니까? – Servy

관련 문제