Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

GridView with Buttons Print, View and Add

Posted on 2009-05-19
9
Medium Priority
?
1,030 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Accepted Solution

by:
baiju_nagori earned 1500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

916 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