【发布时间】:2014-09-13 12:30:51
【问题描述】:
我很感激我在这个过程中出错的地方。在这方面很新,一直在把我的头撞到墙上。可能很简单。
在搜索找到示例后,我使用了一个已填充 SqlDataSource 的转发器,并添加了一个带有静态值的 checkboxlist,当单击按钮时,我试图将其插入 Sql Server DB从转发器页脚。对于已分配任务的用户,它应该只从具有复选框列表值的行中插入。
我收到以下错误
CS0266: Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.CheckBoxList'. An explicit conversion exists (are you missing a cast?)
如果我注释掉复选框列表上的 if 语句,页面将构建而不会引发错误,但插入不会完成。
我已经研究了这个错误,但是在检查 if 语句中的 checkboxlist 值以选择具有值的行时,我并没有完全明白我到底做错了什么。
这是我的代码:
<asp:Repeater ID="Repeater3" runat="server" DataSourceID="TaskAssignReviewSDS">
<FooterTemplate>
<asp:Button ID="Button1" runat="server" Text="Add Users" />
</FooterTemplate>
<ItemTemplate>
<asp:Table ID="Table1" runat="server" Width="400px">
<asp:TableRow>
<asp:TableCell Width="40%">
<asp:HiddenField ID="UserIdHidden" runat="server" Value='<%# Bind("UserId") %>' />
<asp:Label ID="UserNamelbl" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
</asp:TableCell>
<asp:TableCell Width="60%">
<asp:CheckBoxList ID="AssignCB" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Value="1">Assign</asp:ListItem>
<asp:ListItem Value="2">Review</asp:ListItem>
</asp:CheckBoxList>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:Repeater>
与点击代码隐藏相关:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem ri in Repeater3.Items)
{
CheckBoxList cb = ri.FindControl("AssignCB");
if (cb.value != null)
{
string TasksId = Request.QueryString["TasksId"].ToString();
string UserId = ((HiddenField)ri.FindControl("UserIdHidden")).Value;
string Assignation = ((TextBox)ri.FindControl("AssignCB")).Text;
//do inserting process using above values.
string insertSql = "INSERT INTO TaskUser(UserId,TasksId, Assignation) VALUES(@UserId, @TasksID,@Assignation) ";
using (SqlConnection myConnection = new SqlConnection("Data Source={*MyserverIP*}\\SQLEXPRESS12;Initial Catalog=MyDBName;User ID=****;Password=*****"))
using (SqlCommand myCommand = new SqlCommand(insertSql, myConnection))
{
myConnection.Open();
myCommand.Parameters.AddWithValue("@TasksID", TasksId);
myCommand.Parameters.AddWithValue("@UserId", UserId);
myCommand.Parameters.AddWithValue("@Assignation", Assignation);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
}
}
感谢您的所有帮助。
【问题讨论】:
-
字符串赋值 = ((TextBox)ri.FindControl("AssignCB")).Text;是一个问题,将其更改为对复选框的引用,这可能会让您遇到下一个错误。
-
我还假设
if (cb.value != null)应该只是if (cb != null)。同样,这可能无法解决您的问题,但它们是应该考虑的一些事情。如果有帮助,请更新您的代码,以便我们为您提供更多帮助... -
尝试使用单选按钮而不是复选框。
-
在与 SqlConnection 相同的代码中看到“Button1_Click”让我眼花缭乱。
-
granadaCoder,你能给他一个替代方案吗?
标签: c# .net sql-server webforms batch-processing