Page view counter

GridView controls - VS 2005 Web

Last post 06-19-2007 10:36 PM by Techie Zhang - MSFT. 10 replies.

Sort Posts:

  • GridView controls - VS 2005 Web

    06-15-2007, 7:44 AM
    • Loading...
    • Scheinin
    • Joined on 04-03-2007, 9:25 PM
    • Seattle, WA
    • Posts 71
    Question 1
     
    In the RowEditing event, I need to test the length of  the contents of a BoundField.  The only way I can find that works is to reference the Cell number:
     
    Len(gv.Rows(e.NewEditIndex).Cells(8).Text) <> 1
     
    Isn't there a better way that does not depend on specifying the cell number?  If I add, move or delete a cell, I need to remember to change the cell number in code such as this.  It would be better to use the Bound Fields Header Text but I can't find any syntax that works.
     
     
     
    Question 2
     
    In the RowUpdating event, I need to compare the current value of a TextBox in an EditItemTemplate with the original value.  Using

    e.OldValues("CurrentComment").ToString()  does not work.   I tried capturing the old value in the RowEditing event using a Public variable, but it does not survive in the

    RowUpdating event.  How is this done?

     
  • Re: GridView controls - VS 2005 Web

    06-18-2007, 2:36 AM

    Hi:

    Answer1:

    You have two choices:

    1. Use ItemTemplate instead and in code behind, use FindControl to get the instance.

    2. I'm not sure why you do this in RowEditing, what about address it in RowUpdating? Use e.NewValues("field_name") to get the value to be updated and add some logic to change it.

    Andswer2:

    Are you using two-way binding?

    Text= '<%# Bind("field_name")%>'

    Are you using standard DataSource such as SqlDataSource, ObjectDataSource, etc?

     

    If it doesn't solve your problem, please post your code.

    Thanks

     

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: GridView controls - VS 2005 Web

    06-18-2007, 3:17 AM

    Hi, Scheinin :

    Based on my knowledgement, BoundField only can be accessed by the cell's index. But you do not want to access a column by the hard-coded number, you are suggested to use Label to display the data, instead of BoundField. You can get the content by the Label's ID, using the method FindControl().

    To the second question, oldvalues can be retrieved by the field keys. Your statement is correct, but please note that the field key is not the HeaderText of the column, but the field in DataBase. For example, if your SQL is "SELECT name from Table1", you can use e.OldValues("name").ToString() to get the value.

    Please have a try.

    If you have any doubts about it, please feel free to let me know.

    Sincerely,
    Techie Zhang
    Microsoft Online Community Support
  • Re: GridView controls - VS 2005 Web

    06-18-2007, 4:29 AM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 7:10 AM
    • Croatia
    • Posts 1,681

    You can solve your first problem in RowDataBound event (not in RowEditing) like this:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
      if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) ||
       (e.Row.RowState == DataControlRowState.Edit))

      {
       string column = DataBinder.Eval(e.Row.DataItem, "columnName").ToString();
      }
     }

    where columnName is name of your database column.

  • Re: GridView controls - VS 2005 Web

    06-18-2007, 9:47 PM
    • Loading...
    • Scheinin
    • Joined on 04-03-2007, 9:25 PM
    • Seattle, WA
    • Posts 71

    When the user clicks the AutoGenerated Edit hyperlink,  I need to take some action based on the length of a BoundField, so the RowDataBound event won't help me here.  

  • Re: GridView controls - VS 2005 Web

    06-18-2007, 10:04 PM
    • Loading...
    • Scheinin
    • Joined on 04-03-2007, 9:25 PM
    • Seattle, WA
    • Posts 71

    I can't use ItemTemplate because I need to get e.OldValue, which does not appear to be available for a TextBox in an ItemTemplate, only in a BoundField.

    I'm using RowEditing because, when the user clicks the AutoGenerated Edit hyperlink, I need to take some action before the user can edit the data. 

    Answer 2: No. The field in question is not a database field.  I insert a value during the RowDataBound event and need to see if it has changed in the RowUpdating event.

  • Re: GridView controls - VS 2005 Web

    06-18-2007, 10:09 PM
    • Loading...
    • Scheinin
    • Joined on 04-03-2007, 9:25 PM
    • Seattle, WA
    • Posts 71

    In a gridview, there is no <asp:Label

    I did not know about your second point.  Since my HeaderText is usually the name of the database field, I did not realize what was happening.  Thanks.

    Unfortunately, in this case, the field in question is not a database field but one that I fill during the RowDataBound event and then need to see if it changed in the RowEditing event.  I worked around it by creating a separate Hidden field to store the original value

  • Re: GridView controls - VS 2005 Web

    06-18-2007, 10:26 PM

    Hi,Scheinin:

    If you are not using a 'standard' datasource New/OldValues are not always available in the RowUpdating event.

    You are right to save the old values in RowEditing event handler, but you should save them in Session or ViewState.

    Please try this:

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
           ViewState["CurrentComment"] = GridView1.Rows[e.NewEditIndex].Cells[8].Text;
            //
        }

     Hope it helps.

    Sincerely,
    Techie Zhang
    Microsoft Online Community Support
  • Re: GridView controls - VS 2005 Web

    06-19-2007, 2:20 AM
    • Loading...
    • kipo
    • Joined on 07-20-2006, 7:10 AM
    • Croatia
    • Posts 1,681

    Can you post your code, it would be much easier to solve your problems that way?

  • Re: GridView controls - VS 2005 Web

    06-19-2007, 4:34 PM
    • Loading...
    • Scheinin
    • Joined on 04-03-2007, 9:25 PM
    • Seattle, WA
    • Posts 71

    Is there any advantage/disadvantage to using Session, ViewState or just a hidden field?

    The corresponding VB code would be ViewState.Add("CurrentComment", GridView1.Rows[e.NewEditIndex].Cells[8].Text) and works great.

    although I'd sure like to find a way not to have to use an absolute Cells reference. 


  • Re: GridView controls - VS 2005 Web

    06-19-2007, 10:36 PM
    Answer

    Hi,Scheinin :

    Here is an article about how to develope High-Performance ASP.NET Applications and when is neccessary to use ViewState and Session.

    Please refer to http://msdn2.microsoft.com/en-us/library/5dws599a(VS.71).aspx.

    In addtion, I want to point out that the values in ItemTemplate also can be save in OldValues and NewValues, as long as you bind the field name to the control and use standard DataSource, like:

    <ItemTemplate >

     

    <asp:Label runat="server" ID="tb" Text='<%# Bind("class") %>'></asp:Label>

    </ItemTemplate>

    <EditItemTemplate>

    <asp:TextBox runat="server" ID="MyTextBox" Text = '<%# Bind("class") %>'></asp:TextBox>

    </EditItemTemplate>

    </asp:TemplateField>

    Hope it helps.

    Sincerely,
    Techie Zhang
    Microsoft Online Community Support
Page 1 of 1 (11 items)
Microsoft Communities