Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

Gridview and selectedindexchanged event

I have a table where on the left I want to show the names of the prodcut and on the right the image of it. All this info is pulled from a Access database. This works ok as long as all the columns need for the right-TD are being shown in the gridview. As now I show the select-option, the productname and the productimage-field. +But I don't want to show the productimage-field to the users, so I set it to visible=false, but as soon as I do that it does not work anymore : the image is not shown in the right-TD.
Also I would like to get rid of the Select-option in the gridview, but rather have the productname being clickable to use as a select ?
This all can be seen at http://www.nemoto-international.com/products.aspx

this is the event to show the image in the right-TD :
Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim row As GridViewRow = EmployeesGridView.SelectedRow
        Message.Text = "You selected " & row.Cells(1).Text & "."
        Image1.ImageUrl = row.Cells(2).Text
    End Sub
 
This is the way to connect to the Database :
<asp:AccessDataSource    id="AccessDataSource1"    DataFile="data\DBS.mdb"    runat="server"    SelectCommand="SELECT * FROM tblPrd">
  </asp:AccessDataSource>
 
 
this I use in the left-TD :
<asp:GridView id="EmployeesGridView" runat="server" AutoGenerateColumns="False" 
                                DataSourceid="AccessDataSource1" selectedindex="0"       
                                onselectedindexchanged="CustomersGridView_SelectedIndexChanged"       
                                onselectedindexchanging="CustomersGridView_SelectedIndexChanging" 
                                autogenerateselectbutton="true"   >
        <Columns>
        
       
            <asp:BoundField DataField="PrdName" ReadOnly="true" SortExpression="PrdName" />
            <asp:BoundField DataField="PrdImage" ReadOnly="true" SortExpression="PrdImage" />
            
        </Columns>
                            </asp:GridView>
 
and this I use in the right-TD
<asp:label id="Message" forecolor="Red" runat="server"/><br /><br />
<asp:Image ID="Image1" ImageUrl="~/images/DualShot.png" runat="server"  />

Open in new window

0
dekempeneer
Asked:
dekempeneer
  • 6
  • 6
1 Solution
 
JPJ78Commented:
Hi,
A button with the CommandName="Select" does the trick.

I think might help you..
Replace
   <asp:BoundField DataField="PrdName" ReadOnly="true" SortExpression="PrdName" />
with:
        <asp:TemplateField SortExpression="PrdName">
        <ItemTemplate>
            <asp:LinkButton ID="btnSelect" runat="server" CommandName="Select"><%#Eval("PrdName")%></asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>

Open in new window

0
 
dekempeneerAuthor Commented:
Hi, that works, but what about the image-data-field ?
0
 
JPJ78Commented:
sorry, missed that...

You have to use another column template. Such as ImageField or TemplateField

Replace
<asp:BoundField DataField="PrdImage" ReadOnly="true" SortExpression="PrdImage" />
with

<asp:ImageField SortExpression="PrdImage" DataImageUrlField="PrdImage" ReadOnly="true" />

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
dekempeneerAuthor Commented:
No, no I don't want to show the information in the left-TD. I use that information to show it in the right TD
I do not want to show it to the users though.
I now use there <asp:Image ID="Image1" ImageUrl="" runat="server"  /> which is being filled by the _SelectedIndexChanged event using : Image1.ImageUrl = row.Cells(1).Text  
You see ?
0
 
JPJ78Commented:
Ok, I think I understand what you want.
You want to hide the column that shows the image url, but you have to be able to get the image url in the SelectedIndexChangedEvent so you can show the image to the right.

Try this...



<Columns>
   <asp:LinkButton ID="btnSelect" runat="server" CommandName="Select" img='<%#Eval("PrdImage")%>'><%#Eval("PrdName")%></asp:LinkButton>
</Columns>
 
 
Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim row As GridViewRow = EmployeesGridView.SelectedRow
        Message.Text = "You selected " & row.Cells(1).Text & "."
        Dim btnSelect as LinkButton = row.FindControl("btnSelect")
        if btnSelect IsNot Nothing Then Image1.ImageUrl = btnSelect.Attributes("img")
    End Sub

Open in new window

0
 
dekempeneerAuthor Commented:
the it says

System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'asp:LinkButton' is of type 'System.Web.UI.WebControls.LinkButton'
0
 
JPJ78Commented:
Sorry, I must be tired...
The Linkbutton have to be within a TemplateField.

<Columns>
        <asp:TemplateField SortExpression="PrdName">
        <ItemTemplate>
            <asp:LinkButton ID="btnSelect" runat="server" CommandName="Select" img='<%#Eval("PrdImage")%>'><%#Eval("PrdName")%></asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>
</Columns>

Open in new window

0
 
dekempeneerAuthor Commented:
Thanks that looks good.
Allthough the Message.Text = "You selected " & row.Cells(0).Text & "."   contains no data now :)
and what if I want to add another field to the right TD containing some other text (from the database)
0
 
JPJ78Commented:
As long as the text you want to get is in a boundaryfield you can use the the row.Cells(i).Text.
Otherwise you have to find the control that contains the information (linkbutton, hiddenfield etc.)

If there are alot of information you want to show on the right side I would consider getting the specified datarow from the database (maybe using a detailsview control to show the data)

Try this..
Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim row As GridViewRow = EmployeesGridView.SelectedRow
        Dim btnSelect as LinkButton = row.FindControl("btnSelect")
        if btnSelect IsNot Nothing Then 
            Message.Text = "You selected " & btnSelect.Text & "."
            Image1.ImageUrl = btnSelect.Attributes("img")
        End If
    End Sub

Open in new window

0
 
dekempeneerAuthor Commented:
The code looks ok, but there still is no text shown in Message ?
0
 
JPJ78Commented:
Hmmm,
I thought that the eval within the tags set the default property (that is Text for a linkbutton), however it didn't...

Change to this instead...
<asp:LinkButton ID="btnSelect" runat="server" CommandName="Select" img='<%#Eval("PrdImage")%>' Text='<%#Eval("PrdName")%>' />
0
 
dekempeneerAuthor Commented:
Thank you very much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now