Bind Radgrid on linkbutton and not pageload

9 posts, 1 answers
  1. Kevin
    Kevin avatar
    265 posts
    Member since:
    Jun 2011

    Posted 10 Apr 2012 Link to this post

    what I ahve is several drop downs at the top of my page and a linkbutton.  Once they pick what they want and then pick the link button it should fire the radgrid to load.  What I am finding out though is the radgrid wants to load on page load and it is erroring out everytime becuase it does not have the ids from the dropdownlists, how can I avoid this.  Originally I put in a fillGrid private sub  but then I loast fucntionality of paging and the grid woould never show with the editing fucntionality I have built in.

    <telerik:RadGrid ID="myRadGrid" runat="server" Width="75%" BorderWidth="1px" CellPadding="6" GridLines="None" BorderColor="#404040" Skin="Web20">
                           <ExportSettings ExportOnlyData="true" FileName="DonorSlots" IgnorePaging="true" OpenInNewWindow="true"></ExportSettings>
                           <MasterTableView AutoGenerateColumns="false" DataKeyNames="intDeployId" Name="MasterGrid" BorderColor="#404040" Font-Size="9" Font-Names="Veranda,arial,sans-serif" AllowPaging="True" 
                           PageSize="20" PagerStyle-Mode="NumericPages" HeaderStyle-HorizontalAlign="Center" GridLines="Both" BorderWidth="1px" CommandItemDisplay="Top" EditMode="EditForms">
                           <CommandItemSettings ShowAddNewRecordButton="false" ShowRefreshButton="false" ShowExportToExcelButton="false" /><AlternatingItemStyle BackColor="#B0C4DE"  HorizontalAlign="Center" />
                           <ItemStyle HorizontalAlign="Center" /><HeaderStyle ForeColor="White" Font-Bold="true" BorderColor="#404040" BorderWidth="1px" />
                               <CommandItemTemplate>
                                   <table width="100%">
                                       <tr>
                                           <td align="right"><asp:Button ID="btnExport" runat="server" CommandName="ExportToExcel" Text="Export Excel" /></td>
                                       </tr>
                                   </table>
                               </CommandItemTemplate>
                               <Columns>
                                   <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" EditText="Update"></telerik:GridEditCommandColumn>
                                   <telerik:GridTemplateColumn HeaderText="HIST">
                                       <ItemTemplate>
                                               <asp:Image ID="imgLooker" runat="server" ImageUrl="~/Images/ViewResources.gif" ImageAlign="Middle" BorderStyle="None" />
                                               <asp:PopupControlExtender ID="popInfo" runat="server" PopupControlID="pnlPop" TargetControlID="imgLooker" DynamicContextKey='<% #Eval("strSSN") %>' 
                                               DynamicControlID="pnlpop" DynamicServiceMethod="HelloWorld" Position="bottom"></asp:PopupControlExtender>
                                       </ItemTemplate>
                                   </telerik:GridTemplateColumn>
                                   <telerik:GridBoundColumn DataField="strFullname" HeaderText="NAME" />
                                   <telerik:GridBoundColumn DataField="strPrevUic" HeaderText="PRIOR UIC" />
                                   <telerik:GridBoundColumn DataField="strPara" HeaderText="PARA" />
                                   <telerik:GridBoundColumn DataField="strLine" HeaderText="LINE" />
                                   <telerik:GridBoundColumn DataField="strGrade" HeaderText="GRADE" />
                               </Columns>
                               <EditFormSettings EditFormType="Template">
                                   <FormTemplate>
                                           <table width="100%">
                                           <tr>
                                               <td style="width:50%" align="right">Prior Uic: </td>
                                               <td style="width:50%" align="left"><asp:TextBox ID="txtPriorUic" runat="server" Width="100px" Text='<%# Bind("strPrevUic") %>'></asp:TextBox></td>
                                           </tr>
                                           <tr>
                                               <td style="height:5px"></td>
                                           </tr>
                                           <tr>
                                               <td style="width:50%" align="right">Para: </td>
                                               <td style="width:50%" align="left""><asp:TextBox ID="txtpara" runat="server" Width="100px" Text='<%# Bind("strPara") %>'></asp:TextBox></td>
                                           </tr>
                                           <tr>
                                               <td style="height:5px"></td>
                                           </tr>
                                           <tr>
                                               <td style="width:50%" align="right"> Line: </td>
                                               <td style="width:50%" align="left"><asp:TextBox ID="txtLine" runat="server" Width="100px" Text='<%# Bind("strLine") %>'></asp:TextBox></td>
                                           </tr>
                                           <tr>
                                               <td style="height:5px"></td>
                                           </tr>
                                           <tr>
                                               <td style="width:50%" align="right">Return Grade: </td>
                                               <td style="width:50%" align="left"><asp:DropDownList ID="ddlRank" runat="server" CausesValidation="false" Width="105px">
                                                       <asp:ListItem Value="0">Pick Grade</asp:ListItem>
                                                       <asp:ListItem Value="E4">E4 Below</asp:ListItem>
                                                       <asp:ListItem Value="E5">E5</asp:ListItem>
                                                       <asp:ListItem Value="E6">E6</asp:ListItem>
                                                       <asp:ListItem Value="E7">E7</asp:ListItem>
                                                       <asp:ListItem Value="E8">E8</asp:ListItem>
                                                       <asp:ListItem Value="E9">E9</asp:ListItem>
                                                       <asp:ListItem Value="W1">W1</asp:ListItem>
                                                       <asp:ListItem Value="W2">W2</asp:ListItem>
                                                       <asp:ListItem Value="W3">W3</asp:ListItem>
                                                       <asp:ListItem Value="W4">W4</asp:ListItem>
                                                       <asp:ListItem Value="W5">W5</asp:ListItem>
                                                       <asp:ListItem Value="O2">O2 Below</asp:ListItem>
                                                       <asp:ListItem Value="O3">O3</asp:ListItem>
                                                       <asp:ListItem Value="O4">O4</asp:ListItem>
                                                       <asp:ListItem Value="O5">O5</asp:ListItem>
                                                       <asp:ListItem Value="O6">06</asp:ListItem>
                                                       <asp:ListItem Value="O7">O7</asp:ListItem>
                                                       <asp:ListItem Value="O8">08</asp:ListItem>
                                                   </asp:DropDownList>
                                               </td>
                                           </tr>
                                       </table>
                                       <table width="100%">
                                               <tr>
                                                   <td style="width:50%" align="right"></td>
                                                   <td style="width:50%" align="left">
                                                   <asp:LinkButton ID="lnkSubmit" runat="server" text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>' 
                                                   CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'></asp:LinkButton>
                                                         
                                                   <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                               </td>
                                           </tr>
                                       </table>
                                   </FormTemplate>
                               </EditFormSettings>
                           </MasterTableView>
                       </telerik:RadGrid>

    Protected Sub myRadGrid_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles myRadGrid.NeedDataSource
           Dim DeployId As String = Convert.ToInt16(ddlDeployment.SelectedValue.ToString())
           Dim Bn As String = ddlBn.SelectedValue
           Dim Uic As String = ddlUic.SelectedValue
           Dim SqlWhere As String = ""
           If CInt(DeployId) > 0 And Bn = "0" And ddlUic.SelectedValue = "" Then
               SqlWhere = "mu.intMobilizationId = " & ddlDeployment.SelectedValue & " "
           ElseIf CInt(DeployId) > 0 And ddlBn.SelectedValue > "" And ddlUic.SelectedValue = "0" Then
               SqlWhere = "mu.intMobilizationId = " & ddlDeployment.SelectedValue & " and mu.strAAUic = '" & ddlBn.SelectedValue & "'"
           ElseIf CInt(DeployId) > 0 And ddlBn.SelectedValue > "0" And ddlUic.SelectedValue > "0" Then
               SqlWhere = "mu.intMobilizationId = " & ddlDeployment.SelectedValue & " and mu.strAAUic = '" & ddlBn.SelectedValue & "' and mu.intUnitMobId = " & ddlUic.SelectedValue & ""
           End If
           sql = "Select up.intDeployId, up.strPrevUic, up.strPara, up.strLine, up.strGrade, mn.strFullname, up.strSSN From tblMobUnitPersonnel as up INNER JOIN tblMobUnitPosition as p on p.intPositionID = up.intPositionId " _
               & "INNER JOIN tblMOBUnits mu on mu.intUnitMobId = p.intUnitMobID INNER JOIN MnNgPersonnel.dbo.tblMNNatPersonnel mn on mn.strSSN = up.strSSN WHERE " & SqlWhere & " Order by up.strPrevUic, up.strPara, up.strLine"
           myRadGrid.DataSource = getData(sql)
       End Sub




  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 11 Apr 2012 Link to this post

    Hi Kevin,

    I guess you want to bind the RadGrid using NeedDataSource event on a LinkButton Click. Please take a look into the following code snippet.

    VB:
    Private Bind As Boolean = False
    Public Shared connection As String = WebConfigurationManager.ConnectionStrings("NorthwindConnectionString3").ConnectionString
    Private conn As New SqlConnection(connection)
    Public SqlCommand As New SqlCommand()
    Protected Sub myRadGrid_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
        If Bind Then
            Dim selectQuery1 As String = "select top 10 * from Orders"
            Dim adapter1 As New SqlDataAdapter(selectQuery1, conn)
            Dim dt1 As New DataTable()
            conn.Open()
            adapter1.Fill(dt1)
            conn.Close()
            myRadGrid.DataSource = dt1
        End If
     
    End Sub
    'Binding the RadGrid on LinkButton Click
    Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)
        Bind = True
        myRadGrid.Rebind()
    End Sub

    Hope this helps.
    Thanks,
    Princy.
  3. Kevin
    Kevin avatar
    265 posts
    Member since:
    Jun 2011

    Posted 11 Apr 2012 Link to this post

    Hi,
    yes this is what I want but the problem is that the radgird seems to want to load on page load even through I am not calling it on page load, it tries to load the radgrid and runs into my dropdownlists that have not been picked and errors out.  To resolve temporarily I placed inside update panels, so it needs a trigger to load it, but this was a temp solution to the problem of the radgrid automatically loading on page load, when it needs to only load on the click button event.

    <tr>
                           <td align="center" width="100%">
                               <asp:DropDownList id="ddlDeployment" Runat="server" Width="160" AutoPostBack="True"></asp:DropDownList>     
                               <asp:DropDownList id="ddlBn" Runat="server" Width="120" AutoPostBack="True"></asp:DropDownList>     
                               <asp:DropDownList id="ddlUic" Runat="server" Width="120" AutoPostBack="true"></asp:DropDownList>
                           </td>
                       </tr>

    Protected Sub myRadGrid_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles myRadGrid.NeedDataSource
            Dim DeployId As String = Convert.ToInt16(ddlDeployment.SelectedValue.ToString())
            Dim Bn As String = ddlBn.SelectedValue
            Dim Uic As String = ddlUic.SelectedValue
            Dim SqlWhere As String = " "
      
            If CInt(DeployId) > 0 And Bn = "0" And ddlUic.SelectedValue = "" Then
                SqlWhere = "mu.intMobilizationId = " & ddlDeployment.SelectedValue & " "
            ElseIf CInt(DeployId) > 0 And ddlBn.SelectedValue > "" And ddlUic.SelectedValue = "0" Then
                SqlWhere = "mu.intMobilizationId = " & ddlDeployment.SelectedValue & " and mu.strAAUic = '" & ddlBn.SelectedValue & "'"
            ElseIf CInt(DeployId) > 0 And ddlBn.SelectedValue > "0" And ddlUic.SelectedValue > "0" Then
                SqlWhere = "mu.intMobilizationId = " & ddlDeployment.SelectedValue & " and mu.strAAUic = '" & ddlBn.SelectedValue & "' and mu.intUnitMobId = " & ddlUic.SelectedValue & ""
            End If
      
            If CInt(DeployId) = 0 Then
                myRadGrid.DataSource = Nothing
            Else
                sql = "Select up.intDeployId, up.strPrevUic, up.strPara, up.strLine, up.strGrade, mn.strFullname, up.strSSN From tblMobUnitPersonnel as up INNER JOIN tblMobUnitPosition as p on p.intPositionID = up.intPositionId " _
              & "INNER JOIN tblMOBUnits mu on mu.intUnitMobId = p.intUnitMobID INNER JOIN MnNgPersonnel.dbo.tblMNNatPersonnel mn on mn.strSSN = up.strSSN WHERE " & SqlWhere & " Order by up.strPrevUic, up.strPara, up.strLine"
      
                myRadGrid.DataSource = getData(sql)
            End If
        End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
           If Not IsPostBack Then
               'Set the Page label
               Dim page As Label = CType(Master.FindControl("lblPage"), Label)
               page.Text = "Readiness Tracker - Manage Donor Slots"
               'check for admin rights
               If checkAdmin() = False Then
                   Response.Redirect("~/ErrorPage/NoAccess.aspx")
               Else
                   'Fill Deployment menu system
                   sql = "Select intMobilizationId, strDeployment from tblMobDeployment where bitArchive is Null order by strDeployment"
                   ddlDeployment.Items.Add(New ListItem("Pick Deployment", "0"))
                   buildDD(sql, ddlDeployment)
               End If
           End If
       End Sub







  4. Kevin
    Kevin avatar
    265 posts
    Member since:
    Jun 2011

    Posted 11 Apr 2012 Link to this post

    Hi,
    Ok I have another case of the same problem, my Radgrid is trying to load on page load, but I needs an Id of a radibuttonlist before it can load.  it errors out on page load and says my datakeys is not there and that's correct because it should not even try to load at this point as I have not even picked the radio button value.  How can I get around this.

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
         <asp:Panel ID="pnlGrid" runat="server">
            <table width="100%">
                <tr>
                    <td style="height:10px"></td>
                </tr>
                <tr>
                    <td align="center">
                        <asp:RadioButtonlist ID="rblView" runat="server" RepeatDirection="Horizontal" TextAlign="Right" RepeatLayout="Flow" Visible="false">
                            <asp:ListItem Value="1" Text="Pending Issues    "></asp:ListItem>
                            <asp:ListItem Value="2" Text="Pending ReIssues    "></asp:ListItem>
                            <asp:ListItem Value="3" Text="Pending Transfers    "></asp:ListItem>
                        </asp:RadioButtonlist>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        <telerik:RadGrid ID="myRadGrid" runat="server" Width="98%" BorderWidth="1px" CellPadding="6" GridLines="None" BorderColor="#404040" Skin="Web20">
                        <MasterTableView AutoGenerateColumns="false" DataKeyNames="intRecId" Name="MasterGrid" BorderColor="#404040" Font-Size="9" Font-Names="Veranda,arial,sans-serif" HeaderStyle-HorizontalAlign="Center" 
                        GridLines="Both" BorderWidth="1px" AllowPaging="true" PageSize="20" PagerStyle-Mode="NumericPages"><ItemStyle HorizontalAlign="Center"/>
                        <AlternatingItemStyle BackColor="#B0C4DE"  HorizontalAlign="Center" /><HeaderStyle ForeColor="White" Font-Bold="true" BorderColor="#404040" BorderWidth="1px" />
                           <Columns>
                                <telerik:GridBoundColumn DataField="strSN" HeaderText="SN" />
                                <telerik:GridBoundColumn DataField="ITEM" HeaderText="EQUIPMENT" />
                                <telerik:GridBoundColumn DataField="NAME" HeaderText="NAME" />
                                <telerik:GridBoundColumn DataField="dtIssued" HeaderText="DT_ISSUED" />
                                <telerik:GridBoundColumn DataField="intLocationId"  HeaderText="UNIT" />
                                <telerik:GridBoundColumn DataField="strIMOGroup" HeaderText="IMO" />
                            </Columns>
                        </MasterTableView>
                        </telerik:RadGrid>
                    </td>
                </tr>
            </table>
        </asp:Panel>
    </asp:Content>

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                rblView.Visible = True
            End If
        End Sub
      
        Protected Sub myRadGrid_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles myRadGrid.NeedDataSource
            Dim rbl As String = rblView.SelectedValue
      
            Select Case rbl
                Case 1
                    sql = "Select dr.intRecId, dr.strSN, mm.strManufacturer + ' ' + m.strMake + ' ' + dm.strModel ITEM, mn.strRank + ' ' + strFullname Name, Convert(varchar(10), dtIssued, 111) dtIssued, intLocationId, " _
                    & "strIMOGroup From drat_Received dr INNER JOIN Drat_Model dm on dm.intModelId = dr.intModelid INNER JOIN Drat_Make m on m.intMakeId = dm.intMakeId INNER JOIN Drat_Manufacturer mm on mm.intManufacturerId = " _
                    & "m.intManufacturerId INNER JOIN Drat_Issued i on i.intRecId = dr.intRecId INNER JOIN MNNgpersonnel.dbo.tblMnNatPersonnel mn on mn.intPersonnelid = i.intIssuedTo Where dtSigned Is NULL"
                Case 2
                Case 3
      
            End Select
      
            myRadGrid.DataSource = getData(sql)
        End Sub

  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Apr 2012 Link to this post

    Hi Kevin,

    One suggestion is to make the RadGrid Visibility to false in the Aspx page and make it visible in the LinkButton Click event. Please take a look into the following code snippet.

    ASPX:
    <telerik:RadGrid ID="myRadGrid" runat="server" Width="75%" BorderWidth="1px"
                CellPadding="6" GridLines="None" BorderColor="#404040" Skin="Web20"
                onneeddatasource="myRadGrid_NeedDataSource" Visible="false"></telerik:RadGrid>

    VB:
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            rblView.Visible = True
        End If
    End Sub
    Protected Sub myRadGrid_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
                 
        If Bind Then
         'your code
        End If
     
    End Sub
    Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)
        Bind = True
        myRadGrid.Visible = True
        myRadGrid.Rebind()
    End Sub

    Thanks,
    Princy.
  6. Ricardo
    Ricardo avatar
    4 posts
    Member since:
    Nov 2013

    Posted 30 Apr 2014 in reply to Princy Link to this post

    what is purpose of NeedDataSource if it tries to load the grid on PageLoad???
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 01 May 2014 in reply to Ricardo Link to this post

    Hi Ricardo,

    The advanced data binding of a RadGrid control is handling the NeedDataSource event. RadGrid fires the NeedDataSource event each time it needs to be bound to a data source. You can take a look at the following articles to have more information about when this event fires:
    Advanced Data-binding (Using NeedDataSource Event)
    Event sequence

    Thanks,
    Princy
  8. Suresh
    Suresh avatar
    3 posts
    Member since:
    Oct 2017

    Posted 26 Oct 2017 Link to this post

    Hello All and Telerik Admin,
    I have tried one trick that is... I added an "if condition" of Page.IsPostback
    If page is postback, I let the "OnNeedDataSource" code run else nothing.. And it seems working...

    Could you please confirm if this approach is good to go with?
  9. Suresh
    Suresh avatar
    3 posts
    Member since:
    Oct 2017

    Posted 26 Oct 2017 Link to this post

    Hello All and Telerik Admin,
    I have tried one trick that is... I added an "if condition" of Page.IsPostback
    If page is postback, I let the "OnNeedDataSource" code run else nothing.. And it seems working...

    Could you please confirm if this approach is good to go with?
Back to Top