can't find a control inside my gridview - asp.net

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

Related

Asp:Repeater Datasouce - Datatable Checkbox inside Repeater. Delete the current row

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.

How to hide ImageButton based on Row Cell value

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

Get ChekBox Component in Gridview Inside Content Template

I have this aspx snippet:
<asp:updatepanel runat="server" id="resultPanel">
<contenttemplate>
<app:exgridview id="referenceGridView" runat="server" allowpaging="True" allowsorting="True">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAllBox" runat="server" CausesValidation="false"></asp:CheckBox>
</HeaderTemplate>
<asp:TemplateField>
.....
How do I get checkAllBox component in .vb code behind?
I have tried this:
referenceGridView.HeaderRow _
.Cells(0).FindControl("checkAllBox")
And
referenceGridView.FindControl("checkAllBox")
but it doesn't work neither.
<asp:GridView OnRowDataBound="MyGridView_RowDataBound" ... />
Then define MyGridView_RowDataBound:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
Label l = (Label) e.Row.FindControl("lblName");
}
EDITED
You can find the control in GridView.HeaderRow:
NOTE : This code block should be executed after referenceGridView.DataBind(). I am running this in a button click:
Protected Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
Dim checkAllBox As CheckBox = TryCast(referenceGridView.HeaderRow.FindControl("checkAllBox"), CheckBox)
If checkAllBox IsNot Nothing Then
'checkAllBox exists here.
'Place your code for checkAllBox
If checkAllBox.Checked Then
lblResult.Text = "All checked"
Else
lblResult.Text = "All not checked"
End If
End If
End Sub
Presuming that you already have this in page load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'clear xml on page load if it contain data
If Not Page.IsPostBack Then
'Getting datasource
'referenceGridView.DataSource = MyDataSource.GetTable()
referenceGridView.DataBind()
End If
End Sub
And my test button in markup:
<asp:button id="btnTest" runat="server" Text="TEST" />
And you can download the test project here.

Checkbox in TemplateField ItemTmplate event won't fire

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

change gridview row color based on templatefields without controls

My gridview does not use controls, because it is populated using expressions
<asp:TemplateField HeaderText="As Of Sales">
<ItemTemplate>
<%#Getsales(Decimal.Parse(Eval("asofsales").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTemplate>
<%#Getsales1().ToString("C0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
I want to compare column index 1 and column index 8, and if 8 is bigger then 1 it should be a different font color.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim x As String
x = e.Row.Cells(1).Text
Dim y As String
y = e.Row.Cells(8).Text
If Convert.ToInt32(x) <= Convert.ToInt32(y) Then
e.Row.ForeColor = System.Drawing.Color.Blue
End If
End If
End Sub
Here is something that you can try
The gridview
<asp:GridView runat="server" ID="grdv" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="T1">
<ItemTemplate>
<%# Eval("T1")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="T2">
<ItemTemplate>
<%# Eval("T2")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim d As New DataTable
d.Columns.Add("T1")
d.Columns.Add("T2")
d.Rows.Add(1, 2)
grdv.DataSource = d
grdv.DataBind()
End Sub
Private Sub grdv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdv.RowDataBound
Dim data As DataRowView = e.Row.DataItem
If data Is Nothing Then Exit Sub
If e.Row.RowType = DataControlRowType.DataRow Then
If data.Item("T1") <= data.Item("T2") Then e.Row.ForeColor = Color.Red
End If
End Sub
This should work for binding to a DataTable. If you're using a collection then the RowDataBound event will need changing slightly.

Resources