#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    20
    Rep Power
    0

    Angry e.Item.Cell[x].Text always empty?


    I've been working on this for an entire day and have not given up hope!

    I've created a DataGrid which can go into edit mode, and I'm trying to (for now) display the text of a cell which was just updated. Once this works, I'll be able to send the value to the database, but so far, the values are always blank...

    My update function currently just spits out the value into another textbox for testing purposes... so far the text is always empty! I can only get it to display the index number and datakeyfield for now.

    public void btnUpdate_Click(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e){
    for (int x=0; x<e.Item.Cells.Count;x++)
    {
    txtNewProductClass.Text+=e.Item.Cells[x].Text +",";
    }
    txtNewProductClass.Text += " key: " + productClassGrid.DataKeys[e.Item.ItemIndex].ToString();
    }

    I've tested the following values from the event:

    e.Item.Count gives the correct count of 3 columns.
    e.Item.ItemIndex gives the correct index number of the clicked row.
    e.Item.Cells[x].Text is always empty for the row I click on, but the form is full of data??? I need the new updated value so I can put it into the database.


    Here is my ASPX Form definition:
    <aspataGrid runat="server" datakeyfield="product_class_id" ID="productClassGrid" onupdatecommand="btnUpdate_Click" onCancelCommand="btnCancel_Click" OnEditCommand="btnEdit_Click" AutoGenerateColumns="false">
    <HeaderStyle CssClass="TableCategoryHeader"></HeaderStyle>
    <columns>
    <asp:boundcolumn ItemStyle-Width="25px" DataField="product_class_id" HeaderText="ID#" SortExpression="product_class_id"/>
    <asp:boundcolumn ItemStyle-Width="300px" DataField="product_class_name" HeaderText="Product Class" SortExpression="product_class_name" />
    <asp:editcommandcolumn HeaderText="Command" ButtonType="PushButton" EditText="Edit" CancelText="Cancel" UpdateText="Update" runat="server" />
    </columns>
    </aspataGrid>

    <asp:label id="lblNameValidation" runat="server" ForeColor="#FF0000">Please Enter a Class!<BR></asp:label>
    <asp:TextBox id="txtNewProductClass" runat="server"/>

    Thank you in advance, help is greatly appreciated.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    1
    Rep Power
    0
    You might do a quickwatch on e.Item.Cells[x] to see if the text might be in a literal or label child control of the cell...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    20
    Rep Power
    0

    Something just isn't right here...


    Wish I knew how, unfortunately I'm not using VS.Net, just text viewing through Dreamweaver MX and compiling using Csc.exe

    But Perhaps you could tell me a programatical way to display this information?

    The columns from which I'm extracting the data are bound to data from the database, when I enter edit mode, the selected row changes to text boxes plus the third column for the buttons.

    somewhere i found the following being used:
    <EditItemTemplate>
    <asp:TextBox id=txtNewsContent runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.NewsContent") %>' TextMode="MultiLine" Height="101px" Width="205px">
    </asp:TextBox>
    </EditItemTemplate>

    is it possible that the Cell[x].Text property of a bound column is not set? How else would I get at this data, especially since the edit mode still seems to work, and the e.Item.ItemIndex returns the correct selected value...

    I'm stumped.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    America
    Posts
    163
    Rep Power
    11
    Try this way,
    Dim strContent As String = CType(e.Item.FindControl("txtNewsContent"), TextBox).Text

    hth

    rhys
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    20
    Rep Power
    0

    Question boundcolumn vs textbox when accessing updated data


    Unfortunately I'm not currently using the code (I just thought I'd mention it as an alternative in case there is a difference between this way and the way I do it...):
    <asp:TextBox id=txtNewsContent runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.NewsContent") %>' TextMode="MultiLine" Height="101px" Width="205px">

    VS

    <asp:boundcolumn DataField="product_class_name" HeaderText="Product Class" SortExpression="product_class_name" />
    <asp:editcommandcolumn HeaderText="Command" ButtonType="PushButton" EditText="Edit" CancelText="Cancel" UpdateText="Update" runat="server" />


    I have a DataGrid defined with boundcolumns not textboxes... when I click edit and enter edit mode, the data is spit out in the bound columns and the editable line turns to input boxes, (its all automatic), but they don't exactly have an id or name to reference them with... so I don't know what to search for using FindControl nor do I know whether it really is a textbox... but e.Item.Cells[0].Text is empty...

    Any DataGrid masters out there?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    20
    Rep Power
    0

    Cool Solution!


    Alright I figured it out after hours of searching the net...

    TextBox myTextBox = (TextBox) e.Item.Cells[1].Controls[0];
    the value of the cell is in myTextBox.Text...

    Now funny how I've been searching through the WebMatrix Class browser and e.Item.Cells[] which is of type TableCellCollection doesn't show a Controls property or method... how was I supposed to find this?

    Also, how can I determine that a cell actually contains a control, its type, and that it is at [0]?

    Cheers.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Annapolis, MD
    Posts
    1
    Rep Power
    0

    Another Solution!


    Originally Posted by tniedoba
    Alright I figured it out after hours of searching the net...

    TextBox myTextBox = (TextBox) e.Item.Cells[1].Controls[0];
    the value of the cell is in myTextBox.Text...

    Now funny how I've been searching through the WebMatrix Class browser and e.Item.Cells[] which is of type TableCellCollection doesn't show a Controls property or method... how was I supposed to find this?

    Also, how can I determine that a cell actually contains a control, its type, and that it is at [0]?

    Cheers.
    By Default, Bound columns are read/write and the in-line editing code automatically replaces the cells with text boxes for the item when you click the edit link. If you don't want to edit all the columns, just set the "ReadOnly" property to True for each column you don't want to edit. Then the value of the cell will be available in e.Item.Cells[x].Text

    Comments on this post

    • Ronster disagrees : You "woke up" an 8 year old question. That is not good manners.

IMN logo majestic logo threadwatch logo seochat tools logo