2013-03-11 64 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我,如果你需要在它的帮助。

相关问题