Alex Castillo Alex Castillo - 10 days ago 6
C# Question

Get TextBox Text value in a button

I'm trying to get the value of

txt_MRP
in the codebehind but it keeps giving me


The name "txt_MRP" doesn't exist in this current context


Why is this reference not available? How can I get it?

(The button that I am pressing is
btn_add_to_cart_Click
)



<div class="row">

<div class="col-lg-2"></div>
<div class="col-lg-4">

<asp:DataList ID="DataList3" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<ul id="etalage">
<li>
<img id="Img1" class="etalage_thumb_image" runat="server" src='<%# Eval("Fotografia1") %>' />
<img id="Img2" class="etalage_source_image" runat="server" src='<%# Eval("Fotografia2") %>' />

</li>


</ul>
<br />
</ItemTemplate>
</asp:DataList>
</div>
<div class="col-lg-6">

<asp:DataList ID="DataList1" runat="server" DataKeyField="ID" DataSourceID="SqlDataSource1" >



<ItemTemplate>
<div id="product_name_width">
<asp:Label ID="Product_nameLabel" runat="server" CssClass="product_name" Text='<%# Eval("Nombre") %>' />
<br />
</div>
<span id="rs">₡</span><asp:Label ID="MRPLabel" CssClass="price" runat="server" Text='<%# Eval("Precio") %>' />
<br />
<br />
<span id="brand_name">Marca</span>
<asp:Label ID="BrandLabel" runat="server" CssClass="brand" Text='<%# Eval("Marca") %>' />
<br />

</div>

<div class="form-group">
<label> Cantidad </label>
<asp:TextBox CssClass="form-control" ID="txt_MRP" runat="server" ></asp:TextBox>


</div>

<div id="buy_now_css" runat="server">
<ul>
<li>
<asp:Button ID="btn_add_to_cart" CssClass="btn btn-primary" runat="server" Text="Añadir al Carrito" OnClick="btn_add_to_cart_Click" />
</li>
<li>
<asp:Button ID="btn_buy_now" CssClass="btn btn-success" runat="server" Text="Comprar" OnClick="btn_buy_now_Click" />
</li>
</ul>
</div>
</ItemTemplate>
</asp:DataList>

<asp:DataList ID="DataList2" runat="server" DataKeyField="ID" DataSourceID="SqlDataSource1">
<ItemTemplate>
<h2>Descripcion</h2>
<div id="Description_width">
<asp:Label ID="DetailLabel" CssClass="description" runat="server" Text='<%# Eval("Descripcion") %>' />
</div>

</ItemTemplate>
</asp:DataList>

</div>
</div>

Answer

txt_MRP does not exist because it is in a DataList Control. It is only available inside the scope of the Control. If you want to get the value you have to use another method.

Change the button event from Onclick to OnCommand and add a CommandName and set the ItemIndex as CommandArgument.

<asp:Button ID="btn_add_to_cart" CommandName="addToCart" CommandArgument='<%# Container.ItemIndex %>' CssClass="btn btn-primary" runat="server" Text="Añadir al Carrito" OnCommand="btn_add_to_cart_Command"  />

And in code behind

protected void btn_add_to_cart_Command(object sender, CommandEventArgs e)
{
    //check the correct commandname
    if (e.CommandName == "addToCart")
    {
        //get the row number from the commandargument
        int itemIndex = Convert.ToInt32(e.CommandArgument);

        //find the texbox in the correct row with findcontrol
        TextBox textBox = DataList1.Items[itemIndex].FindControl("txt_MRP") as TextBox;

        //do stuff with the value
        Label1.Text = textBox.Text;
    }
}