Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get textbox value from textbox in itemtemplate

Tags:

c#

asp.net

The quantity (quantityWanted in DB) in textbox is loaded via Eval() method from Basket DB table. What I want to achieve is that when I change quantity manually and click update the quantity for that record will be updated and then grid will be reloaded. I seem unable to retrieve value of that textbox in code behind.

I am aware of FindControl() method which is used to get value from controls within itemtemplate but I don't know how to use it here.

The tried below but always get nullReferenceException

TextBox txt = (TextBox)GridView2.FindControl("txtQuantityWanted");
int _quantity = Convert.ToInt16(txt.Text);

Note: button is there but does nothing.

enter image description here

<ItemTemplate>        
    <asp:TextBox runat="server" ID="txtQuantityWanted" Text='<%# Eval("quantityWanted") %>' ></asp:TextBox>
    <asp:LinkButton ID="LinkButton11" runat="server" CommandName="update" CommandArgument='<%# Eval("coffeeName") + ";" + Eval("datetimeAdded")  %>' >Update</asp:LinkButton>
    <asp:Button ID="Button21" runat="server" Text="Button" CommandName="edit" />
</ItemTemplate> 

<asp:TemplateField HeaderText="Total [£]">
    <ItemTemplate>
        <asp:Label id="lblItemTotal" runat="server" Text='<%# String.Format("{0:C}", Convert.ToInt32(Eval("quantityWanted"))* Convert.ToDouble(Eval("price"))) %>' ></asp:Label> 
        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="remove" CommandArgument='<%# Eval("coffeeName") + ";" + Eval("datetimeAdded") %>' >Remove</asp:LinkButton>
    </ItemTemplate> 
</asp:TemplateField>

C# code:

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // .....
    else if (e.CommandName == "update")
    {
        string params = Convert.ToString(e.CommandArgument);
        string[] arg = new string[2];
        arg = params.Split(';');
        name = Convert.ToString(arg[0]);
        datetimeAdded = Convert.ToString(arg[1]);

        const string strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=L:\ASP.NET\Exercise1\Exercise1\Exercise1\App_Data\ASPNETDB.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";

        DataSet ds = new DataSet("Employees");
        SqlConnection connection = new SqlConnection(strConn);

        // Here I need value from textbox to replace 11
        SqlCommand abc = new SqlCommand("UPDATE Basket SET quantityWanted = 11 WHERE coffeeName LIKE '%" + name + "%' AND datetimeAdded LIKE '" + datetimeAdded + "' ", connection);
        connection.Open();
        int ii = abc.ExecuteNonQuery();
        connection.Close();
    }
}
like image 546
JanT Avatar asked Jan 14 '23 23:01

JanT


1 Answers

Use GridView.Rows collection to find control. You can pass the index of row in rows collection indexer.

TextBox txt = (TextBox)GridView2.Rows[0].FindControl("txtQuantityWanted");
like image 81
Adil Avatar answered Jan 19 '23 11:01

Adil