Pass GridView Cell Value to Sub Routine in ASP.NET / VB.NET - asp.net

I have a GridView with Cell 0 containing the ID that I need to pass to a Public Sub.
I cannot figure out how to pick the value from Cell 0 in order to pass it to the Sub. I have tried experimenting (see the Dimmed EventID below) but have failed. Here is my code:
Protected Sub gvAppointmentsCalls_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvAppointmentsCalls.RowCommand
Dim EventID As String = gvAppointmentsCalls.Rows(e.RowIndex).Cells(0).Text
If e.CommandName = "gvAdd2Outlook" Then
Send_iCal_Call(EventID)
End If
End Sub
If I type the value directly e.g. Send_iCal_Call(123) then it works perfectly.
Public Sub Send_iCal_Call(ByRef Event_ID As Integer)
' My code in here
End Sub

Use the CommandArgument to pass the ID to the RowCommand-Handler.
For example:
CommandName="gvAdd2Outlook" CommandArgument='<%# Bind("EventID")%>'

In my opinion you should obtain your ID value within GridView's DataKeyNames property. You should define it in your grid markup this property like here
<asp:GridView DataKeyNames="EvenID">
and then will access it in code behind:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "gvAdd2Outlook" Then
Dim EventIDString As String = GridView1.DataKeys.Item(e.RowIndex).Value.ToString()
Dim EventID As Integer
If Integer.TryParse(EventIDString, EventID) = False Then Throw New ArgumentException("Wrong EventID=" & EventID)
Send_iCal_Call(EventID)
End If
End Sub

Related

Delete selected gridview row

trying to delete selected gridview row. I read few sites and used code however I am getting error. I think because most of the codes I've seen they using DataGridView, This code is within delete button event
For i = 0 To myGridView.Rows.Count - 1 Step i + 1
Dim delrow As GridViewRow = myGridView.Rows(i)
If delrow.Selected = True Then
myGridView.Rows.RemoveAt(i)
End If
Next
the two errors I am getting are:
1) RemoveAt is not a member of GridViewRowCollection
2) Selected is not a member of GridViewRow
Your help will be highly appreciated!
I usually delete rows on GridViews putting an ImageButton (with a trash bin) on every row with a Template Field, then on RowDataBound event I associate "DeleteRow" event and rowIndex to the CommandName and CommandArgument
Protected Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ib As ImageButton = e.Row.FindControl("ibDeleteRow")
ib.CommandName = "DeleteRow"
ib.CommandArgument = e.Row.RowIndex
End if
end sub
Private Sub myGridView_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "DeleteRow" Then
Dim rowToDeleteIndex as integer = e.CommandArgument
myGridView.DeleteRow(rowToDeleteIndex)
end if
end sub
Private Sub myGridView_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting
End Sub

Listbox.items.count is always 0?

I am trying to determine if a list box has any text in it. I tried using a custom validator and the code below always has a result of 0 or false either when there is text or no text in the list box? How can I properly determine if there is text in a list box?
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
args.IsValid = Listbox.Items.Count > 0
End Sub
Private Sub PopulateListBox()
If NameTextBox.Text = "" Then
Else
' Get value from text box
Dim textBoxValue As String = Me.NameTextBox.Text
' Create new item to add to list box
Dim newItem As New ListItem(textBoxValue)
' Add item to list box and set selected index
Listbox.Items.Add(newItem)
Listbox.SelectedIndex = Listbox.Items.Count - 1
End If
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End If
End Sub
By chance, is this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End If
End Sub
Is the IF statement supposed to be instead:
If Not IsPostBack Then
You currently have it not binding the first time, but everytime after.

ASP.NET Add Command Argument to Button in Code Behind

I am trying to grab an argument added to an asp:button in code behind, but getting error:
Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.CommandEventArgs'
This button is part of a normal form, not a gridview, etc..
What might I be doing wrong? Regards.
<form><asp:Button ID="btnPrint" runat="server" Text="Print" CommandArgument="" /></form>
Imports System.Web
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim String AS String = "MyValue"
btnPrint.CommandArgument = String
End Sub
Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As CommandEventArgs) Handles btnPrint.Click
Dim ID as String = e.CommandArgument.toString
'Have also tried
'Dim btn As Button = sender
'Dim ID As String = btn.CommandArgument
Response.Redirect("Print.aspx?tid=" & ID)
End Sub
You just need to update btnPrint_Click to handle the Command event instead of "Click":
Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As CommandEventArgs) Handles btnPrint.Command
My ASP button click events look like this:
Private Sub SearchASPxButton_Click(sender As Object, e As EventArgs) Handles SearchASPxButton.Click
Notice the second parameter is of type System.EventArgs. Did you change the type of the automatically generated click event? Or did you create this manually yourself?

VB.net/Asp.net loading a control programatically with a public property to be set

I have a vb.net asp application where I'm loading a control (from another control on a page). I want to set a variable between the two controls when it loads.
This is where I load the control:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
phCompanies.Controls.Add(AuditControl)
End Sub
On the control that's being loaded i've exposed the item i want to change as a property
Public Property resultType() As String
Get
Return m_resultType
End Get
Set(ByVal value As String)
m_resultType = value
End Set
End Property
Basically all it is doing is setting a parameter for my table adaptor
Public Sub Load_Data()
dtblog = New dsDECorp.ClientInfoDataTable
dtblog = tablog.GetAudits(m_resultType)
For Each rClient As dsDECorp.ClientInfoRow In dtblog
CID = rClient.ClientID
ClientName = rClient.CompanyName
Next
dtblog.Dispose()
End Sub
How do I pass the parameter through the property from the first control to the second when it loads?
Thanks
Tom
I'm a C# guy so forgive me if I make any syntax errors but the following should work for you:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
Dim AuditControlType As Type = AuditControl.GetType()
Dim AuditField As FieldInfo = AuditControlType.GetField("resultType")
AuditField.SetValue(AuditControlType, "Your Value")
phCompanies.Controls.Add(AuditControl)
End Sub

problem with asp textbox control

i have a textbox control in asp.net. textbox have a search button beside it. On clicking the search button i redirect to new page with the value in textbox. the new page also hase textbox and button beside it. I set the value sent from previous page to the textbox on new page. If i change the value on new page and click the search button it should take the new value. But it takes the previous value.
on page load method wrote the following code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
End Sub
On button click following code
Protected Sub bsrcnew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsrcnew.Click
Dim s As String
s = txtsrch.Text
If (flag.Equals(0)) Then
Response.Redirect("newSearch.aspx?str1=" + s)
ElseIf (flag.Equals(1)) Then
Response.Redirect("termsnew.aspx?str1=" + s)
End If
end sub
can any1 tell me how do i get changed value in textbox?
Try assigning it in a
If(!IsPostBack)
{
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
}
On buttonclick its again assigning the value from query string.
Use IsPostBack to set your text box only on the first run in the second page. Example:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack
str1 = Request.QueryString("str1").ToString()
flag = Request.QueryString("flg")
txtsrch.Text = str1
End If
End Sub

Resources