标签返回datalist中的空值

时间:2015-09-22 19:03:38

标签: c# asp.net

我有一个带有一个标签,文本框和一个按钮的数据列表。在按钮上单击我想要一个文本框和标签的值,我得到文本框的值但标签是空的。如何解决这个问题呢。 Thanyou

protected void btnAdd_Click(object sender, EventArgs e)
{
    foreach (DataListItem item in rptrItems.Items)
    {
        string quantity = ((TextBox)item.FindControl("txtQuantity")).Text;
        string itemName =((Label)item.FindControl("itemName")).Text;
    }
}



 <asp:DataList ID="rptrItems" runat="server" RepeatColumns="3" >

                       <ItemTemplate> 
                         <div class="panel panel-info">
                           <div class="panel-heading"> Item= <asp:Label ID="itemName" runat="server"> <strong>"<%#Eval("ItemName")%>"</strong></asp:Label></div>
                           <div class="panel-body">
                              <div class="row">
                                <label class="lblQuantity"> Quantity:  </label>
                                <asp:TextBox ID="txtQuantity" runat="server" CssClass="form-control"></asp:TextBox>  
                               </div>
                              <div class="row">
                                <asp:Button ID="btnAdd" runat="server" CssClass="btn-Add" OnClick="btnAdd_Click" /> </div>                                  
                            </div>
                          </div>
                       </ItemTemplate>

                        </asp:DataList>

2 个答案:

答案 0 :(得分:1)

只有表单元素将其值发布到服务器,因此标签不会发布其值。这就是为什么你无法获得其Text值的原因。请改为使用隐藏的输入元素。

在这里查看HiddenField元素:

<asp:DataList ID="rptrItems" runat="server" RepeatColumns="3" >
    <ItemTemplate> 
     <div class="panel panel-info">
       <div class="panel-heading"> 
            Item= <asp:Label ID="itemName" runat="server"> <strong>"<%#Eval("ItemName")%>"</strong></asp:Label>
            <asp:HiddenField ID="itemName" runat="server" Value='<%# Eval("ItemName") %>' />
       </div>
       <div class="panel-body">
          <div class="row">
            <label class="lblQuantity"> Quantity:  </label>
            <asp:TextBox ID="txtQuantity" runat="server" CssClass="form-control"></asp:TextBox>  
           </div>
          <div class="row">
            <asp:Button ID="btnAdd" runat="server" CssClass="btn-Add" OnClick="btnAdd_Click" /> </div>                                  
        </div>
      </div>
    </ItemTemplate>
</asp:DataList>

在您的代码后面访问它:

protected void btnAdd_Click(object sender, EventArgs e)
{
    foreach (DataListItem item in rptrItems.Items)
    {
        string quantity = ((TextBox)item.FindControl("txtQuantity")).Text;
        string itemName =((HiddenField)item.FindControl("itemName")).Value;
    }
}

答案 1 :(得分:1)

Per Canavar评论使用隐藏字段

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rptrItems.DataSource = new List<Item>
            {
                new Item {ItemName = "Item Name",Quantity=1}
            };
            rptrItems.DataBind();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        foreach (DataListItem item in rptrItems.Items)
        {
            string quantity = ((TextBox)item.FindControl("txtQuantity")).Text;
            string itemName = ((HiddenField)item.FindControl("itemName")).Value;
        }
    }

    public class Item
    {
        public string ItemName { get; set; }

        public int Quantity { get; set; }
    }

 <asp:DataList runat="server" ID="rptrItems">
<ItemTemplate>
    <asp:HiddenField ID="itemName" runat="server" Value='<%# Eval("ItemName") %>' />
    <asp:Label ID="labItemName" runat="server" Text='<%# Eval("ItemName") %>' /> : 
    <asp:TextBox ID="txtQuantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="btnSubmit" OnClick="btnAdd_Click" runat="server" Text="Submit" />