数据绑定问题
我有一个 DataList 控件
<asp:DataList ID="DataList1" runat="server" DataKeyField="FruitID" RepeatColumns="2" Width="387px">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="104px" ImageUrl='<%# Eval("ImageUrl") %>' Width="135px" />
<br />
Item ID:
<asp:Label ID="lblItemID" runat="server" Text='<%# Eval("FruitID") %>' />
<br />
FruitName:
<asp:Label ID="lblFruitNameLabel" runat="server" Text='<%# Eval("FruitName") %>' />
<br />
UnitPrice:
<asp:Label ID="lblUnitPriceLabel" runat="server" Text='<%# Eval("UnitPrice") %>' />
<br />
Quantity:
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:Button ID="btnAddtoCart" runat="server" onclick="Button1_Click" Text="Add to Cart" />
,如下所示,在我使用以下代码获取 DataList 控件内控件的值的代码中,
int id = int.Parse(((Label)DataList1.Controls[0].FindControl("lblItemID")).Text.ToString());
string Name = ((Label)DataList1.Controls[0].FindControl("lblFruitNameLabel")).Text;
double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("lblUnitPriceLabel")).Text.ToString());
int Quantity = int.Parse(((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString());
string Url = ((Image)DataList1.Controls[0].FindControl("Image1")).ImageUrl;
我收到以下异常
输入字符串的格式不正确。
以下行中出现异常
int Quantity = int.Parse(((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString());
我非常确定我正在向文本框输入整数值:)
我是否遗漏了某些内容?
I have a DataList Control as follows
<asp:DataList ID="DataList1" runat="server" DataKeyField="FruitID" RepeatColumns="2" Width="387px">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="104px" ImageUrl='<%# Eval("ImageUrl") %>' Width="135px" />
<br />
Item ID:
<asp:Label ID="lblItemID" runat="server" Text='<%# Eval("FruitID") %>' />
<br />
FruitName:
<asp:Label ID="lblFruitNameLabel" runat="server" Text='<%# Eval("FruitName") %>' />
<br />
UnitPrice:
<asp:Label ID="lblUnitPriceLabel" runat="server" Text='<%# Eval("UnitPrice") %>' />
<br />
Quantity:
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:Button ID="btnAddtoCart" runat="server" onclick="Button1_Click" Text="Add to Cart" />
and in the code behind im using the following code to get the values of the controls inside the DataList control
int id = int.Parse(((Label)DataList1.Controls[0].FindControl("lblItemID")).Text.ToString());
string Name = ((Label)DataList1.Controls[0].FindControl("lblFruitNameLabel")).Text;
double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("lblUnitPriceLabel")).Text.ToString());
int Quantity = int.Parse(((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString());
string Url = ((Image)DataList1.Controls[0].FindControl("Image1")).ImageUrl;
I'm getting the following exception
Input string was not in a correct format.
Exception occurs in the following line
int Quantity = int.Parse(((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString());
I'm very much sure that i'm entering a Integer value to the textbox :)
am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
时,也许会很棒
当您稍微重写代码并使用调试器检查 myQuantity 的类型 。这应该可以帮助您找到问题所在。
Maybe that would be great when you little bit rewrite your code to this
and check with Debugger what type is myQuantity. This should help you to locate your issue.
这可能是因为类型转换问题,从您输入的数字到整数。尝试将其设置为长数据类型,并确保您输入的字符串之间不存在任何字符。
It could be because of the type casting problem, from the number you entered, to integer. Try making it a long data type instead and also make sure that no characters exists in between the string you entered.
试试这个
try this
我发现您没有对 txtQuantity 进行任何绑定。 Convert.ToInt32 给出异常,因为它是空字符串。我想是的
i saw that u have not make any bind to txtQuantity. And Convert.ToInt32 gives exception because it s null string. i think so