复选框实时更新到数据库中的表
本文关键字:数据库 实时更新 复选框 | 更新日期: 2023-09-27 18:03:13
使用中继器在屏幕上显示列表。对于每条记录,我添加了一个<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('55');">
<input id="ctl00_ContentPlaceHolder1_rptSelectedUtilities_ctl01_ctl00" type="checkbox" name="ctl00$ContentPlaceHolder1$rptSelectedUtilities$ctl01$ctl00" onclick="UtilityChanged(<%#((int)Eval("id"))%>);" />
</span></th>
<th style="width:200px;"></th>
而不是onclick = " UtilityChanged (& lt; % # ((int) Eval("id") %>);"/>我需要它返回UtilityChanged("71")。只要身份证号码。
如果我错了,请纠正我,但听起来你想在一个有服务器端事件的中继器中有一个复选框,在那里你可以得到一个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>
现在,添加复选框事件。当控件发送回服务器时,如果它位于中继器或类似的控件中,则可以将sender强制转换为引起回发的对象,然后获取其父行,然后找到标签控件:
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
应该可以了。如果有任何问题,请告诉我。
(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>
你可以注意到这里有两件事::
- 复选框包含在
<span>
标签中。 - 该复选框的
ID
和Name
属性由.Net
与父容器(Repeater)相关的东西重命名。RepeaterId_ct100_CheckBoxId
"。
第二件事,如果你想得到一些值没有混乱的视图引擎或.Net
尝试使用自定义属性。
很抱歉,我的介绍太长了。
解决方案::
<input id="chkBox" runat="server" type="checkbox" checked='<%# Eval("Your_Record_ColumnName")%>' onchange="YourJavascriptFunctionName(this);return false;" columId='<%# Eval("Column_Id")%>'/>
然后通过JavaScript或jQuery
获得该属性的值。
function YourJavascriptFunctionName(chk)
{
recordId = $(chk).attr("columId");
//then use ajax to update the DB Table.
}
如何使ajax调用,这是另一回事,告诉我,如果你需要帮助,