2015-02-06 84 views
0

我也搜索了其他類似的問題,其中大部分都與jscript有關。我有足夠的時間努力學習C#,希望有答案。我似乎總是接近答案,只是發現我遇到了另一個隱藏的例外。我試圖做的是解決一些必須存在的更優雅的方式,但這裏是:我有一個複選框,當選中時,將Label1文本更改爲「1」,並且未選中將Label1文本更改爲「0」 。當單擊頁面上的按鈕時,它將數據字段發送到存儲過程。即使我可以看到標籤值是1或0,發送到SP函數總是會爲Label1發送'NULL',我假設它是空白標籤的默認值。那麼點擊按鈕時,如何讓頁面讀取標籤的當前值?獲取標籤文本值的當前值

<%@ Page Language="C#" %> 
<!DOCTYPE html PUBLIC "-/W3C//DTD/ XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1-transitional.dtd"> 

<script runat="server"> 

public void InsertCard (object source, EventArgs e) 
{ 
SqlDataSource1.Insert(); 
} 

public void CheckBox1_CheckChanged (object source, EventArgs e) 
{ 

    Response.Write(""); 
    if (CheckBox1.Checked == true) 
{ 
     Label1.Text = "1"; 
} 
    else 
    { 
     Label1.Text = "0";  
    } 
} 

</script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 

<body> 
    <form id="form1" runat="server"> 
      <asp:sqldatasource 
    id="SqlDataSource1" 
    runat="server" 
    connectionstring="<%$ ConnectionStrings:DevTestConnectionString %>" 
    insertcommand="AddMember" InsertCommandType="StoredProcedure"> 
     <insertparameters> 
     <asp:FormParameter Name="CardNumber" formfield="CardNumberBox" Type="int32" /> 
     <asp:FormParameter Name="NameFirst" formfield="NameBox" Type="String" /> 
     <asp:FormParameter Name="Valid" formfield="Label1" Type="string" /> 
     </insertparameters> 
    </asp:sqldatasource> 

     <div> 
      <asp:Label ID="Label1" runat="server"></asp:Label> 
      <hr /> 
      Last Name:<asp:TextBox id="NameBox" runat="server" Width="150px"></asp:TextBox> 
      <br>    
      Card Number:<asp:textbox id="CardNumberBox" runat="server" /> 
      <br> 
      <asp:CheckBox ID="CheckBox1" runat="server" Text=" Valid" OnCheckedChanged="CheckBox1_CheckChanged" AutoPostBack="true" /> 
      <br> 

     <asp:button id="Button1" runat="server" text="Add Card"  onclick="InsertCard" /> 
</div> 
     </form> 
</body> 
</html> 
+0

我認爲你想改變的代碼,並把它放到'CheckBox1_CheckStateChanged'事件也'按鈕Clicks'引發什麼是知道的'PostBacks'所以你可以處理這幾種不同的方式,'的JavaScript, HiddenField,會話變量....等'我也會移動事件代碼'