GridView column losing hyperlink when looping through rows - asp.net

I have an asp.net page written with vb.net that has a gridview table. The table is setup to have a checkbox column for each row. If this checkbox is checked for a row, I do some additional things. One of the other columns also includes a hyperlink on the text for each cell in that column.
I have a button on the page that will loop through and programmatically check each one of the checkboxes. The button does this correctly; however, it also removes the hyperlink from the other column for some reason. Any ideas why?
Here is the code where I set the hyperlinks for the hyperlink column:
Protected Sub gridData_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridData.RowDataBound
'When a row is added to the data grid view we need to add the hyperlink to the zipname column
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cell As TableCell = e.Row.Cells(4)
Dim lnk As HyperLink = New HyperLink
'Set properties of the link
lnk.NavigateUrl = cell.Text
lnk.Text = cell.Text
'Format the filename, filepath, zipname, and username to be lowercase
e.Row.Cells(1).Text = LCase(e.Row.Cells(1).Text)
e.Row.Cells(3).Text = LCase(e.Row.Cells(3).Text)
e.Row.Cells(4).Text = LCase(e.Row.Cells(4).Text)
e.Row.Cells(5).Text = LCase(e.Row.Cells(5).Text)
'Add the hyperlink
cell.Controls.Clear()
cell.Controls.Add(lnk)
End If
End Sub
Here is the code for the check all button:
Protected Sub cmdCheckAll_Click(sender As Object, e As EventArgs) Handles cmdCheckAll.Click
'Check all of the download checkboxes
For Each row As GridViewRow In gridData.Rows
CType(row.FindControl("CheckBox1"), CheckBox).Checked = True
Next
End Sub

As is the usual case with Dynamic Controls, All the dynamically created Hyperlinks in RowDataBound event needs to be recreated every postback.
So, there are 2 options.
1.) Either bind your GridView on every postback :
protected void Page_Load(object sender, EventArgs e)
{
gridData.DataBind();
}
OR
2.) Turn off ViewState for your GridView::
<asp:GridView ID="gridData" EnableViewState="false" ... />.
On using any of above options, the OnRowDataBound event is executed each time postback happens, thus recreating the Dynamic controls. Now you can access the Hyperlinks in your button click event.
Check properly which one of above options is best for your application.

Related

Create dropdownlist and two textbox each time when click button

I am trying on every button click to add a drop-down list (filled from a database using EF) and two textboxes dynamically,
and after that store tha value from list and textbox into the database.
How can I do that using asp.nrt(vb) and EF?
First you will want to set up a Panel on the Client side to hold your new Textbox, and dropdownlists like below:
<asp:Panel ID="pnlButton" runat="server" Width="100%"/>
Then you will want to have a event set up like this for your button click.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim tb As New TextBox
tb.ID = "TextBox1"
Dim ddl As New DropDownList
ddl.ID = "DropDownList1"
ddl.DataSource = 'your data source from the Database'
pnlButton.Controls.Add(tb)
pnlButton.Controls.Add(ddl)
End Sub
This should get you started on atleast creating the controls dynamically.

What is the proper way to use data from a grid view before data binding

I have a user control that creates a grid view of database records. Each record has a checkbox for deleting the record.
My problem is that the the page_load event builds the grid view, then the delete button even fires. The deleteButton_click event is looping over the gridview looking for checked boxes but never finds any because the 'page_load' event just gave me a clean gridview. What is the best way to check for checked boxes before the grid view is re-built? Or can I get the checked values without looking at the grid view?
Protected Sub Page_Load(...) Handles Me.Load
'db calls and other code
gv.DataBind()
End Sub
Protected Sub btnDelAtt_Click(...) Handles btnDelAtt.Click
For Each grdRow As GridViewRow In gvFileViewer.Row
Dim chkBox As CheckBox = CType(grdRow.FindControl("cbItem"), CheckBox)
If chkBox.Checked = True Then 'this is always false thanks to page_load
'code that does not run
end if
next
end sub
As mentioned in the comments, adding !IsPostBack should do it.
You only need to load the grid from the database in the initial call, you don't need to get the data again when post back occurs. You will need to rebind the grid once the delete is over.
Protected Sub Page_Load(...) Handles Me.Load
If(!Page.IsPostBack)
'db calls and other code
gv.DataBind()
End Sub
Protected Sub btnDelAtt_Click(...) Handles btnDelAtt.Click
For Each grdRow As GridViewRow In gvFileViewer.Row
Dim chkBox As CheckBox = CType(grdRow.FindControl("cbItem"), CheckBox)
//Delete your record
end if
next
//Rebind grid
end sub

Dynamically show/hide columns in Gridview in ASP.NET

I'm attempting to show/hide columns in gridview for better viewing.
What I need to do is:
hide columns 8, 9 and 10 on page load
show them upon button click
I successfully hidden them on pageload using RowCreated Event (code below). but as of now, I haven't yet found a way to show them again via button click.
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim row As GridViewRow = e.Row
' Intitialize TableCell list
Dim columns As New List(Of TableCell)()
For Each column As DataControlField In GridView1.Columns
'Get the first Cell /Column
Dim cell As TableCell = row.Cells(0)
' Then Remove it after
row.Cells.Remove(cell)
'And Add it to the List Collections
columns.Add(cell)
Next
' Add cells
row.Cells.AddRange(columns.ToArray())
e.Row.Cells(8).Visible = False
e.Row.Cells(9).Visible = False
e.Row.Cells(10).Visible = False
e.Row.Cells(11).Visible = False
End Sub
I tried the following methods with unfortunate results:
Set width to 0px then reset to auto upon click- since most of my columns are itemfields that contains either button or checkbox, this doesn't work at all
Use GridView1.Columns(8).Visible = False - same reason as above
Create RowDataBound Event with e.Row.Cells(8).Visible = True but i can't successfully call this event via button click yet.
Please advise. Thanks in advance.
DataGridView1.Columns(n).Visible = False ' working in vb.net
where n represent the column index

Gridview - Greying out checkboxes once one is checked

I'm new to all of this code, so please be patient.
On my gridview, I am trying to make it so, once I check on of the checkboxes, the others get greyed out, making it impossible to select another. How would I code this?
Thanks in advance.
PS: I'm using vb, rather than C#.
Edit: Here is the code I want to run.
Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs) Handles btnOK.Click
Dim TxtbxID As TextBox
Dim RB As System.Web.UI.WebControls.RadioButton
For Each row As GridViewRow In GridView1.Rows
RB = row.FindControl("select")
If RB.Checked Then
Me.txtHiddenProblem.Text = row.Cells(3).Text & " on " & row.Cells(5).Text
TxtbxID = FormView1.FindControl("ProblemTextbox")
TxtbxID.Text = txtHiddenProblem.Text
End Sub
Take a look at this article: http://www.ezzylearning.com/tutorial.aspx?tid=5187857
It has some lines of code which help you determine the particular gridview row the checkbox element was associated with...
public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkStatus = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
//other code
}
Now all we need to do is when the checkbox element is checked, iterate through the grid, find the checkbox, and disable them if its not the same as the one which caused the event...

Referencing items within a gridview

I have a a gridview attached to an objectdatasource. In each row I have a bound textbox for input. I have a button beside the textbox in each row that launches a javascript popup for unit conversion.
The question is: how can i tell the unit converter (js function) what textbox (in which row) to populate the results with?
In the RowCreated event of the GridView:
Protected Sub MyGridView_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGridView.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim btn As Button = e.Row.Cells(0).FindControl("btnJavaScriptButton")
Dim txt As TextBox = e.Row.Cells(1).FindControl("txtResults")
btn.OnClientClick = "calculate(" & txt.ClientID & ");"
End If
End Sub
Where 0 and 1 are the indices of the columns that contain the button and the textbox, and "calculate" is the name of your JavaScript function.

Resources