我在Repeater中的CheckBox有问题总是返回False Value。我已经阅读了很多这方面的问题,并总是指着我用Not Page.IsPostBack
绑定数据,我已经完成了我发现的所有示例问题,但仍然没有解决我的问题。
这里我如何绑定复选框:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not Page.IsPostBack Then
Call Load_menu()
End If
Catch ex As Exception
Response.Write("Page_Load Exception :<br>" & ex.ToString)
End Try
End Sub
Private Sub Load_menu()
Try
sqlstring = "SELECT a.menuID, a.name, b.[status] " & _
"FROM masterMenu a LEFT JOIN rolesDetail b ON b.menuID=a.menuID " & _
"WHERE a.[status] = 1 and b.RoleID = '" & roleID & "' " & _
"ORDER BY a.menuID "
DS = SQLExecuteQuery(sqlstring)
DT = DS.Tables(0)
rptMenu.DataSource = DT
rptMenu.DataBind()
Catch ex As Exception
Response.Write("Load_Menu Exception :<br>" & ex.ToString)
End Try
End Sub
Private Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
Try
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim lblID As Label = e.Item.FindControl("LblID")
Dim lblName As Label = e.Item.FindControl("LblName")
Dim cblChild As CheckBox = e.Item.FindControl("cblChild")
Dim dr As DataRowView = e.Item.DataItem
lblID.Text = dr.Item("menuID").ToString
lblName.Text = dr.Item("name").ToString
If dr.Item("status").ToString = "1" Then
cblChild.ID = dr.Item("menuID").ToString
cblChild.Checked = True
Else
cblChild.ID = dr.Item("menuID").ToString
cblChild.Checked = False
End If
End If
Catch ex As Exception
Throw New Exception("<b>Error RptMenu Data bound :</b>" & ex.ToString)
End Try
End Sub
在这里我如何渲染转发器:
<form id="form1" runat="server" class="form-horizontal">
<div class="box-header">
<h3 class="box-title"><asp:Label ID="lblHeader" runat="server"></asp:Label></h3>
</div>
<div class="box-body no-padding">
<asp:Repeater ID="rptMenu" runat="server">
<HeaderTemplate>
<table class="table table-striped">
<tr>
<th style="width: 10px">#</th>
<th>ID</th>
<th>Nama</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:CheckBox ID="cblChild" runat="server"></asp:CheckBox></td>
<td><asp:Label ID="LblID" runat="server"></asp:Label></td>
<td><asp:Label ID="LblName" runat="server"></asp:Label></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div class="box-footer text-center">
<dxe:aspxbutton id="btSimpan" runat="server" text="Simpan" cssclass="btn btn-primary" enabledefaultappearance="False"></dxe:aspxbutton>
</div>
</div>
</form>
这里Submit
按钮如何处理它:
Protected Sub btSimpan_Click(sender As Object, e As EventArgs) Handles btSimpan.Click
Try
sqlstring = ""
For i As Integer = 0 To rptMenu.Items.Count - 1
Dim item As RepeaterItem = rptMenu.Items(i)
Dim lblID As Label = item.FindControl("LblID")
Dim cblChild As CheckBox = item.FindControl("cblChild")
Dim status As String = "0"
If cblChild.Checked Then
status = "1"
End If
sqlstring = sqlstring & " UPDATE rolesDetail SET [status] = " & status & " " & _
" WHERE roleID = '" & roleID & "' AND menuID = '" & lblID.Text.ToString.Replace("'", "''") & "'; "
Next
If SQLExecuteNonQuery(sqlstring) > 0 Then
Response.Redirect("roles.aspx")
End If
Catch ex As Exception
Response.Write("Error btSimpan_Click <BR> " & ex.ToString)
End Try
End Sub
我在SQL Server中使用Profiler跟踪它,它总是返回0
值,这意味着cblChild.Checked
总是返回False。
我不知道我错过了什么。
答案 0 :(得分:2)
尝试使用HtmlInputCheckBox
控件而不是CheckbBox
更改此行
Dim cblChild As CheckBox = item.FindControl("cblChild")
到
Dim cblChild As HtmlInputCheckBox = DirectCast(item.FindControl("cblChild"), HtmlInputCheckBox)
这还需要使用带runat="server"
的html复选框控件:
<input type="checkbox" runat="server" id='cblChild'/>
因为我们从html为复选框分配ID
。删除在rptMenu_ItemDataBound
事件中设置此ID,此行应删除:
cblChild.ID = dr.Item("menuID").ToString