Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have problem regarding data and button click in gridview
this is my code :
ASP:
ASP.NET
<table width="580px" cellspacing="5px" align="left">
        <tr>
            <td colspan="4">
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Error Page :"
                            Width="100%" CssClass="validation" ShowMessageBox="True" Style="color: #FF0000"
                            Font-Names="Book Antiqua" Font-Size="10"></asp:ValidationSummary>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
        <tr>
            <td valign="top" style="font-family: 'book Antiqua'; font-size: 10pt; color: #000000"
                nowrap="nowrap" width="100px">
                File Name <font color="#FF0000" size="2">*</font>
            </td>
            <td width="10px" valign="top" style="font-family: 'book Antiqua'; font-size: 10pt;
                color: #000000">
                :
            </td>
            <td>
                <%--<asp:UpdatePanel ID="UpdatePanel4" runat="server">
                    <ContentTemplate>--%>
                <dx:ASPxUploadControl ID="filenameASPxUploadControl"  runat="server" CssFilePath="~/App_Themes/Office2010Silver/{0}/styles.css"
                    CssPostfix="Office2010Silver" Font-Names="Book Antiqua" Font-Size="10pt" Height="20px"
                    SpriteCssFilePath="~/App_Themes/Office2010Silver/{0}/sprite.css" AddUploadButtonsHorizontalPosition="Right"
                    ClientInstanceName="uploader" ShowProgressPanel="True" ForeColor="Black">
                    <ValidationSettings MaxFileSize="4000000" AllowedFileExtensions=".xls">
                    </ValidationSettings>
                </dx:ASPxUploadControl>
                <%-- </ContentTemplate>
                </asp:UpdatePanel>--%>
            </td>
            <td align="right" valign="top">
                <asp:UpdatePanel ID="UpdatePanel7" runat="server">
                    <ContentTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server">Download Template</asp:LinkButton>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                 
            </td>
            <td colspan="2">
                <table width="100%" style="margin-removed 0px">
                    <tr>
                        <td width="80px" align="left">
                            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                                <ContentTemplate>
                                    <dx:ASPxButton ID="btnUpload"  runat="server" EnableTheming="True" Text="Add" ClientInstanceName="btnUpload"
                                        EnableDefaultAppearance="False" ImagePosition="Right" Width="78px" Height="25px"
                                        Cursor="pointer" ForeColor="Black" Font-Names="Book Antiqua" BackgroundImage-HorizontalPosition="center"
                                        BackgroundImage-VerticalPosition="top" VerticalAlign="Top" TabIndex="2" Font-Size="10pt"
                                        AutoPostBack="False">
                                        <HoverStyle ForeColor="#000000" BackgroundImage-Repeat="NoRepeat">
                                            <BackgroundImage ImageUrl="Image/btnhover.png" Repeat="NoRepeat" HorizontalPosition="center"
                                                VerticalPosition="top" />
                                        </HoverStyle>
                                        <Paddings PaddingTop="3px" />
                                        <PressedStyle ForeColor="#7C7B7B" BackgroundImage-Repeat="NoRepeat">
                                            <BackgroundImage ImageUrl="Image/hoverpress.png" Repeat="NoRepeat" HorizontalPosition="center"
                                                VerticalPosition="top" />
                                        </PressedStyle>
                                        <FocusRectBorder BorderColor="#757575" BorderStyle="Dotted" BorderWidth="2px" />
                                        <BackgroundImage ImageUrl="Image/Button.png" Repeat="NoRepeat" HorizontalPosition="center"
                                            VerticalPosition="top" />
                                        <ClientSideEvents Click="function(s, e) { uploader.UploadFile(); }" />
                                    </dx:ASPxButton>
                                </ContentTemplate>
                            </asp:UpdatePanel>
                        </td>
                        <td width="80px">
                            <dx:ASPxButton ID="btnClear"  runat="server" EnableTheming="True" Text="Clear" EnableDefaultAppearance="False"
                                ImagePosition="Right" Width="78px" Height="25px" Cursor="pointer" ForeColor="#000000"
                                Font-Names="Book Antiqua" BackgroundImage-HorizontalPosition="center" BackgroundImage-VerticalPosition="top"
                                VerticalAlign="Top" TabIndex="2" Font-Size="10pt" AutoPostBack="False">
                                <HoverStyle ForeColor="#000000" BackgroundImage-Repeat="NoRepeat">
                                    <BackgroundImage ImageUrl="Image/btnhover.png" Repeat="NoRepeat" HorizontalPosition="center"
                                        VerticalPosition="top" />
                                </HoverStyle>
                                <Paddings PaddingTop="3px" />
                                <PressedStyle ForeColor="#7C7B7B" BackgroundImage-Repeat="NoRepeat">
                                    <BackgroundImage ImageUrl="Image/hoverpress.png" Repeat="NoRepeat" HorizontalPosition="center"
                                        VerticalPosition="top" />
                                </PressedStyle>
                                <FocusRectBorder BorderColor="#757575" BorderStyle="Dotted" BorderWidth="2px" />
                                <BackgroundImage ImageUrl="Image/Button.png" Repeat="NoRepeat" HorizontalPosition="center"
                                    VerticalPosition="top" />
                            </dx:ASPxButton>
                        </td>
                        <td>
                             
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td colspan="4" style="border-top-style: solid; border-color: #979595">
                 
            </td>
        </tr>
        <tr>
            <td colspan="4">
                <asp:UpdatePanel ID="UpdatePanel3" runat="server" >
                    <ContentTemplate>
                        <dx:ASPxGridView ID="grdData"  runat="server" AutoGenerateColumns="True" CssFilePath="~/App_Themes/Office2010Silver/{0}/styles.css"
                            CssPostfix="Office2010Silver" Width="100%" KeyFieldName="SN"  önRowDeleting="grdData_RowDeleting">
                            <Columns>
                                <dx:GridViewDataTextColumn VisibleIndex="0" Caption="No" Width="40px" ReadOnly="True"
                                    HeaderStyle-HorizontalAlign="Center">
                                    <DataItemTemplate>
                                        <dx:ASPxLabel ID="lblRowID_T1"  runat="server" Text='<%# Container.ItemIndex + 1 %>'>
                                        </dx:ASPxLabel>
                                    </DataItemTemplate>
                                    <HeaderStyle Font-Names="book Antiqua" Font-Size="10pt" HorizontalAlign="Center"
                                        VerticalAlign="Middle" Wrap="True" />
                                    <CellStyle Wrap="False">
                                    </CellStyle>
                                </dx:GridViewDataTextColumn>
                                <dx:GridViewDataTextColumn VisibleIndex="1" Caption="Tran Date" Width="19%" ReadOnly="True"
                                    FieldName="TranDate">
                                    <HeaderStyle Font-Names="book Antiqua" Font-Size="10pt" HorizontalAlign="Center"
                                        VerticalAlign="Middle" Wrap="True" />
                                    <CellStyle Wrap="False">
                                    </CellStyle>
                                </dx:GridViewDataTextColumn>
                                <dx:GridViewDataTextColumn VisibleIndex="2" Caption="Partner Code" Width="19%" ReadOnly="True"
                                    FieldName="PartnerCode">
                                    <HeaderStyle Font-Names="book Antiqua" Font-Size="10pt" HorizontalAlign="Center"
                                        VerticalAlign="Middle" Wrap="True" />
                                    <CellStyle Wrap="False">
                                    </CellStyle>
                                </dx:GridViewDataTextColumn>
                                <dx:GridViewDataTextColumn VisibleIndex="3" Caption="Partner Name" Width="19%" ReadOnly="True"
                                    FieldName="PartnerName">
                                    <HeaderStyle Font-Names="book Antiqua" Font-Size="10pt" HorizontalAlign="Center"
                                        VerticalAlign="Middle" />
                                    <CellStyle Wrap="False">
                                    </CellStyle>
                                </dx:GridViewDataTextColumn>
                                <dx:GridViewDataTextColumn VisibleIndex="4" Caption="Serial Number" Width="19%" ReadOnly="True"
                                    FieldName="SN">
                                    <HeaderStyle Font-Names="book Antiqua" Font-Size="10pt" HorizontalAlign="Center"
                                        VerticalAlign="Middle" />
                                    <CellStyle Wrap="False">
                                    </CellStyle>
                                </dx:GridViewDataTextColumn>
                                <dx:GridViewDataTextColumn VisibleIndex="5" Caption="MTM" Width="19%" ReadOnly="True"
                                    FieldName="MTM">
                                    <HeaderStyle Font-Names="book Antiqua" Font-Size="10pt" HorizontalAlign="Center"
                                        VerticalAlign="Middle" />
                                    <CellStyle Wrap="False">
                                    </CellStyle>
                                </dx:GridViewDataTextColumn>
                                <dx:GridViewCommandColumn VisibleIndex="6" Caption="Delete" ButtonType="Link" Width="50px">
                                    <DeleteButton Text="Delete" Visible="True">
                                    </DeleteButton>
                                    <HeaderStyle Font-Names="book Antiqua" Font-Size="10pt" HorizontalAlign="Center"
                                        VerticalAlign="Middle" />
                                    <CellStyle Wrap="False">
                                    </CellStyle>
                                </dx:GridViewCommandColumn>
                            </Columns>
                            <SettingsBehavior ColumnResizeMode="Control" />
                            <Settings ShowHorizontalScrollBar="True" />
                            <Images SpriteCssFilePath="~/App_Themes/Office2010Silver/{0}/sprite.css">
                                <LoadingPanelOnStatusBar Url="~/App_Themes/Office2010Silver/GridView/Loading.gif">
                                </LoadingPanelOnStatusBar>
                                <LoadingPanel Url="~/App_Themes/Office2010Silver/GridView/Loading.gif">
                                </LoadingPanel>
                            </Images>
                            <ImagesFilterControl>
                                <LoadingPanel Url="~/App_Themes/Office2010Silver/GridView/Loading.gif">
                                </LoadingPanel>
                            </ImagesFilterControl>
                            <Styles CssFilePath="~/App_Themes/Office2010Silver/{0}/styles.css" CssPostfix="Office2010Silver"
                                AlternatingRow-BackColor="#F3F3F3">
                                <Header ImageSpacing="5px" SortingImageSpacing="5px">
                                </Header>
                                <AlternatingRow BackColor="#F3F3F3">
                                </AlternatingRow>
                                <LoadingPanel ImageSpacing="5px">
                                </LoadingPanel>
                                <CommandColumn CssClass="Office2010Silver">
                                </CommandColumn>
                            </Styles>
                            <StylesEditors ButtonEditCellSpacing="0">
                                <ProgressBar Height="21px">
                                </ProgressBar>
                            </StylesEditors>
                        </dx:ASPxGridView>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
        <tr>
            <td colspan="4">
                <asp:UpdatePanel ID="UpdatePanel6" runat="server">
                    <ContentTemplate>
                        <dx:ASPxButton ID="btnSubmit"  runat="server" EnableTheming="True" Text="Submit" EnableDefaultAppearance="False"
                            ImagePosition="Right" Width="78px" Height="25px" Cursor="pointer" ForeColor="#000000"
                            Font-Names="Book Antiqua" BackgroundImage-HorizontalPosition="center" BackgroundImage-VerticalPosition="top"
                            VerticalAlign="Top" TabIndex="2" Font-Size="10pt" AutoPostBack="False">
                            <HoverStyle ForeColor="#000000" BackgroundImage-Repeat="NoRepeat">
                                <BackgroundImage ImageUrl="Image/btnhover.png" Repeat="NoRepeat" HorizontalPosition="center"
                                    VerticalPosition="top" />
                            </HoverStyle>
                            <Paddings PaddingTop="3px" />
                            <PressedStyle ForeColor="#7C7B7B" BackgroundImage-Repeat="NoRepeat">
                                <BackgroundImage ImageUrl="Image/hoverpress.png" Repeat="NoRepeat" HorizontalPosition="center"
                                    VerticalPosition="top" />
                            </PressedStyle>
                            <FocusRectBorder BorderColor="#757575" BorderStyle="Dotted" BorderWidth="2px" />
                            <BackgroundImage ImageUrl="Image/Button.png" Repeat="NoRepeat" HorizontalPosition="center"
                                VerticalPosition="top" />
                        </dx:ASPxButton>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
        <tr>
            <td colspan="4">
                <asp:CustomValidator ID="cvalPageError" runat="server" Display="None" Style="color: Black"
                    Font-Names="Book Antiqua" Font-Size="10pt"></asp:CustomValidator>
            </td>
        </tr>
    </table>



VB Code:
VB
    Private Sub FillGridFroExcel(ByVal x As DataTable)
        Dim bLoop As Boolean = True
        Dim ino As Integer

        Dim dt As DataTable
        Dim dtGrid As DataTable
        Dim dr As DataRow
        Dim sBarCode As String = ""
        Try
            ino = 0
            dt = x
            dtGrid = Session("DT_DataTable_T1SO_ByFile")
            If Session("BPType") = "T1" Then
                For i = 1 To dt.Rows.Count - 1
                    Try
                        dr = dtGrid.NewRow()
                        dr.Item(0) = CType(dt.Rows(i).Item(0), Date)
                        dr.Item(1) = CType(dt.Rows(i).Item(1), String)
                        Try
                            dr.Item(2) = CType(dt.Rows(i).Item(2), String)
                        Catch ex As Exception
                            dr.Item(2) = ""
                        End Try
                        sBarCode = CType(dt.Rows(i).Item(3), String)
                        dr.Item(3) = CType(Right(sBarCode, 10), String)
                        dr.Item(4) = CType(Mid(sBarCode, 3, 8), String)
                        dr.Item(5) = CType("", String)
                        dtGrid.Rows.Add(dr)
                        dtGrid.AcceptChanges()
                        ino += 1
                    Catch ex As Exception
                    End Try
                Next i
            End If
            Session("DT_DataTable_T1SO_ByFile") = dtGrid
            Session("FillGrid") = dtGrid

            grdData.DataSource = dtGrid
            grdData.DataBind()
        Catch ex As Exception
            Throw New Exception(String.Format("File error ! {0}Please check your file, make sure use default template.", vbCrLf))
            Me.cvalPageError.IsValid = False
            Me.cvalPageError.ErrorMessage = ex.Message
            'Return Nothing
        End Try
    End Sub

    Protected Function OpenExcelFile(ByVal fileName As String) As Data.DataTable
        Dim dataTable As New DataTable()
        Try
            Dim connectionString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName)
            Dim adapter As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString)
            adapter.Fill(dataTable)
            Dim FileToDelete As String
            'Set full path to file 
            FileToDelete = Server.MapPath(UploadDirectory) + Session("UserNam") + ".xls"
            'Delete a file
            File.Delete(FileToDelete)
            Return dataTable
        Catch ex As Exception
            Throw New Exception(String.Format("File error ! {0}Please check your file, make sure use default template.", vbCrLf))
            Me.cvalPageError.IsValid = False
            Me.cvalPageError.ErrorMessage = ex.Message
            Return dataTable
        End Try
    End Function

    Private Function SavePostedFiles(ByVal uploadedFile As UploadedFile) As String
        Try
            If (Not uploadedFile.IsValid) Then
                Return String.Empty
            End If

            Dim fileInfo As New FileInfo(uploadedFile.FileName)
            Dim newFileName As String
            'x = Replace(file.Name, file.Name, "ishaq")
            newFileName = fileInfo.Name.Replace(fileInfo.Name, Session("UserNam"))
            Session("FileName") = fileInfo.Name
            Dim resFileName As String = Server.MapPath(UploadDirectory) + newFileName + ".xls"
            uploadedFile.SaveAs(resFileName)

            Return resFileName
        Catch ex As Exception
            Me.cvalPageError.IsValid = False
            Me.cvalPageError.ErrorMessage = ex.Message
        End Try

    End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("wasLogin") <> "Login" Then
                Response.Redirect("~/Login.aspx", False)
                Exit Sub
            End If
            Dim sl As New SSaveLog
            Dim dts As New SServerInformation
            sl.SaveLog(Session("CountryCode"), Session("UserNam"), dts.GetServerTime)
            If (Not IsPostBack) Then
                ClearPageSession()
                BindTable()
                If Session("BPType") = "T1" Then
                    'UpdatePanel3.Update()
                    grdData.DataSource = Session("FillGrid")
                    grdData.DataBind()
                End If
            Else
                If Session("BPType") = "T1" Then
                    grdData.DataSource = Session("DT_DataTable_T1SO_ByFile")
                    grdData.DataBind()
                End If
            End If
            'Session("FillGrid") = Nothing
            Session("FillGrid") = Nothing
        Catch ex As Exception
            Me.cvalPageError.IsValid = False
            Me.cvalPageError.ErrorMessage = ex.Message
        End Try
    End Sub

    Protected Sub filenameASPxUploadControl_FileUploadComplete1(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs) Handles filenameASPxUploadControl.FileUploadComplete
        Try
            FillGridFroExcel(OpenExcelFile(SavePostedFiles(e.UploadedFile)))
            'Response.Redirect("~/T_T1SO_ByFile.aspx", False)
            'e.CallbackData = Session("DT_DataTable_T1SO_ByFile")
        Catch ex As Exception
            Me.cvalPageError.IsValid = False
            Me.cvalPageError.ErrorMessage = ex.Message
        End Try
    End Sub

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        grdData.DataSource = Session("DT_DataTable_T1SO_ByFile")
        grdData.DataBind()
    End Sub



Please help me,
Gridview isnot showing data on single click of "upload" button, but if I click again "upload" button gridview will be showing data.
Posted
Updated 3-Nov-13 23:14pm
v2

 
Share this answer
 
Hi
Be clear with your question, what is the requirement you want to achieve what type of trouble you are facing. Just comment the update panel and try the code.
On a button click you want to bring the data from Database and bind that to the grid view? is this your intention?
 
Share this answer
 
I want to show data from a import file, after I import the file I want to show data in gridview.
no error message appears, but I must click the upload button twice.
for information my file is excel file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900