【问题标题】:textbox not displaying in code behind c# file文本框未显示在 C# 文件后面的代码中
【发布时间】:2010-11-17 19:41:12
【问题描述】:

我有一个用 C# 编写的销售模拟器。我没有写这个但是我目前正在修改它以适应不同的要求。

它基本上显示来自数据库的产品列表,并且每个产品都有一个文本框,您可以在其中输入销售数量。售出的数量是根据它在数据库中的价格计算的。

现在一切正常,但是有一个小问题。单击“购买”时,它会将我返回到正确的产品列表,但是我为之前的产品输入的数量仍然存在。

我想知道在数据提交到数据库后如何将文本框恢复为默认值。

我一直认为这样做的方法是在文件后面的 .cs 代码中

txtQuantity.Text = "";

但是,由于某些奇怪的原因,txtQuantity 不会显示。

谁能想到我做错了什么?这是aspx文件中的sn-p代码。

    <form id="form1" runat="server">
    Date:
    <asp:TextBox ID="txtDate" runat="server" />
    Retailer:
    <asp:DropDownList ID="dlStore" runat="server" 
        onselectedindexchanged="dlStore_SelectedIndexChanged" />
    <asp:ListView ID="lbProducts" runat="server">
        <LayoutTemplate>
            <layouttemplate>
 <table border="1" cellpadding="1" style="width:800px">
  <tr style="background-color:#E5E5FE">
   <th>ID</th>
   <th>ProductCode</th>
   <th>Product Title</th>
   <th>RRP $</th>
   <th>Quantity</th>
   <th>Sale Price $</th>
  </tr>
  <tr id="itemPlaceholder" runat="server"></tr>
 </table>
</layouttemplate>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td style="width: 50px;">
                    <%#Eval("ProductID") %>
                </td>
                <td>
                    <%#Eval("ProductCode") %>
                </td>
                <td>
                    <%#Eval("ProductTitle") %>
                </td>
                <td>
                    <%#Eval("USListPrice") %>

                </td>
                <td style="width: 50px;">
                    <asp:TextBox ID="txtQuantity" runat="server" Text="0" />
                </td>
                <td style="width: 50px;">
                    <asp:TextBox ID="txtSalePrice" runat="server" Text="0.00" />
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>
    <asp:Button ID="btnBuy" Text="Buy These Items" runat="server" OnClick="btnBuy_Click" />
    <asp:Button ID="btnClear" Text="Clear Existing Sales" runat="server" 
        onclick="btnClear_Click"  />
    </form>

文件背后的代码发生了很多事情,我不希望任何人通过它,但是我如何从 txtQuantity 收集数据是通过以下代码行完成的:

Int32 quantity = Int32.Parse(((TextBox)item.FindControl("txtQuantity")).Text);

所以我想做的就是将此文本框设置为空或归零。

感谢您的帮助。

【问题讨论】:

  • 首先您应该检查 txtQuantity 和 txtSalePrice 是否设置在 ListView 的绑定事件处理程序中的任何位置。我可以想象在内部有一个订单对象,它保存了数量并且在提交后绑定回列表视图,但是在不知道代码的情况下无法说出发生了什么。
  • 此外,您实际上会在提交订单后将用户重定向到确认页面/订单状态页面。
  • @ovm,谢谢回复,我去看看。另外,如果数据已成功提交,我的页面上有一个标签会显示给用户,但是我在这里取出它只是为了显示更相关的代码位。

标签: c# forms textbox code-behind


【解决方案1】:

因为txtQuantity 控件位于ListView 中,所以可以生成任意数量的该控件实例。所以你不能通过单个变量访问所有这些。

您将需要查看该 ListView 中的所有控件(以及几个级别的深度)以找到所有 txtQuantity 控件。

txtSalePrice 控件当然也一样。

编辑
你可以找到那些带有代码的文本框(未经测试)

public IEnumerable<TextBox> FindTextBoxes(Control parent)
{
   if (parent == null) yield break;

   foreach (Control child in parent.Controls)
   {
      TextBox tb = child as TextBox;
      if (tb != null)
         yield return tb; // found one!
      else
         foreach(TextBox tb in FindTextBoxes(child))
            yield return tb; // found it deeper
   }
}

然后这样称呼它:

foreach(TextBox tb in FindTextBoxes(lbProducts)
{
   if (tb.Name == "txtQuantity")
   {
     // found a quantity 
   }
   else if (tb.Name == "txtSalePrice")
   {
     // found the salesprice
   }
}

【讨论】:

  • 我认为这正是发生在我身上的事情。你知道我是怎么做到的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
相关资源
最近更新 更多