-
ASP.NET 2 Textbox value in Gridview row
Hi,
I have written a page whose content includes a gridview. This gridview will display the shopping cart of my web app. I have set the columns and one column is a template field set as follows:
<ItemTemplate>
<asp:TextBox runat="server" id="txtChangeQuantity" text='<%# Eval("Quantity") %>'>
</ItemTemplate>
The gridview is bound to the "cart" DataTable (included in the Profile in web.config)
I want to accomplish the following: When the user changes the quantity in the "txtChangeQuantity" textbox of a row and clicks on a button next to it (the next cell), the Profile cart gets updated with the new quantity. However, even though I have tried many ways to get the NEW value that the user types in the quantity text box, I always get either an empty string or the databound original value. I bind the gridview in the Page_Load method checking that it is not a postback i.e. "if not page.ispostback then BIND GRIDVIEW".
I have tried to get hold of the user typed value in various code-behind methods (e.g. rowupdating), using the following generic syntax:
dim tx as string = CType(e.row(index).FindControl("txtChangeQuantity"),textbox).Text or even CType(e.row(index).Cells(4).FindControl("txtChangeQuantity"),textbox).Text
and also tried through client side javascript (which I found on the internet). However I always get an empty string "" or at most the original unedited value. I've tried to put autopostbacks and disabled/enabled the viewstate of the gridview but to no avail.
Now I am starting to wonder if this type of functionality is possible if the gridview is not set as EDITABLE (i.e. with the "Edit" and "Update" buttons).
If it is possible to set the 'txtChangeQuantity' text box as editable and I can get hold of the newly typed value (after a button click in the same row or other method), I would greatly appreciate any help in knowing the method!! Or maybe what I should add/remove in the gridview code....
Thanks very much in advance,
Timothy
-
I think you want to bind the gridview even if it is a postback, in order to update the grid when the user changes the quantity. Try removing the "If Not IsPostBack" test from Page_Load.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Hi Phil,
Thanks for your reply. I have tried that "out of desperation" previously. It gives the values that are present in the textboxes by default (i.e the unedited values taken from the current "Cart" in the Profile). I do not want the bound values but the ones that the user entered in the textbox. I still do not see how I can accomplish this!
Thanks and best regards,
Timothy
-
You might take a look at the example in the documentation of the BoundColumn's DataFormatString property and see if it helps: http://msdn2.microsoft.com/en-us/lib...matstring.aspx
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Thanks Phil for your reply...however I could not get it to work...I found a workaround though...using a startupscript which forced the onchange in the textbox, to do a window.location.href='SamePage.aspx' with the querystring containing the product ID (primary key) and the new quantity value typed in the textbox, which was then handled in the same page's Page_Load.
-
Another workaround...
This is kludgy at best, but it does allow one to use RowUpdating event and get the latest values. Find the control from the row, take the HTTP equivalent of its ClientID property and pluck the new value off the raw HTTP request...
protected void grdWorkUnits_RowUpdating(object sender, GridViewUpdateEventArgs e) {
GridViewRow gvr = grdWorkUnits.Rows[grdWorkUnits.EditIndex];
DropDownList dept = (DropDownList)gvr.FindControl("ddlDepartment");
DropDownList user = (DropDownList)gvr.FindControl("ddlUser");
DropDownList queue = (DropDownList)gvr.FindControl("ddlQueue");
CheckBox active = (CheckBox)gvr.FindControl("chkActive");
string deptVal = Request.Params[dept.ClientID.Replace("_","$")];
string userVal = Request.Params[user.ClientID.Replace("_", "$")];
string queueVal = Request.Params[queue.ClientID.Replace("_", "$")];
string activeVal = Request.Params[active.ClientID.Replace("_", "$")];
// -----------------------------------
// save data here
// -----------------------------------
// take us out of edit mode
grdWorkUnits.EditIndex = -1;
// rebind the GridView to see the changes
WorkUnitController wuc = new WorkUnitController();
DataTable dt = wuc.List(false, true);
grdWorkUnits.DataSource = dt;
grdWorkUnits.DataBind();
}
Like I said, not exactly the best approach, but it does work for any type of GridView column (DataBound, Template, etc) and any control.
-
Problem was with master page!!
Hi Brockweaver...
Thanks for your reply...but in the process of solving a similar issue, I think I found the solution to this one. Problem was that in the masterpage.master that I am using I had a redundant <form action="" method="post"> (which was there from the template I downloaded). This was causing problems whenever I use the master page: In this case the textboxes kept their initial value as they were not posting back to the <form> with the runat="server" attribute.
Timothy
-
What did you do with masterpage?
Hi Tim,
I am facing the same problem & almost spent 2 days eating my brain...
Can you pls. explain what changes you did in the master page?
Thanks
-
gridview Casting
---------------------------------------------------------
Dim cal As Calendar = DirectCast(FormView1.FindControl("Calendar1"), Calendar)
Dim txt As TextBox = DirectCast(FormView1.FindControl("txt_date"), TextBox)
txt.Text = Format(cal.SelectedDate, "dd-MMM-yyyy")
----------------------
using above code i can write data from a calendar control to textbox control in a gridview ....
regards
Similar Threads
-
By seasider in forum ASP.NET
Replies: 7
Last Post: 12-03-2008, 03:03 PM
-
By Rathi in forum ASP.NET
Replies: 3
Last Post: 05-12-2006, 12:03 AM
-
By clean in forum ASP.NET
Replies: 1
Last Post: 02-13-2006, 02:56 PM
-
By mycwcgr in forum ASP.NET
Replies: 0
Last Post: 10-11-2005, 08:12 AM
-
By ASPSmith Training in forum dotnet.announcements
Replies: 0
Last Post: 06-18-2002, 03:39 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|