Solved

GridView with Buttons Print, View and Add

Posted on 2009-05-19
9
1,026 Views
Last Modified: 2012-05-07
Hi experts-Exchange,

Please see the attached file for your review.
I have GridView with the following data fields :  Number, Date and three different buttons :  Print,  View,  Add.

After search Number 12345 and GridView it will display. In the grid view, if I click the button Print it will print image of the selected record... or If I click the button View it will display an image in Window Popup with JavaScript of the selected record and finally if click the button Add it will add the selected information record  into listbox.

I would like to see how to make each button working for specific select row?

Here is my code:


<asp:Label 
                      ID="Label4" 
                      runat="server" 
                      Text="Number:" 
                      Font-Bold ="True"
                      Font-Names ="Vrinda"
                      Font-Size = "15pt" 
                      Width="76px"></asp:Label>
                     
 
<asp:TextBox 
                       ID="TxtNum" 
                       runat="server" 
                       Width="61px"
                        TabIndex ="1"></asp:TextBox>
 
<asp:Button 
                      ID="BtnFill" 
                      runat="server" 
                      Text="Go" 
                      Width="45px"
                      Font-Bold ="false"
                      Font-Names ="arial"
                      Font-Size ="9pt" Height="22px"
                      TabIndex ="2"    />
 
<asp:GridView  ID="gridview1" 
          runat="server" 
          AutoGenerateColumns ="false"
          PageSize="5" 
          Width="410px" 
          BorderWidth ="1px" 
          Font-Names= "tahoma"
          Font-Size ="8pt" 
          BackColor="#DEBA84" 
          AlternatingRowStyle-BackColor="#F9F9F9"
          BorderColor="#DEBA84" 
          BorderStyle="None" 
          DataKeyNames ="IDNum"
          CellPadding="3" CellSpacing="2"
          DataSourceID="SqlDataSource1" Height="3px"
          OnRowCreated =" GridView1_RowCreated "> 
          
      <Columns>
            <asp:BoundField DataField="InfoNum" HeaderText="Number"  >
                <ControlStyle Font-Bold="True"  />
                <HeaderStyle Width="75px" />
            </asp:BoundField>
            
            <asp:BoundField DataField="InfoDate" HeaderText="Date" HtmlEncode ="False" DataFormatString="{0:MM/dd/yyyy}"  >
                <ControlStyle Width="100px" />
                <HeaderStyle Width="100px" />
 
            </asp:BoundField>    
                   <asp:ButtonField  ButtonType ="Button" CommandName="print Text="Print" HeaderText ="Print">
                       <ControlStyle Font-Names="Calibri" Font-Size="8pt" />
                   </asp:ButtonField>
                   <asp:ButtonField  ButtonType ="Button" Text="View" CommandName="view HeaderText ="View" >
                       <ControlStyle Font-Names="Calibri" Font-Size="8pt" />
                   </asp:ButtonField>
                   <asp:ButtonField  ButtonType ="Button" Text="Add"  CommandName="add HeaderText ="Add">
                       <ControlStyle Font-Names="Calibri" Font-Size="8pt" />
                   </asp:ButtonField>
        
        </Columns>
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="#F9F9F9" />
        
    </asp:GridView>
 
 
<asp:SqlDataSource 
             ID="SqlDataSource1" 
             runat="server" 
             ConnectionString="<%$ ConnectionStrings:ABCConnStr %>" SelectCommand="SELECT [idNum], [infoNum], [infodate] FROM [InfoTable] "></asp:SqlDataSource>   
    
VB.CODE
 
 
Protected Sub BtnFill_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnFill.Click
 
SearchNumber()
 
End sub
 
Private Sub SearchNumber()
 
        Dim StrCnn As String
        Dim cnn As New SqlConnection
        Dim StrSQL As String
        Dim da As SqlDataAdapter
        Dim ds As Data.DataSet
 
        Try
 
            StrCnn = "server=XP;database=Info;uid=aa;pwd=bb"
            cnn = New SqlConnection(StrCnn)
            cnn.Open()
 
 
            StrSQL = "SELECT IDNum,  "
            StrSQL += "InfoNum, InfoDate "
            StrSQL += "FROM InfoTable WHERE InfoNum = '" & TxtNum.Text & "' "
 
            'get the data:
            da = New SqlDataAdapter(StrSQL, cnn)
            ' Create a DataSet Object
            ds = New DataSet
            ' Fill DataSet with the data
            da.Fill(ds, 0)
 
Session("DataView") = ds.Tables(0).DefaultView()
 
            ' Me.GridView1.DataSource = ds.Tables("InfoTable")
            Me.gridview1.DataBind()
            GridLoad()
End If
 
        Catch x As Exception
 
        End Try
 
    End Sub
 
 
Private Sub GridLoad()
        Dim dv As Data.DataView
 
        Me.gridview1.AutoGenerateColumns = False
        gridview1.Visible = True
        gridview1.Enabled = True
 
        ''fill the grid with data:
        gridview1.DataSource = Session("DataView")
        gridview1.DataBind()
 
        ''show the record count:
        dv = CType(Session("DataView"), Data.DataView)
 
    End Sub
 
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridview1.SelectedIndexChanged
 
        ' Obtain the index of the selected row
        Dim selectedRow As Integer
selectedRow = gridview1.SelectedIndex
Dim row As GridViewRow = gridview1.SelectedRow
End Sub
 
 
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
 
        If e.Row.RowType = DataControlRowType.DataRow Then
 
            e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Print$" & e.Row.RowIndex.ToString))
end if
 
        If e.Row.RowType = DataControlRowType.DataRow Then
 
            e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "View$" & e.Row.RowIndex.ToString))
end if
 
        If e.Row.RowType = DataControlRowType.DataRow Then
 
            e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Add$" & e.Row.RowIndex.ToString))
 
        End If
End Sub
 
 
 
Thanks

Open in new window

GV.jpg
0
Comment
Question by:slb2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 4

Expert Comment

by:baiju_nagori
ID: 24424176
0
 

Author Comment

by:slb2008
ID: 24428595
Hi Baiju
Thanks for the link and I tested it, but tomorrow I'll send you the code so you could review it.
Thanks again.
 
0
 

Author Comment

by:slb2008
ID: 24438305
Hi Baiju,
I tested and it's working.  But I have a little problem with button View in the gridview.   When I click the button View pops up the  window to view the image, instead of to be in front of the web page the window popup stays behind of the  web page.  Why this is happening?
Thanks
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 4

Accepted Solution

by:
baiju_nagori earned 500 total points
ID: 24440391
Try creating a modal window

http://msdn.microsoft.com/en-us/library/ms536759(VS.85).aspx

This might not work in firefox, so use following code
function modalWin() {    
    if (window.showModalDialog) {
        window.showModalDialog("xpopupex.htm","name", "dialogWidth:255px;dialogHeight:250px");
} else {
window.open('xpopupex.htm','name',
'height=255,width=250,toolbar=no,directories=no,status=no,
continued from previous linemenubar=no,scrollbars=no,resizable=no ,modal=yes');
}
} 

Open in new window

0
 

Author Comment

by:slb2008
ID: 24444353
Thansk for sending the javascript code, but isn't working.  The button "View is nested in the gridview, and I click to view image, is still behind of the web page.
See this code:

Protected Sub GV_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
 
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim selectedRow As GridViewRow = (DirectCast(e.CommandSource, GridView)).Rows(index)
 
        If e.CommandName = "View Image" Then
 
             
            GetImage()
 
        End If
 
 
Private Sub GetImage()
 
       Dim strSQL As String
       Dim cnn  as New SqlConnection
       Dim ds As Data.DataSet
       Dim FileId As Integer
       Dim FileData() As Byte
       Dim LocalImageFile As String
 
        Try
 StrCnn = "server=XP;database=Info;uid=aa;pwd=bb"
cnn = New SqlConnection(StrCnn)
cnn.Open()
 
 
strSQL = "SELECT IDNum, InfoNum, InfoDate FROM InfoTable WHERE InfoNum =  & Txtnum.Text &  
 
            If Not IsNothing(ds) AndAlso ds.Tables(0).Rows.Count > 0 Then
                FileId = CType(ds.Tables(0).Rows(0)("IDNum"), Integer)
                FileData = getFile ("infotable", FileId, 1, 0)
 
                If Not IsNothing(FileData) Then
                    LocalImageFile = Func.cacheFile(LocalPath, FileData, FileId.ToString, "TIF", 1, 1)
This pops up window behind of the web page -->  Response.Write("<script>window.open('/abc/images/" & LocalImageFile & "','abcImage','');</script>")
 
                Else
                    Throw New Exception("Filedata is nothing.")
                End If
            Else
                Throw New Exception("Dataset is nothing.")
            End If
 
        Catch x As Exception 'if query response fails:
            lblNoPlat.Visible = True
        End Try
    End Sub
 
 
Thanks
0
 
LVL 4

Expert Comment

by:baiju_nagori
ID: 24444998
Hi slb2008,

Can you put this in the body tag of the popup html page, when the popup loses focus it is brought back to the top again, it is now modal.

<body onblur = "window.focus( )">
0
 

Author Comment

by:slb2008
ID: 24479030
Baiju,
I put in th body tag <body onload onblur = "window.focus( )"> (instead of onblur -  doesn't work for asp.Net web form). Still the same.  After click the button "view"  the window popup view image stay behind of the asp.net web form.
Do you think the "Response.Write" is the problem?  Or is the GridView problem?
Thanks
0
 

Author Comment

by:slb2008
ID: 24488577
Baiju,
I have a little problem with select row after click one the buttons, for example if I select the second row doesn's display the information in second row, but show the information on first row.  What am I doing wrong?  Please could you help me how to click each button for the first row and display the data in the first row,  If  I select the second and click add it will display info from the second row.
here the code:

<asp:GridView  ID="gridview1"
                 runat="server"
                 AutoGenerateColumns ="false"
               PageSize="3"
                Width="409px"
                BorderWidth ="1px"
                Font-Names= "tahoma"
                Font-Size ="8pt"
                AlternatingRowStyle-BackColor="#F9F9F9"
                BorderColor="#DEBA84"
                BorderStyle="None"
                DataKeyNames ="platdate"
                CssClass="GridViewStyle" CellPadding="3" CellSpacing="2"
                 OnRowCreated ="GrdDocs_RowCreated"
                OnRowCommand ="GV_RowCommand"
                  > 
 
 <Columns>            <asp:BoundField DataField="InfoNum" HeaderText="Number"  >                <ControlStyle Font-Bold="True"  />                <HeaderStyle Width="75px" />            </asp:BoundField>                        <asp:BoundField DataField="InfoDate" HeaderText="Date" HtmlEncode ="False" DataFormatString="{0:MM/dd/yyyy}"  >                <ControlStyle Width="100px" />                <HeaderStyle Width="100px" />             </asp:BoundField>                       <asp:ButtonField  ButtonType ="Button" CommandName="print Text="Print" HeaderText ="Print">                       <ControlStyle Font-Names="Calibri" Font-Size="8pt" />                   </asp:ButtonField>                   <asp:ButtonField  ButtonType ="Button" Text="View" CommandName="view HeaderText ="View" >                       <ControlStyle Font-Names="Calibri" Font-Size="8pt" />                   </asp:ButtonField>                   <asp:ButtonField  ButtonType ="Button" Text="Add"  CommandName="add HeaderText ="Add">                       <ControlStyle Font-Names="Calibri" Font-Size="8pt" />                   </asp:ButtonField>                </Columns>        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />        <AlternatingRowStyle BackColor="#F9F9F9" />            </asp:GridView>
 
 
 
 
Protected Sub GV_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
 
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim selectedRow As GridViewRow = (DirectCast(e.CommandSource, GridView)).Rows(index)
 
        If e.CommandName = "View" Then
 
            View()
End if
 
 
 
            'Dim indexa As Integer = Convert.ToInt32(e.CommandArgument)
            'Dim selectedRowa As GridViewRow = (DirectCast(e.CommandSource, GridView)).Rows(indexa)
        ElseIf e.CommandName = "Add" Then
 
            AddToList()
 
 
 
 
 
            '            Dim indexaa As Integer = Convert.ToInt32(e.CommandArgument)
            '           Dim selectedRowaa As GridViewRow = (DirectCast(e.CommandSource, GridView)).Rows(indexaa)
        ElseIf e.CommandName = "Print" Then
 
            Me.lblSelectPlat.Visible = False
            Me.lblRecs.Visible = False
            Me.lblNoSubPlat.Visible = False
            Me.lblEmptyList.Visible = False
 
            Printer()
        End If
End Sub
 
 
 Thanks.
 
 
0
 

Author Comment

by:slb2008
ID: 24498250
Dear experts-exchange,
 
Please could  someone help me on this.  
 Sincerely Thanks
 
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question