I want to have an image displayed from a row click of gridview. not part of gridview, so should be outside of gridview when have image. It will show different images based to a value from a column in the gridview. I have tried a couple of ways, such as using gridview on row data bound plus on selected index changed. But I just could not even have the image displayed upon row click.
Ok, so we assume that the GridView has a column with the path name to the image?
The markup can say be this:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div style="float:left">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ClientIDMode="Static">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="ImageName" HeaderText="Image Name" />
<asp:BoundField DataField="Image" HeaderText="Image Name" />
</Columns>
</asp:GridView>
</div>
<div style="float:left;margin-left:40px">
<asp:Image ID="Image1" runat="server" Height="302px" Width="448px" />
</div>
<br />
</div>
</form>
data table:
Results:
I clicked on dog in the grid, and above was the result.
Edit: it would seem that the user wants a row click and NOT necessary a button on the page.
Ok, button has been removed.
Code is now this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Using cmdSQL As New SqlCommand("SELECT ID, ImageName, Image from tblImage",
New SqlConnection(My.Settings.TEST4))
cmdSQL.Connection.Open()
GridView1.DataSource = cmdSQL.ExecuteReader
GridView1.DataBind()
End Using
End Sub
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" & e.Row.RowIndex)
e.Row.ToolTip = "Click to select this row."
End If
End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
Image1.ImageUrl = GridView1.SelectedRow.Cells(2).Text
End Sub
so we now add a click row event to the WHOLE row.
Note that you MUST add this to the first line in the page directive:
EnableEventValidation="false"
The above goes in the VERY first line of the markup.
We now add two more events.
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" & e.Row.RowIndex)
e.Row.ToolTip = "Click to select this row."
End If
End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
Image1.ImageUrl = GridView1.SelectedRow.Cells(2).Text
End Sub
So now the button can be REMOVED from the row - you can click anywhere on the row. And I suppose we could hide the 3rd column - the "url of the image"
Related
Couldn't find anything like it.
The page has a Repeater, the data source is a datatable. Rows from the page are added to the datatable, then Repeater is updated and displayed on the page. For example, a user has added incorrect information and needs to remove something from Repeater by placing a check mark in the Checkbox. If the Checkbox is checked, the selected data (a row in the datatable ) should be removed from the data source and Repeater should be updated. How can this be implemented and whether it is possible at all? My code:
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:TableCell Width="80px" BackColor="#e3a99a" HorizontalAlign="Right">
<asp:CheckBox ID="cb_GetOut" runat="server" AutoPostBack="true" Checked="false" Text="DeleteRow" style="padding-right:5px" />
</asp:TableCell>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
dtTest_add.Columns.AddRange(New DataColumn(3) {New DataColumn("text1"), New DataColumn("text2"), New DataColumn("text3"), New DataColumn("text4")})
ViewState("dtTest_add") = dtTest_add
Else
dtTest_add = ViewState("dtTest_add")
Repeater1.DataSource = TryCast(ViewState("dtTest_add"), DataTable)
Repeater1.DataBind()
End If
End Sub
Protected Sub btn_addTest_Click(sender As Object, e As System.EventArgs) Handles btn_addTest.Click
dtTest_add.Rows.Add(txt1.Text, txt2.Text, txt3.Text, txt4.Text)
dtTest_add = ViewState("dtTest_add")
ViewState("dtTest_add") = dtTest_add
Repeater1.DataSource = TryCast(ViewState("dtTest_add"), DataTable)
Repeater1.DataBind()
UpdatePanel1.Update()
End Sub
Protected Sub Repeater1_ItemCreated(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemCreated
Dim cb_GO As CheckBox = CType(e.Item.FindControl("cb_GetOut"), CheckBox)
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(cb_GO)
End Sub
Protected Sub Repeater1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Dim cb_GO As CheckBox = DirectCast(e.Item.FindControl("cb_GetOut"), CheckBox)
cb_GO.Attributes("onclick") = "this.checked = (" + dtTest_add + ").deleteRow(" + CStr(e.Item.ItemIndex) + ");"
End Sub
The "btn_addTest" button is on a form not in Repeater. With it I add data to the data source. Then update the Repeater.
I handle the "onclick" Checkbox event in this line of code
cb_GO.Attributes("onclick") = "this.checked = (" + dtTest_add +
").deleteRow(" + CStr(e.Item.ItemIndex) + ");"
It is necessary to remove data from datatable, and Repeater will simply be updated then and all. But how do you do that? Can't get through to a datatable with the Repeater in ItemDataBound.
I am trying to add a secondary header on my gridview that would appear just below the normal header text assigned in the columns on the asp page. I am trying to create this row using vb, but every method I find ends with the same error:
"The table must contain row sections in order of header, body, then footer."
I have no idea what this means and the table works fine when I remove the code attempting to create another header.
Here is my normal code with a working table:
ASP:
<div style="display: none;" id="divGrid">
<asp:GridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" HeaderStyle-Font-Size="small" ShowFooter="true" Width="100%" OnRowCreated="ASPxGridView1_RowCreated">
<Columns>
There are actually fields here in my version
</Columns>
<HeaderStyle Font-Size="Small"></HeaderStyle>
<RowStyle CssClass="dtMid" />
</asp:GridView>
</div>
VB:
Protected Sub ASPxGridView1_PreRender(sender As Object, e As EventArgs) Handles ASPxGridView1.PreRender
If (ASPxGridView1.Rows.Count > 0) Then
ASPxGridView1.UseAccessibleHeader = True
ASPxGridView1.HeaderRow.TableSection = TableRowSection.TableHeader
ASPxGridView1.FooterRow.TableSection = TableRowSection.TableFooter
End If
End Sub
Protected Sub ASPxGridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowIndex = 0 Then
e.Row.RowType = DataControlRowType.Header
End If
End Sub
It seems like the error only occurs when I have a prerender Sub in there:
Protected Sub ASPxGridView1_PreRender(sender As Object, e As EventArgs) Handles ASPxGridView1.PreRender
If (ASPxGridView1.Rows.Count > 0) Then
ASPxGridView1.UseAccessibleHeader = True
ASPxGridView1.HeaderRow.TableSection = TableRowSection.TableHeader
ASPxGridView1.FooterRow.TableSection = TableRowSection.TableFooter
End If
End Sub
You can change row type to be header using below code
Protected Sub ASPxGridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowIndex = 0 Then
e.Row.RowType = DataControlRowType.Header
End If
End Sub
Also, Check this answer for more customization
I am trying to hide my imagebutton based on the cell value of another column.
So if my cell value.Text = "OPEN" then I want that specific imagebutton for that row to be invisible.
However my code hides all of the imagebuttons and I just wanna hide the ones that contain the cell text "OPEN"
Here is the code I have:
<asp:GridView ID="gvv" OnRowDataBound="gv1_RowDataBound" onrowcommand="gridupdate_RowCommand" OnPreRender="GridView1_PreRender" class="table table-striped table-bordered table-hover" runat="server">
<Columns>
<asp:TemplateField HeaderStyle-Width ="115px" HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="ImageButton3" runat="server" CommandName="Submit" ImageUrl="~/img/Sumbit.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CommandName="ASN" ImageUrl="~/img/ASN-send.png" />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/img/invoice.png" CommandName="View" />
</ItemTemplate>
<HeaderStyle Width="115px"></HeaderStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
Backend Code:
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
If (e.Row.Cells(2).Text.ToString = "OPEN") Then
Else
Dim imgBtn As ImageButton = CType(e.Row.FindControl("ImageButton3"), ImageButton)
imgBtn.Visible = False
End If
End If
End Sub
I think your code works correctly but you just need to revise your If statement, it should be:
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
If (e.Row.Cells(2).Text.ToString = "OPEN") Then
'Hide ImageButton3
Dim imgBtn As ImageButton = CType(e.Row.FindControl("ImageButton3"), ImageButton)
imgBtn.Visible = False
Else
'Do nothing
End If
End If
End Sub
Tried it on my side and it's working, unless you are doing something else in GridView1_PreRender method that maybe affect on this.
You can use the following scenario if you are using telerik rad grid. hope that this may help you to find a solution.
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Item.ItemType = GridItemType.AlternatingItem Or e.Item.ItemType = GridItemType.Item Then
Dim imgBtn As ImageButton = DirectCast(e.Item.FindControl("ImageButton3"), ImageButton)
If (e.Item.Cells(2).Text.ToString = "OPEN") Then
imgBtn.Visible = True
Else
imgBtn.Visible = False
End If
End If
End Sub
I have a simple gridview that contains a label in one of the rows. I'm trying to access that label in the RowDataBound event, but for some reason I keep getting a "Object reference not set to an instance of an object." error on the line where I am using FindControl.
I've tried using "gvQReport.FindControl", "e.Row.FindControl", and "Me.FindControl" but nothing works.
Am I not doing this correctly?
Thanks!
Protected Sub gvQReport_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim lblTest As Label = CType(gvQReport.FindControl("lblTest"), Label)
lblTest.Text = "test Label"
End Sub
<asp:GridView ID="gvQReport" OnRowDataBound="gvQReport_RowDataBound" runat="server">
<Columns>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="lblTest" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The Row property of GridViewRowEventArgs is the current row, look for your control there instead of the whole GridView.
Protected Sub gvQReport_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblTest As Label = CType(e.Row.FindControl("lblTest"), Label)
lblTest.Text = "test Label"
End If
End Sub
Ok I am using a column with check box to be able to select my data row from a GridView. But The OnCheckChanged event won't fire. I have tried reading articles to make it work and copy code exactly and it just won't fire. I am using vb.net and asp.net
<asp:GridView ID="locationDetailGrid" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate >
<asp:CheckBox ID="locationSelection" AutoPostBack="true"
runat="server" OnCheckedChanged="CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Protected Sub CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim checkbox As CheckBox = DirectCast(sender, CheckBox)
Dim row As GridViewRow = DirectCast(checkbox.NamingContainer, GridViewRow)
Response.Write(row.Cells(0).Text)
End Sub
Probably because you're databinding the GridView also on postbacks. Add an If Not Page.IsPostback into Page_Load around your databinding stuff of the GridView.
If you rebind the GridView on postbacks, you're preventing events from triggering.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridToDataSourceAndDataBind()
End If
End Sub