数据绑定问题

发布于 2024-09-25 02:23:53 字数 1764 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恬淡成诗 2024-10-02 02:23:53

时,也许会很棒

var myQuantity = ((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString();
int Quantity = int.Parse(myQuantity);

当您稍微重写代码并使用调试器检查 myQuantity 的类型 。这应该可以帮助您找到问题所在。

Maybe that would be great when you little bit rewrite your code to this

var myQuantity = ((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString();
int Quantity = int.Parse(myQuantity);

and check with Debugger what type is myQuantity. This should help you to locate your issue.

梦途 2024-10-02 02:23:53

这可能是因为类型转换问题,从您输入的数字到整数。尝试将其设置为长数据类型,并确保您输入的字符串之间不存在任何字符。

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.

愿与i 2024-10-02 02:23:53

试试这个

int Quantity =Convert.ToInt32(((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString());

try this

int Quantity =Convert.ToInt32(((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString());
魔法唧唧 2024-10-02 02:23:53
<br />
Quantity :
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
<br />

我发现您没有对 txtQuantity 进行任何绑定。 Convert.ToInt32 给出异常,因为它是空字符串。我想是的

<br />
Quantity :
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
<br />

i saw that u have not make any bind to txtQuantity. And Convert.ToInt32 gives exception because it s null string. i think so

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文