ASP.NET 2 Textbox value in Gridview row


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: ASP.NET 2 Textbox value in Gridview row

Hybrid View

  1. #1
    Join Date
    Jun 2004
    Location
    Malta, Europe
    Posts
    50

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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!

  3. #3
    Join Date
    Jun 2004
    Location
    Malta, Europe
    Posts
    50
    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

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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!

  5. #5
    Join Date
    Jun 2004
    Location
    Malta, Europe
    Posts
    50
    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.

  6. #6
    Join Date
    Dec 2006
    Posts
    1

    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.

  7. #7
    Join Date
    Jun 2004
    Location
    Malta, Europe
    Posts
    50

    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

  8. #8
    Join Date
    Nov 2007
    Posts
    1

    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

  9. #9
    Join Date
    Mar 2007
    Posts
    2

    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

  1. ASP.NET 2.0 GridView nightmare!!!
    By seasider in forum ASP.NET
    Replies: 7
    Last Post: 12-03-2008, 04:03 PM
  2. Replies: 3
    Last Post: 05-12-2006, 01:03 AM
  3. Replies: 1
    Last Post: 02-13-2006, 03:56 PM
  4. Replies: 0
    Last Post: 10-11-2005, 09:12 AM
  5. Free ASP.NET Web Matrix Design/Editor Tool Released
    By ASPSmith Training in forum dotnet.announcements
    Replies: 0
    Last Post: 06-18-2002, 04: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center