I am trying to use a label from another row in the gridview and insert it in a table when the user clicks the button which is in its own column.
my problem is that i cant get bookno which finds the label set to the #bookno in my sqldatasource
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim selectedRow As Button = DirectCast(sender, Button)
Dim bookno As Label = CType(selectedRow.FindControl("label1"), Label)
Dim why As TextBox = CType(selectedRow.FindControl("textbox2"), TextBox)
Dim yesint As Integer = 1
Dim sql As SqlDataSource = CType(selectedRow.FindControl("sqldatasource4"), SqlDataSource)
sql.InsertParameters.Add(bookno.Text, 0)
sql.Insert()
End Sub
Try
Dim bookno As Label = CType(selectedRow.Parent.FindControl("label1"), Label)
and see if you get a valid control for bookno. If it works, do the same for other FindControl() calls.
Related
Actually my problem is i want to insert new row in a footer row of gridview while clicking the button which is outside of the gridview,i couldn't get the values of text box in the footer row of gridview
my coding is
Dim footerRow As GridViewRow = grvTBD_LoanProducts.FooterRow
Dim sStateCode As String = TryCast(footerRow.FindControl("txtStateCode"), TextBox).Text
Dim sEntryTypeID As String = TryCast(footerRow.FindControl("txtEntryTypeID"),TextBox).Text
the above coding didn't works help me to fix it thanks in advance
Try This code it might work:
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
Dim txtName As TextBox = TryCast(GridView1.FooterRow.FindControl("txtStateCode"), TextBox)
Dim aa As String = txtName.Text
End Sub
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.
I am using ItemTemplate & EditTemplate for editing the gridview. (ASP.net + VB).
I click EDIT button then I can check/uncheck those checkbox and amend the textbox value.
When click UPDATE button, it will fire the RowUpdating Event, But I found that when I get the value for update statement, it still get the value before editing, not updated value.
How can I get the latest & updated value ? Thanks.
Joe
The following is the VB code:
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
'Update the values.
Dim row = Gridview1.Rows(e.RowIndex)
Dim Col1_SL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_SL"), CheckBox)
Dim Col1_VL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_VL"), CheckBox)
Dim Col1_ML = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_ML"), CheckBox)
Dim Col1_PH = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_PH"), CheckBox)
Dim Col1_APH = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_APH"), CheckBox)
Dim Col1_TOIL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_TOIL"), CheckBox)
Dim Col1_Others = CType(Gridview1.Rows(e.RowIndex).FindControl("tb1_Others"), TextBox)
Dim Col1_RosterKey = CType(Gridview1.Rows(e.RowIndex).FindControl("lb1_rosterkey"), Label)
Using conn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("hris_shiftdutyConnectionString").ConnectionString)
conn.Open()
cmd.Connection = conn
sql = "SET DATEFORMAT dmy;UPDATE troster SET SL='" & Convert.ToInt32(Col1_SL.Checked) & "' where roster_key='" & Col1_RosterKey.Text & "';"
cmd.CommandText = Sql
reader = cmd.ExecuteReader()
conn.Close()
reader.Close()
End Using
'Reset the edit index.
Gridview1.EditIndex = -1
'Bind data to the GridView control.
BindData()
End Sub
The most likely reason will be this.
You are calling BindData() on Page_Load without using !IsPostBack
Protected Sub Page_Load Handles Me.Load
If Not IsPostBack
' Bind Grid only at the first load
' Do not load Grid again at Postbacks
BindData()
End If
End Sub
You have 2 options:
There is a separate RowUpdated method that is fired after updates have been performed by the data source that is bound to the grid view. This method will contain the new values for edits and inserts.
The RowUpdating method should have a parameter of type GridViewEventArgs. This will have a property called NewValues that contains the new values. In your example this was the variable e.
whats wrong in this code
i have a Imagebutton2 in gridview whose commandname is xxx and i have added modalpopup extendar panel2 with ImageButton2 i want when image button 2 will be clicked then the modalpopup will display and retrieve the values from selected gridview row to the literal3 control of panel 1 which acts as a modalpopup control for gridview ?
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim myrow As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
If e.CommandName = "xxx" Then
Dim lab5 As Label = DirectCast(myrow.FindControl("Label5"), Label)
Dim lit3 As Literal = Me.Panel2.FindControl("Literal3")
lit3.Text = lab5.Text
End If
End Sub
Many things can fail:
e.CommandArgument may not be an integer
e.CommandSource may not be of Control type
the NamingContainer may not be of GridViewRow type
myRow can be null, at least when the code is ran
the "Label5" control may not be found, or may not be of Label type, when the code is ran
the "Literal3" control may not be found (null ref) or may not be of Literal type, when the code is ran
...
I have a gridview i have customize gridview using labels .. i wanna copy the label in cell 2 to textbox outside gridview ?
Since your label is inside your GridView, you have to go through the rows within the GridView. You can use GridView's RowDataBound event go through the rows and access the value of inner control. Something like this..
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If Not e.Row.DataItem Is Nothing Then
Dim lbl As Label = CType(e.Row.Cells(1).FindControl("YourLabelName"), Label)
If lbl.Text = "Copy" Then
TextBox1.Text = lbl.Text
End If
End If
End If
End Sub
or if you want to copy particular row value, then
Dim lbl As Label = DirectCast(DirectCast(e.Item.FindControl("GridView1"), GridView).Rows(0).FindControl("YourLabelName"), Label)
TextBox1.Text = lbl.Text