ASP.net - Set focus to editing row - asp.net

How do you set focus to a specific control in a datagridview row after clicking the edit button? I'm able to do it for a new row when the grid is binding, but not for an existing row. The control doesn't seem to exist yet.
'This doesn't work (existing row)
Protected Sub gvDays_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles gvDays.RowEditing
Try
gvDays.EditIndex = e.NewEditIndex
gvDays.Rows(e.NewEditIndex).FindControl("txtDayText").Focus()
Catch ex As Exception
Helper.WriteException(ex)
End Try
End Sub
'This does work for a newly bound row
Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
If e.Row.RowState = DataControlRowState.Edit Then
e.Row.Cells(3).Controls(0).Focus()
End If
End Sub

gvDays_RowDataBound should work, the problem is you are looking at e.Row.RowState using the = operator, but RowState is a bitflag
try this
Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then
e.Row.Cells(3).Controls(0).Focus()
End If
End Sub

Related

Click event cannot be fired on buttons created dynamically in VB.Net ASP.Net

I'm creating a code in Page_PreRender function to dynamically creates some labels and buttons:
Dim btnExcludeDr As New Button()
btnExcludeDr.ID = "btnExcludeDr"
btnExcludeDr.Text = "Rate Driver"
form1.Controls.Add(btnExcludeDr)
AddHandler btnExcludeDr.Click, AddressOf Me.cmdExcludeDrv_Click
And the event which must be fired for each btnExcludeDr button is:
Protected Sub cmdExcludeDrv_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("hello")
End Sub
But the event is not fired. Do you have any solution? thank you !
The best place to create your dynamic controls is in the Page_Init function that the page code-behind class provides.
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim btnExcludeDr As New Button()
btnExcludeDr.ID = "btnExcludeDr"
btnExcludeDr.Text = "Rate Driver"
form1.Controls.Add(btnExcludeDr)
AddHandler btnExcludeDr.Click, AddressOf Me.cmdExcludeDrv_Click
End Sub
Protected Sub cmdExcludeDrv_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("hello")
End Sub

ASP.Net GridView - how to get selected column Index (in Dynamic Gridview)

I have n number of dynamically generated GridViews in my asp.net page. In all the GridView all the cells are linkbuttons which also dynamically generated. And I want to get selected cells/ linkbuttons index.
How do i do it?
Note- I dont have GridView name as it is dynamically generated in loop.
code as below-
Protected Sub Btncalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btncalculate.Click
Call GetFinal()
End Sub
Public Sub GetFinal()
For count As Integer = 0 To dtValue.Rows.Count - 1
Dim GrdView1 As New GridView()
'DataTable filling Code goes here
GrdView1.DataSource = dt1
AddHandler GrdView1.SelectedIndexChanged, AddressOf GrdView1_SelectedIndexChanged
AddHandler GrdView1.RowDataBound, AddressOf GrdView1_RowDataBound
AddHandler GrdView1.RowCommand, AddressOf GrdView1_RowCommand
GrdView1.DataBind()
end sub
Public Sub GrdView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' All cell to LinkButton code goes here
AddHandler lnkShow.Click, AddressOf LinkButton1_Click
end sub
You create linkButtons
Public Sub GrdView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
LinkButton lnkShow= new LinkButton();
lnkShow.Text = "Show";
lnkShow.CommandArgument = "Show";
lnkShow.CommandArgument = e.Row.Cell(0); ' For instance
End IF
End Sub
And then you handle the GridView1.RowCommand event:
Public Sub GrdView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "show" Then
LinkButton lnkShow= (LinkButton)sender;
argument = e.CommandArgument;
(do something)
End IF
End Sub
I thoink you dont need Link1_Click

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.

GridView_RowDataBound (If e.Row.RowType = DataControlRowType.DataRow)....include 2 IF's?

I've got a grid view which uses a "If e.Row.RowType = DataControlRowType.DataRow" to calculate the total of a column and hold this in the footer.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Totalnumbers += Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "RequestTotalnumbers"))
ElseIf
e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(3).Text = String.Format("{0}", Totalnumbers)
End If
However i now wish to also add....
If e.Row.RowType = DataControlRowType.DataRow Then
Dim datakey As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString()
End If
so that it can transfer on click to another page....
'Handle button click
Protected Sub RowClick(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) _
Handles GridView1.RowCommand
If e.CommandName = "Select" Then
'Add to session variable; translate the index of clicked to Primary Key
Session.Add("ID", GridView1.DataKeys(e.CommandArgument).Value.ToString)
Response.Redirect(" ")
End If
End Sub
I've tried combining to two if's together but have had no success...how can i do this?
Not quite sure, this may be what you want. Make sure you also added "datakeynames" property in your GridView.
'Handle button click
Protected Sub RowClick(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) _
Handles GridView1.RowCommand
If e.CommandName = "Select" Then
Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
'Add to session variable; translate the index of clicked to Primary Key
Session.Add("ID", GridView1.DataKeys(row.RowIndex).Value.ToString)
Response.Redirect(" ")
End If
End Sub

View state after postback VB?

I have a label's value that is posted back from a previous page and I want to use this label to do a simple calculation in the current page, but when I do the calculation the page (refreshed) the value of this label and automatically deleted the value (Since there would be no value in postback when it refreshed).
Here is the code behind file:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label2.Text = Request.Form("Hidden1")
End Sub
and here where I want to use the label
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Stotal As String
Stotal = Val(Label2.Text) * 10
Label3.Text = Stotal
End Sub
How can I save the value in the page via view state or any other method? Thanks in advance
Unless you've disabled ViewState on your page, the problem isn't that your label's ViewState isn't being saved, it's that you're overwriting it in your Page_Load method on the postback since the form variable Hidden1 is no longer being posted to your page from the previous page. Try:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack
Label2.Text = Request.Form("Hidden1")
End If
End Sub

Resources