从 ASPX:GridView 中的复选框列编辑/访问数据 - c#
我有一个 GridView,我将手动创建的数据表绑定到它。 GridView 和 dataTable 都包含 2 列:Name 和 isBusy。我的 GridView 看起来像这样
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name">
</asp:BoundField>
<asp:CheckBoxField DataField="isBusy" HeaderText="Busy" SortExpression="isBusy" />
</Columns>
工作正常,除了“忙碌”列不可编辑,除非您将特定行设置为编辑模式。我要求整列复选框都可以选中。因此,我将该列转换为模板,因此这些列看起来像这样:
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name">
</asp:BoundField>
<asp:TemplateField HeaderText="Busy" SortExpression="isBusy">
<ItemTemplate>
<asp:CheckBox ID="isBusy" runat="server" Checked='<%# Eval("isBusy") %>' oncheckedchanged="CheckBoxBusy_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
现在,这会在运行时引发错误:
System.InvalidCastException was unhandled by user code
Message="Specified cast is not valid."
Source="App_Web_zzjsqlrr"
StackTrace:
at ASP.projects_aspx.__DataBinding__control24(Object sender, EventArgs e) in c:\Project\Users.aspx:line 189
at System.Web.UI.Control.OnDataBinding(EventArgs e)
at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
at System.Web.UI.Control.DataBind()
at System.Web.UI.Control.DataBindChildren()
InnerException:
知道为什么会发生这种情况吗?我需要的下一步是知道如何设置和获取复选框的状态(尚未找到如何手动检查复选框)。
I have a GridView to which I bind a dataTable that I manually created. Both the GridView and the dataTable contain 2 columns, Name and isBusy. My GridView looks like this
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name">
</asp:BoundField>
<asp:CheckBoxField DataField="isBusy" HeaderText="Busy" SortExpression="isBusy" />
</Columns>
That works fine, except that the Busy column is non-editable unless you set a specific row to edit mode. I require the entire column of checkboxes to be checkable. So I converted the column to a template, and so the columns look like this:
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name">
</asp:BoundField>
<asp:TemplateField HeaderText="Busy" SortExpression="isBusy">
<ItemTemplate>
<asp:CheckBox ID="isBusy" runat="server" Checked='<%# Eval("isBusy") %>' oncheckedchanged="CheckBoxBusy_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
Now, this throws an error at runtime:
System.InvalidCastException was unhandled by user code
Message="Specified cast is not valid."
Source="App_Web_zzjsqlrr"
StackTrace:
at ASP.projects_aspx.__DataBinding__control24(Object sender, EventArgs e) in c:\Project\Users.aspx:line 189
at System.Web.UI.Control.OnDataBinding(EventArgs e)
at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
at System.Web.UI.Control.DataBind()
at System.Web.UI.Control.DataBindChildren()
InnerException:
Any idea why this is happening? The next step I would need is to know how to set and get a checkbox's state (haven't been able to find how to manually check a checkbox).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我克服了将复选框值转换为 bool 的错误:
Checked='<%# Convert.ToBoolean(Eval("isBusy")) %>'
现在,我似乎找不到如何手动检查特定复选框并在单击复选框时生成事件。有什么想法吗?
Ok, I got past the error coverting the checkbox value to a bool:
Checked='<%# Convert.ToBoolean(Eval("isBusy")) %>'
Now, I can't seem to find how to manually check a specific checkbox and generate an event when a checkbox is clicked. Any ideas?