Get ChekBox Component in Gridview Inside Content Template - asp.net

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.

Related

how to change label with button in vb?

so i want to have a button the changes the text of a label when i click it and it says page not available, but when i comment out the update panel it atleast shows up.
<form id="form1" runat="server">
<asp:ScriptManager ID="s1" runat = "server"></asp:ScriptManager>
<div>
<h1>UpDaTe PaNeL tEsT PaGe</h1>
start text : Hello <br />
updated text: World<br />
<hr />
<asp:UpdatePanel ID="p1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Update" OnClick="updateLabel" Height="29px" Width="110px"/> -->
Text: <asp:Label ID="Label1" runat="server" Text="hello"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
codebehind
Public Class aaaa
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Function updatelabel() As String
Label1.Text = "World"
End Function
End Class
The previous answer is correct. The handler for the Label Click event needs the sender as Object and the e as EventArgs parameters. If you do not put them, your updateLabeL() function will not be a valid match for the event handler.
You can remove the OnClick from the button and Try the classic method of handling events.
So your code will be... ( Sorry I'm posting it via app so it might not be in code view )
Protected Sub ChangeLabel(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.text = "TextGoesHere"
End Sub

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

How to dynamically add controls and preserve Viewstate?

I'm attempting to use Controls.AddAt(), but it apparently breaks controls at later indexes:
Here's my minimal example:
Aspx put in a form:
<asp:DropDownList runat="server" ID="ddl" />
<asp:Button Text="text" runat="server" OnClick="Unnamed2_Click" />
Code Behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ddl.Items.Add("Click the button")
'Controls.Add(New HyperLink) 'Works fine, but is put at end of collection.
'Controls.AddAt(2 ,New HyperLink) 'Is also safe but I wanted the control first
Controls.AddAt(0, New HyperLink) 'ddl loses it's item after postback
End If
End Sub
On the first postback of the page after calling AddAt, the DropDownList loses it's item. It doesn't matter what kind of control I add even HTMLControls. Viewstate is not disabled.
How do I dynamically add controls without breaking others?
If you used a PlaceHolder to add your HyperLink into, it would not mess up the rest of the page:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:DropDownList ID="ddl" EnableViewState="true" runat="server" />
<asp:Button ID="bn1" Text="text" OnClick="Unnamed2_Click" runat="server" />
With code like
Protected Sub Unnamed2_Click(sender As Object, e As EventArgs) Handles bn1.Click
Dim newItem = "Click the button" & DateTime.Now.ToString("HH:mm:ss")
ddl.Items.Add(newItem)
ddl.SelectedIndex = ddl.Items.Count - 1
PlaceHolder1.Controls.Add(New HyperLink With {.ID = "hyp", .Text = "Hyperlink here"})
End Sub
And always give your asp:Controls an ID if they take one.

can't find a control inside my gridview

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

Formview.ChangeMode is not working

This is my webpage:
<asp:FormView
ID = "frmView1"
DefaultMode = "Insert"
runat = "server"
>
<ItemTemplate>
Item Template
</ItemTemplate>
<InsertItemTemplate>
Insert Item Template
</InsertItemTemplate>
</asp:FormView>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
and this is the code-behind:
Private Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
frmView1.ChangeMode(FormViewMode.ReadOnly)
End Sub
When I'm clicking the btnSubmit the formview is not showing anything! Is this the right way to change mode in code behind?
Moreover, in code behind instead of ChangeMode if write this:
frmView1.DefaultMode = FormViewMode.ReadOnly
forview shows the content of InsertItemTemplate. Could someone please explain what's going on here?
FormView won't show unless you bind, something like this:
using(SqlDataAdapter adapter = new SqlDataAdapter(sql,connection))
{
DataTable table = new DataTable();
adapter.Fill(table);
FormView1.DataSource = table;
FormView1.DataBind();
}
And just add EmptyDataTemplate tag to show empty message if your datasource don't return any value.
<EmptyDataTemplate>
There is nothing to see here.
</EmptyDataTemplate>
follow this tutorial and more about this on MSDN
In your code
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
must change to
<asp:Button ID="btnSubmit" Text="Submit" runat="server" command="ChangeModeToReadOnly" OnCommand="Button_Command" />
Instead of implementing OnClick event implement this
Protected Sub Button_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
Select Case e.CommandName
Case "ChangeModeToReadOnly"
frmView1.ChangeMode(FormViewMode.ReadOnly)
End Select
End Sub
You can try with adding the following DataBound function
protected void myFormView_DataBound(object sender, EventArgs e)
{
frmView1.ChangeMode(FormViewMode.ReadOnly);
}
The syntax is in C#

Resources