locked
usefulness of "Handles GridView1.DataBound" part RRS feed

  • Question

  • User-427368358 posted

    Hi

    what's the usefulness of "handles GridView1.DataBound " in  Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound?

    I tried with and without it and i coudln't notice any difference.

    Thanks

    Raf

    Sunday, November 15, 2020 11:43 AM

Answers

  • User-427368358 posted

    Hi

    i found my error. In edit mode, the label can't be found because it's in normal mode.

    Thanks

    Raf

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 17, 2020 1:36 PM

All replies

  • User475983607 posted

    what's the usefulness of "handles GridView1.DataBound " in  Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound?

    I tried with and without it and i coudln't notice any difference.

    We can only see the code you share on the forum.  What did you try?  What difference do you expect?  

    The openly published documentation states the event fires after the server control binds to a data source?   If removing this event does not affect your code then I assume you have another event that does the same thing.  

    Sunday, November 15, 2020 12:17 PM
  • User-427368358 posted

    Thangs for replying. I try to understand the usefulness of the last part of event procedure.
    I wrote there was no difference, but here i notice differences.

    Lets' see that the purpose is to put the value "ok" in a cell of a gridview connected to a datasource.
    For both cases, this is the content of the aspx page: a gridview connected to a sqldatasource and this remplates:

    <asp:TemplateField >
    <EditItemTemplate>
    <asp:TextBox ID="mail" runat="server" Text='<%# Bind("m") %>' ></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="maillabel" runat="server" Text='<%# Bind("m") %>'></asp:Label>
    </ItemTemplate>

    Code-behind:

    case 1: this gives cell gets value "ok" in normal mode; edit mode gives an error "bject reference not set to an instance of an object".
     
      Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
      Dim m As Label = CType(e.Row.FindControl("mailllabel"), Label)
      If e.Row.RowType = DataControlRowType.DataRow Then
       m.Text = "ok"
      End If
     End Sub
    --------------------------
    case 2: here nothing happens: "ok" is not put into the cell and no error in edit mode

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    Dim m As Label = CType(e.Row.FindControl("maillabel"), Label)
    If e.Row.RowType = DataControlRowType.DataRow Then
    m.Text = "ok"
    End If
    End Sub

    So, it seems that Handles GridView1.RowDataBound makes a difference. So what is it?
    And if you could tell me how to solve my problem with putting "ok" in the cells with no error in edit mode?
    Thanks
    Raf

    Sunday, November 15, 2020 7:32 PM
  • User-939850651 posted

    Hi raggarin,

    <asp:TemplateField >
    <EditItemTemplate>
    <asp:TextBox ID="mail" runat="server" Text='<%# Bind("m") %>' ></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="maillabel" runat="server" Text='<%# Bind("m") %>'></asp:Label>
    </ItemTemplate>

    Code-behind:

    case 1: this gives cell gets value "ok" in normal mode; edit mode gives an error "bject reference not set to an instance of an object".
     
      Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
      Dim m As Label = CType(e.Row.FindControl("mailllabel"), Label)
      If e.Row.RowType = DataControlRowType.DataRow Then
       m.Text = "ok"
      End If
     End Sub

    According to your description, I tested the code you provided and I got this problem. I checked the code carefully and found:

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

    Dim m As Label = CType(e.Row.FindControl("mailllabel"), Label)

    The control ID error caused your current problem, please check it.

    Best regards,

    Xudong Peng

    Monday, November 16, 2020 8:50 AM
  • User-427368358 posted

    Hi 

    thanks for replying. I checked the label name and they are correct (both maillabel). Sorry, I made an error when copying the code or something. So that's not the problem. Now, I show you the sqldatasource because maybe it has something to do with my questions:

      <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:lening %>" 
                DeleteCommand="DELETE FROM [lening] WHERE [leningnr] = @leningnr" 
                SelectCommand="SELECT * FROM [lening] order by enddat" 
                 UpdateCommand="UPDATE [lening] SET [naam] = @naam where leningnr=0" >
                <DeleteParameters>
                    <asp:Parameter Name="leningnr" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="naam" Type="String" />
                 </UpdateParameters>
               </asp:SqlDataSource>

    As you can see, the real UPDATE operation doesn't occur here (the condition "WHERE [leningnr] = @leningnr" is never true)  but in code-behind, because i wanted to check every entry (not empty, some vaues etc ..) before updating. I know it's possible to do that in javascript but that's another story. This UPDATE line must be present here because otherwise, the event GridView1_RowUpdating(ByVal doesn't fire.

    Thanks

    Monday, November 16, 2020 9:53 AM
  • User-427368358 posted

    Before you answer, I tried to do it like this: updating from aspx page and not from code-behind:

     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:lening %>" 
                DeleteCommand="DELETE FROM [lening] WHERE [leningnr] = @leningnr" 
                SelectCommand="SELECT * FROM [lening] " 
     UpdateCommand="UPDATE [lening] SET m=@m WHERE [leningnr] = @leningnr">
                   <DeleteParameters>
                    <asp:Parameter Name="leningnr" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                     <asp:Parameter Name="m" Type="String" />
                   </UpdateParameters>
               </asp:SqlDataSource>

    I get in both cases just the same result as when updating from code-behind.

    Monday, November 16, 2020 10:15 AM
  • User-939850651 posted

    Hi raffarin,

    I tried the method you described to update the record data in the grid view, but did not encounter the problem you mentioned. The RowUpdating() function can be executed correctly. I am not sure what issue you encountered.

    So if possible, please provide a complete sample code of the page and behind, and also table data for testing. This can help us better reproduce your problem and find out the cause of the problem

    Best regards,

    Xudong Peng

    Tuesday, November 17, 2020 8:21 AM
  • User-427368358 posted

    Hi

    i found my error. In edit mode, the label can't be found because it's in normal mode.

    Thanks

    Raf

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 17, 2020 1:36 PM
  • User475983607 posted

    i found my error. In edit mode, the label can't be found because it's in normal mode.

    Um yeah, I mentioned that in your other thread with the same subject.

    Tuesday, November 17, 2020 3:04 PM
  • User-427368358 posted

    Right. Thanks for that.

    Thursday, November 19, 2020 10:57 AM