How to retrieve the selected row value using command row in gridview? - asp.net

i have a button in gridview whose command name is "hold" i want when i click n hold button of first row then the whole row values will be transfererd to default2.aspx ...
and if i click the hold button of gridview second row then the second row value will be transferred to Default2.aspx ,,how to do that ?
i m using that ...but it always transfer the 1st row value even i select first last or middle row ..in gridview ....
whats wrong in this code ?
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
For Each myRow As GridViewRow In GridView1.Rows
'Find the checkbox
Dim lab5 As Label = DirectCast(myRow.FindControl("Label5"), Label)
If e.CommandName = "feedback" Then
Me.Response.Redirect("~/view_feedback.aspx?" & "serv_code=" & lab5.Text.ToString)
End If
Next
End Sub

...assuming your posting back the whole page and not using ajax the GridView1_RowCommand will be fired whenever your hold button is clicked, immediately.
As your code is written now you're trying to loop through all rows, and parsing them in turn. However, you are redirecting to your view_feedback.aspx page on the first row if your commandname is Feedback. The first row will be the only one ever processed because your redirect moves the current web request's execution to the new page.
Pass the row index to your command in the CommandArgument property as specified in this example and use it rather than looping through all rows (if you're only processing a single row upon button click anyway).

try this code
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "feedback" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim myRow As GridViewRow = GridView1.Rows(index)
Dim lab5 As Label = DirectCast(myRow.FindControl("Label5"), Label)
Me.Response.Redirect("~/view_feedback.aspx?" & "serv_code=" & lab5.Text.ToString)
End If
End Sub

Related

How do I go to the next item in Drop Downlist on Button Click

below is my code. this selects the first but 1 and it does not go again
Dim idx As Integer = Me.Dpdtokens.SelectedIndex
If idx <> Me.Dpdtokens.SelectedIndex - 1 Then
Me.Dpdtokens.SelectedIndex = idx + 1
Else
Me.Dpdtokens.SelectedIndex = 0
End If
This works for me:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If cboHotel.SelectedIndex >= 0 Then
' user has selected item, increase by 1
If cboHotel.SelectedIndex < cboHotel.Items.Count - 1 Then
cboHotel.SelectedIndex += 1
End If
Else
' no selection at all - lets start at 0
cboHotel.SelectedIndex = 0
End If
End Sub
So, if user not selected anything (and you allowed that, then selected index is -1. However, by default it will start out with the first item as being selected.
It also not clear what/why your logic grabs selected index, and then on next link checks if they are different? (they can't be).
Edit: ==============================
Also, it not been shown how the dropdown/combo box is being loaded up with values.
For example, I have this on page load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadCombo()
End If
End Sub
Sub LoadCombo()
Dim rstHotels As DataTable = New DataTable
Using conn As New SqlConnection(My.Settings.TEST4)
Dim strSQL As String =
"SELECT ID, HotelName, City, Province FROM tblHotels ORDER BY HotelName"
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
rstHotels.Load(cmdSQL.ExecuteReader)
End Using
End Using
cboHotel.DataSource = rstHotels
cboHotel.DataBind()
End Sub
Note VERY careful how I check for is postback. And thus do not re-load the combo box over and over. If you re-load and re-bind the combo box each time on page load, then the selected index etc. will be lost.
and the above advice applies to gridview, listbox etc. In other words, while we have a page load event, we only want to load up controls on FIRST page load. Since all other buttons and events ALSO WILL trigger page load over and over for each post-back, then if we change the index of the combo box, but re-load each time, then the index location will be lost.
Hence, in general, loading up of such controls like combo box, gridviews etc. should only occur one time, and the first time the page loads.
This goal is achieved by checking and using a If Not IsPostBack code stub in the page on-load event.

asp.net vb.net gridview, get row id when pressing template field button

I have a template field with a button. I want to press the button and get the row Id so I can return the users selection. I have a regular select button which works fine but the user want the people who are not employee’s to hide the select button. I have this working but since it’s a template field it fires the RoW_command procedure and I cannot seem to get the row index since it is a template field and not the regular Select button. Or I cannot seem to name the Regular select button since it Command field does not have a Name or ID property?
Like I said this works hiding the template field called btnSelect
Private Sub GridView3_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView3.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If (DataBinder.Eval(e.Row.DataItem, "LegacyPersonType") <> "Employee") Then
e.Row.ForeColor = Drawing.Color.Black
e.Row.BackColor = Drawing.Color.Yellow ' This will make row back color yellow
e.Row.FindControl("btnSelect").Visible = False
Else
e.Row.ForeColor = Drawing.Color.Black
e.Row.BackColor = Drawing.Color.White ' the normal employees make white
End If
End If
End Sub
I need to find the Row index when I press btnSelect
Private Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
Dim index As Integer = Convert.ToInt32(e.CommandArgument) ‘ Error invalid
‘ in other words when pressing the tem-plate field there is no e.CommandArgument
‘But clicking the regular select there is
Dim row As GridViewRow = GridView3.Rows(index)
Session("EnterpriseID") = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(2).Text)
Dim EmployeeType As String = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(7).Text)
Dim CommonName As String = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(1).Text)
Dim EnterpriseID = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(6).Text)
The GridViewRow object knows its row index via the RowIndex property in the RowCommand event, like this:
Private Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
' Get the row by finding the grandparent of the control (button)
' that initiated the command
Dim theGridViewRow As GridViewRow
theGridViewRow = CType(CType(e.CommandSource, Button).Parent.Parent, GridViewRow)
' Get the index value from the grid view row
Dim index As Integer = theGridViewRow.RowIndex
End Sub
Ok I figured it out myself, I made the default Select button on the grid a Template field. since it was the default button it had the necessary code to make it fire. This was the problem with making a template field from scratch. And by making it a template field it then also gave it a name. So in the RowDataBound code the FindControl method above hides it when a person is not an employee. – R2 Builder 1 min ago edit

Programmatically update a bound checkbox in a GridView using an asp:ButtonField in ASP.NET 2005

The GridView is bound to an Access Table, but I want to update the checkbox using the asp:ButtonField. The best I've been able to do is replace the checkbox with a string value. Below is the code I have now:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "check" Then
'Enable Editing on the GridView
GridView1.EditIndex = GridView1.SelectedIndex
'Assign value to the row selected
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
'Assign selected gridview row
Dim selectedRow As GridViewRow = GridView1.Rows(index)
'Assign checkbox cell as table cell
Dim selectedCell As TableCell = selectedRow.Cells(0)
'Assign a value to the checkbox, **but instead is replacing the checkbox**
selectedCell.Text = "True"
End If
End Sub
I think you need to use .FindControl().
The GridViewCell is not the same thing as a CheckBox control inside it. You code should probably look more like:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "check" Then
'Enable Editing on the GridView
GridView1.EditIndex = GridView1.SelectedIndex
'Assign value to the row selected
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
'Assign selected gridview row
Dim checkBox As CheckBox = GridView1.Rows(index).FindControl("checkBoxIdGoesHere")
checkBox.Checked = true 'or false or whatever based on your command.
End If
End Sub
I've had my own share of problems accessing controls from within GridViews. I solved something similar about a month ago. Assuming that your checkbox control is the only thing in that cell, you could try something like this:
'Assign a value to the checkbox
CType(GridView1.Rows(index).Cells(0).Controls(0), CheckBox).Checked = True
Try that and see if it works. If not, post back and we can take another look.

How to get Command Functionality from a Panel

Is there a Panel or any container with CommandName, CommandArguments instead of using Buttons (LinkButton, ImageButton, ...)?
I want to add a column in a GridView to make selection for the row, the whole cell's rectangle instead of a Select link.
You can make (almost) anything implement CommandName and CommandArguments by implementing the IButtonControl interface. You will probably also want to implement the IPostBackEventHandler interface.
This article covers in detail exactly what you are asking about, generally: making a Panel into a command control. It's not entirely trivial.
But making a table row clickable is a lot easier, though, esp. using jQuery. See this article. You just bind an event to the row, and go from there. In this example, they're redirecting to the url for a link in the row on a click event. You could just as easily do something else like call __doPostBack to cause an async postback and run arbitrary server code, e.g.
Here is another way how you can make a GridView-Cell as Sort-Column(see in RowCreated, the rest is sample-data):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindGridView()
End If
End Sub
Private Sub BindGridView()
Dim source As New List(Of String)(New String() {"1. row", "2. row", "3. row", "4. row", "5. row"})
Me.GridView1.DataSource = source
Me.GridView1.DataBind()
End Sub
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim cell As New TableCell
If e.Row.RowType = DataControlRowType.DataRow Then
cell.Text = "select"
cell.ToolTip = "click to select row"
cell.Attributes("onmouseover") = "this.style.cursor='pointer';"
cell.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(DirectCast(sender, GridView), "Select$" & e.Row.RowIndex)
End If
e.Row.Cells.Add(cell)
End Sub
Edit: if you get a "Invalid postback or callback argument"-Exception from ASP.Net, you can either set EnableEventValidation="False" or add following to your page's codebehind:
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
ClientScript.RegisterForEventValidation(GridView1.UniqueID, "Select$" & row.RowIndex)
End If
Next
MyBase.Render(writer)
End Sub
If you need C#, copy all into: http://www.developerfusion.com/tools/convert/vb-to-csharp/

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