2013-03-11 32 views
0

正在使用中繼器在屏幕上顯示列表。對於每條記錄,我添加了一個<asp:checkbox,我希望實時更新到DB中的對應表,具體取決於它是否被點擊.....到目前爲止,我得到了:(使用JS) ASPX:複選框實時更新爲數據庫中的表格

<th style="width:200px;"><asp:CheckBox Name='<%# CallUtilityChangeId((int)Eval("id")) %>' runat="server" 
onclick='UtilityChanged(<%#((int)Eval("id"))%>);' 
Checked='<%# Convert.ToBoolean(Eval("Checked")) %>'/></th> 
<th style="width:200px;"><%# Eval("Comment") %></th> 

C#

protected string CallUtilityChangeId(int id) 
    { 
     return "Utilitychanged('" + id.ToString() + "');"; 
    } 

JavaScript的:

function UtilityChanged(id) { 
        userId = userIdentifier;      
       } 

源代碼返回:

<th style="width:200px;"> 
<span Name="Utilitychanged(&#39;55&#39;);"> 
<input id="ctl00_ContentPlaceHolder1_rptSelectedUtilities_ctl01_ctl00" type="checkbox" name="ctl00$ContentPlaceHolder1$rptSelectedUtilities$ctl01$ctl00" onclick="UtilityChanged(&lt;%#((int)Eval(&quot;id&quot;))%>);" /> 
</span></th> 
<th style="width:200px;"></th> 

而不是onclick =「UtilityChanged(<%#((int)Eval(" id "))%>);」 /> 我需要它爲例如返回UtilityChanged(「71」)。只是身份證號碼。的id。

+0

所以你只需要得到數據庫的記錄,你將要更新和一些AJAX javascript函數?用它的「ID」 - 我想知道你要怎樣做。 – 2013-03-11 10:44:45

+0

嗨Ebram,嗯......總體來說,我想選擇或由用戶,這將更新數據庫中的相應記錄,實時完成選擇此複選框(不使用頁面刷新)。 – John 2013-03-11 10:54:04

+0

@約翰在這種情況下,你需要配合使用一個UpdatePanel與中繼器。 – 2013-03-11 10:57:04

回答

0

糾正我,如果我錯了,但它聽起來像你想在一個服務器端事件的中繼器有一個複選框,你可以得到一個ID值在數據庫命令中使用,都是異步完成?如果是這樣,您可以使用更新面板來執行此操作。

首先,你必須換您的中繼器在一個更新面板和添加腳本經理:

<asp:UpdatePanel ID="UpdatePanel4" runat="server" >              
<ContentTemplate> 
<!-- put repeater in here --> 
</ContentTemplate> 
</asp:UpdatePanel> 

接下來,您要添加一個複選框與連接到它的服務器事件。這被添加到中繼器行。您還需要添加具有ID值的標籤。在本例中,其由數據源添加

<ItemTemplate>        
<tr class="listcolor">       
<td style="display:none;border-right:0px solid #808080 ; border-bottom:0px solid #808080;"> 
<asp:CheckBox ID="myCB" Text="Approved" runat="server" AutoPostBack="true" OnCheckedChanged="check_change" /> 
</td> 
<td style="display:none;border-right:0px solid #808080 ; border-bottom:0px solid #808080;"> 
<asp:label ID="userId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "someId") %>'></asp:label> 
</td>        
</tr> 
</ItemTemplate> 

現在,添加複選框事件。當控件回發到服務器,如果在一箇中繼器,或類似的控制,你可以投發件人導致回發,然後得到它的父行的對象,然後找到Label控件:

Protected Sub check_change(ByVal sender As Object, ByVal e As EventArgs) 
    'first, cast the sender to a check box 
    Dim cb As CheckBox = CType(sender, CheckBox) 
    'then use that to find the label in its parent control (the row its on) 
    Dim userId As Label = CType(cb.Parent.FindControl("userId"), Label) 
End Sub 

這應該做到這一點。如果您有任何問題,請告訴我。

0

(without using page refresh)表示使用Ajax

首先您應該瞭解如何將.Net服務器控件解析爲正常的HTML標籤。

假設你的代碼是這樣的:

<asp:Repeater Id="rpt"> 
...... 
    <asp:CheckBox ID="chkBox" runat="server" Name="someName" Checked="true" /> 
.... 
</asp:Repeater> 

它會呈現這樣的:

<span name="someName"> 
    <input id="rpt_ctl00_chkBox" type="checkbox" name="rpt$ctl00$chkBox" checked="checked"> 
</span> 

,你可以看到這裏有兩件事情::

  1. 複選框包含在<span>標籤內。
  2. IDName該複選框的屬性由.Net用相對於父容器(Repeater)「RepeaterId_ct100_CheckBoxId」的某些內容重命名。

其次的事情,如果你想獲得一些價值沒有通過視圖引擎被弄亂或.Net嘗試使用自定義屬性。

對不起長時間介紹。

解決方案::

然後由JavaScript或jQuery你得到的屬性值。

function YourJavascriptFunctionName(chk) 
{ 
    recordId = $(chk).attr("columId"); 
    //then use ajax to update the DB Table. 
} 

如何使Ajax調用是另一thing..tell我,如果你需要在它的幫助。