gpt4 book ai didi

c# - 复选框!指定的转换无效

转载 作者:行者123 更新时间:2023-12-02 15:40:06 25 4
gpt4 key购买 nike

我有这个错误:specified cast is not valid 当我尝试向我的网格添加一个包含 itemtemplate 复选框的新行时,如下所示。每当我使用“已检查”一词而不是“文本”时,就会显示错误。但我想要做的是在我的“choiceQn”为真时显示复选框“已选中”,而不是在我的复选框旁边显示“真”。如果你能解决我的问题,请帮助我。

ASP.NET

 <ItemTemplate>
<asp:CheckBox ID="ChoiceCheckBox" runat="server" **Checked**='<%# Bind("ChoiceQn") %>'/>
</ItemTemplate>

C#

    private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;

if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox box1 = (TextBox)UpdateQuestionGrid.Rows[rowIndex].Cells[0].FindControl("QuestionsTbx");

drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["QuestionId"] = i + 1;
drCurrentRow["Question"] = "";
drCurrentRow["ChoiceQn"] = false;
rowIndex++;
}

//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);

//Store the current data to ViewState
ViewState["CurrentTable"] = dtCurrentTable;

//Rebind the Grid with the current data
UpdateQuestionGrid.DataSource = dtCurrentTable;
UpdateQuestionGrid.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}

//Set Previous Data on Postbacks
//SetPreviousData();
}

堆栈跟踪:

[InvalidCastException: Specified cast is not valid.]
ASP.asc_questionupdate_aspx.__DataBinding__control15(Object sender, EventArgs e) in f:\ASC_FeedbackSystem(latest)\ASC_FeedbackSystem\asc\questionupdate.aspx:26
System.Web.UI.Control.OnDataBinding(EventArgs e) +99
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +92
System.Web.UI.Control.DataBind() +15
System.Web.UI.Control.DataBindChildren() +211
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
System.Web.UI.Control.DataBind() +15
System.Web.UI.Control.DataBindChildren() +211
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
System.Web.UI.Control.DataBind() +15
System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +155
System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +2417
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57
System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
System.Web.UI.WebControls.GridView.DataBind() +4
ASC_FeedbackSystem.questionupdate.AddNewRowToGrid() in F:\ASC_FeedbackSystem(latest)\ASC_FeedbackSystem\asc\questionupdate.aspx.cs:108
ASC_FeedbackSystem.questionupdate.ButtonAdd_Click(Object sender, EventArgs e) in F:\ASC_FeedbackSystem(latest)\ASC_FeedbackSystem\asc\questionupdate.aspx.cs:161
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

最佳答案

当你使用 nvarchar(1) ,尝试将 Eval() 用于 CheckBox 的 Checked 属性:

<ItemTemplate>  
<asp:CheckBox ID="ChoiceCheckBox" runat="server" Checked='<%# Eval("ChoiceQn").ToString().Equals("1") %>'/>
</ItemTemplate>

或者考虑使用与 '<%# Bind("ChoiceQn") %>'/ 兼容的 bool 数据类型

关于c# - 复选框!指定的转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8799986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com