2012-10-30 33 views
0

我發現奇怪的是,當我檢查一些複選框(生成的HTML代碼)時,子函數沒有提醒(或顯示消息)正在檢查哪個複選框。生成HTML代碼的Web應用程序由複選框而不是顯示消息組成。 (Asp.net,C#)

我錯過了什麼嗎?

裏面的WebForm.aspx

<script type="text/javascript"> 

function sub() 
{ 

    for (i=0; i<arrChecks.length; i++) 
    { 
     var attribute = arrChecks[i].getAttribute("xid") 
     if (attribute == elementName) 
     { 
      // if the current state is checked, unchecked and vice-versa 
      if (arrChecks[i].checked) 
      { 
       arrChecks[i].checked = false; 
      } else { 
       arrChecks[i].checked = true; 
       alert("Checked!"); 
      } 

     } else { 
      arrChecks[i].checked = false; 
     } 
    } 

} 


</script> 

的WebForm.cs內

protected void ButtonCheckDate_Click(object sender, EventArgs e) 
    { 
     SqlConnection conn = ConnectDB("Data Source=JACKSERVERA;Initial Catalog=tablea;User Id=tableuser;Password=tablepassword"); 


     if ((TextBox2.Text != null) && (TextBox2.Text != "")) 
     { 
      try 
      { 
       String res = ""; 

       SqlCommand command = conn.CreateCommand(); 
       SqlDataReader reader; 
       command.CommandType = System.Data.CommandType.Text; 
       command.CommandText = "SELECT * from overviewtable where [dated] = @PDATED"; 
       command.Parameters.AddWithValue("@PDATED", TextBox2.Text); 
       command.Connection = conn; 
       sqlstmt.Text = command.CommandText; 

       conn.Open(); 
       reader = command.ExecuteReader(); 

       if (reader.HasRows) 
       { 
        res = res + "<form name=\"input\" runat=\"server\"><table border=1>"; 
        while (reader.Read()) 
        { 
         res = res + "<tr>"; 


         string thetime = (string)reader["time"]; 
         string thevenue = (string)reader["venue"]; 
         int numfreeseat = (int)reader["freeseat"]; 

         int occupiedseat = 0; 

         SqlCommand bkcommand = conn.CreateCommand(); 
         SqlDataReader bookinglist; 
         bkcommand.CommandType = System.Data.CommandType.Text; 
         bkcommand.CommandText = "SELECT count(*) from venuea where [dated] = @PDATED"; 
         bkcommand.Parameters.AddWithValue("@PTIME", thetime); 
         bkcommand.Connection = conn; 
         sqlstmt.Text = bkcommand.CommandText; 
         bookinglist = bkcommand.ExecuteReader(); 

         while (bookinglist.Read()) 
         { 
          if (bookinglist.HasRows) 
          { 
           occupiedseat = (int)bookinglist.GetValue(0); 
          } 
         } 


         int leftnumofseat = numfreeseat - occupiedseat; 

         string color = ""; 
         Boolean fullyoccupied = false; 

         if (leftnumofseat > 0) 
         { 
          if (leftnumofseat == numfreeseat) 
          { 
           // white 
           color = "#FFFFFF"; 
          } 
          else 
          { 
           // light gray - partial occupied 
           color = "#B8B8B8"; 
          } 
         } 
         else 
         { 
          // dark gray - fully occupied 
          color = "#505050"; 
          fullyoccupied = true; 
         } 


         res = res + "<td bgcolor=\"" + color + "\">"; 
         res = res + "Available: " + leftnumofseat + "/" + numfreeseat + ""; 
         res = res + "</td>"; 

         string checkboxval = TextBox2.Text + "_" + thetime + "_" + thevenue; 

         res = res + "<td>"; 
         if (fullyoccupied == false) 
         { 
          res = res + "<input type=\"checkbox\" name=\"xid\" value=\"" + checkboxval + "\" runat=\"server\" />"; 
         } 
         else 
         { 
          res = res + "FULL"; 
         } 
         res = res + "</td>"; 

         res = res + "</tr>"; 
        } 
        res = res + "</table><input type=\"submit\" value=\"Submit\" OnClick=\"sub\" runat=\"server\"></form>"; 
       } 
       LabelDateSelection.Text = res; 

       conn.Close(); 



      } 
      catch (Exception err) 
      { 
       errormsg.Text = err.Message; 
      } 
     } 
     else 
     { 
      LabelDateSelection.Text = "Enter Date!"; 
     } 



    } 

回答

1

取出這部分從你的代碼&看看它是否工作runat=\"server\"

我我不確定你是否可以創建和ASP.net服務器控制ol,因爲您正在嘗試&您正在向客戶端發送輸出,而runat=\"server\"由服務器呈現。檢查你的HTML代碼&看看它看起來如何。

動態創建HTML控件或創建簡單的HTML控件,如果這可以適用於您的情況。

看看例子,如何務實地創建控件

http://msdn.microsoft.com/en-us/library/kyt0fzt1%28v=vs.100%29.aspx

+0

是的,我試着刪除'runat = \「服務器\」'但它仍然不起作用。我需要動態地使用複選框列表創建表,因此我無法使用簡單的HTML代碼。 – Jack

+0

@KnowledgeSeeker是正確的,你不能使用JS創建ASP.net控件,所有的ASP控件都必須通過代碼創建,才能正確註冊爲可用。 –

+0

對不起,我使用CS動態創建了我的HTML表格和HTML複選框。我沒有使用Javascript來動態創建它們。問題是我需要動態創建,以便我可以檢測到哪個複選框被選中。 (另一個問題是可能有200多個複選框,當然,我不想使用靜態HTML來逐個創建它們)。我想在asp.net中是否有其他方法可以幫助我動態地創建複選框。 – Jack

0

除了由KnowledgeSeeker提出的觀點,你不能在風格創建ASP控制。

您可以通過使用FindControl方法來定位元素,然後從它還是在C#中獲得的價值得到了創建複選框的值添加類似代碼:

for (int i = 0; i < 5; i++) 

{ 

CheckBox chk = new CheckBox(); 

chk.ID = Convert.ToString(i); 

chk.Text = Convert.ToString(i); 

form1.Controls.Add(chk); 

} 

在這種情況下,你從0循環到5並創建複選框並將這些控件添加到窗體。

+0

感謝Ryan的幫助,但隨時可能有1到200多個複選框(如動態創建並取決於數據庫記錄)。請注意,我也希望找到選中的複選框的值,以便我可以找到最終用戶選擇的記錄。 – Jack

+0

那麼是的,然後你循環但是很多記錄被返回,0到5只是示例代碼。如果你已經爲200個盒子設置了命名約定,你可以在一個循環中尋找checkbox_i - 我是整數。 –

相關問題