2009-08-13 3 views
1

저는 ADO.NET 및 SQL에 대한 지식을 테스트 중이며 현재는 기본 작업을 수행하려고합니다. 테이블에서 읽으려는 경우 사용자가 단추를 클릭하면 ApplicationName 열의 값이있는 메시지 상자가 나타납니다.SQL 데이터가있는 메시지 상자를 활성화 할 수 없습니다 - ASP.NET

버튼을 클릭하면 현재 아무 것도하지 않습니다. 어떤 아이디어입니까?

protected void TestSubmit_ServerClick(object sender, EventArgs e) 
    { 
     // Initialize the database connector. 
     SqlConnection connectSQL = new SqlConnection(); 

     // Send the connection string. 
     connectSQL.ConnectionString = @"Data Source = localhost\SQLEXPRESS;" + 
      "Initial Catalog = Inputs; Integrated Security = SSPI"; 

     try 
     { 
      // Setup our command. 
      SqlCommand theCommand = new SqlCommand("SELECT * FROM Inputs", connectSQL); 

      // Write the stored value in the text boxes. 
      connectSQL.Open(); 

      SqlDataReader theReader; 

      theReader = theCommand.ExecuteReader(); 
      theReader.Read(); 
      MessageBox(theReader["ApplicationName"].ToString()); 

      theReader.Close(); 
      connectSQL.Close(); 
     } 
     catch (Exception ee) 
     { 
      MessageBox("Oopsie: " + ee); 
     }  

private void MessageBox(string msg) 
    { 
     Label lbl = new Label(); 
     lbl.Text = "" + Environment.NewLine + "window.alert('" + msg + "')"; 
     Page.Controls.Add(lbl); 
    }

답변

2

기본적으로 "window.alert ('your message'); HTML을 브라우저에 보내면 JavaScript로 실행되지 않습니다. 정말로 "팝업"이되고 싶습니까? 그렇다면 레이블을 사용하여 JS를 출력하는 대신 RegisterStartupScript()를 사용하는 것이 좋습니다 (http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx). 그렇지 않다면 왜 라벨의 내용을 반송 메시지로 설정하지 않습니까? MSDN &에서 촬영

+0

바로 - 구현의 문제는 당신이 주위에 태그없이 자바 스크립트의 조각을 전송하고 있다는 점이다. 분명히 그것은 스크립트를 실행하지 않을 것입니다. WinForms에서 포스트 백에서이 서버 측을 수행하는 적절한 방법은 RegisterStartupScript 또는 RegisterClientScript – Skeolan

+0

DOH를 사용하여 스크립트를 등록하는 것입니다. 어떤 이유로 든

0

샘플은 예를

private void MessageBox(string msg) 
{ 
    StringBuilder cstext2 = new StringBuilder(); 
    cstext2.Append("<script type=\"text/javascript\">"); 
    cstext2.Append("window.alert('" + msg + "')} </"); 
    cstext2.Append("script>"); 
    ClientScript.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false); 
} 

당신은 대신 RegisterClientScriptBlockRegisterStartupScript을 사용할 수에 대한 수정했습니다.

편집 : 또는 기존 ASP 방식도 작동해야합니다.
편집기없이이 글을 쓰고 있습니다.

Response.Write(@"<script language='javascript'>window.alert('" + msg + "');</script>"); 
0

실제로 실행됩니다. 다른 코드에서이 코드를 사용하고 다른 프로젝트에서는 messagebox 함수가 잘 작동합니다. TextBox6이 웹 사이트의 ASP 텍스트 상자입니다

 
try 
     { 
      // Setup our command. 
      SqlCommand theCommand = new SqlCommand("SELECT * FROM Inputs", connectSQL); 

      // Write the stored value in the text boxes. 
      connectSQL.Open(); 

      SqlDataReader theReader; 

      theReader = theCommand.ExecuteReader(); 
      theReader.Read(); 
      TextBox6.Text = (theReader["ApplicationName"].ToString()); 

      theReader.Close(); 
      connectSQL.Close(); 
     } 
     catch (Exception ee) 
     { 
      MessageBox("Oopsie: " + ee); 
     }  

참고 :

는 여기에 내가 정말하고 싶은거야. TestSubmit을 클릭해도 텍스트가 표시되지 않습니다.

관련 문제