问题描述
||
我有一个数据源,该数据源具有一个insertparameters,其类型之一设置为Boolean,但是在insertparameters.defaultvalue之后的代码中添加它时,总是返回字符串。
代码背后的代码:
if (e.CommandName == \"InsertNew\")
{
TextBox Title = GridView1.FooterRow.FindControl(\"txtAddTitle\") as TextBox;
TextBox Quote = GridView1.FooterRow.FindControl(\"txtAddQuote\") as TextBox;
TextBox QuoteLink = GridView1.FooterRow.FindControl(\"txtAddQuoteLink\") as TextBox;
CheckBox Published = GridView1.FooterRow.FindControl(\"chkAddBox\") as CheckBox;
TextBox PublishedDate = GridView1.FooterRow.FindControl(\"txtAddPublishedDate\") as TextBox;
SqlDataSource1.InsertParameters[\"Title\"].DefaultValue = Title.Text;
SqlDataSource1.InsertParameters[\"Quote\"].DefaultValue = Quote.Text;
SqlDataSource1.InsertParameters[\"QuoteLink\"].DefaultValue = QuoteLink.Text;
SqlDataSource1.InsertParameters[\"Published\"].DefaultValue = Convert.ToString(Published.Checked);
SqlDataSource1.InsertParameters[\"PublishedDate\"].DefaultValue = PublishedDate.Text;
SqlDataSource1.Insert();
}
sql数据源的aspx代码为:
<InsertParameters>
<asp:Parameter Name=\"Title\" Type=\"String\" />
<asp:Parameter Name=\"Quote\" Type=\"String\" />
<asp:Parameter Name=\"QuoteLink\" Type=\"String\" />
<asp:Parameter Name=\"Published\" Type=\"Boolean\" />
<asp:Parameter Name=\"PublishedDate\" Type=\"DateTime\" />
</InsertParameters>
请从后面的代码中回复如何将数据类型设置为布尔值。
解决方法
1:不知道为什么要设置DefaultValue而不是值
要么
2:您可以尝试以下方法:
SqlDataSource1.InsertParameters[\"Published\"].DefaultValue = Published.Checked==true?\"true\":\"false\";