I have an image button that is created on rowcreated manually in code.
Dim deletecshr As New ImageButton
deletecshr.ImageUrl = "\images\bttnDeletemini.gif"
deletecshr.ToolTip = "This Will Delete All Cashiers"
deletecshr.ID = "deletecshr"
In gridveiw_rowdatabound I have the following:
Dim deletecshr As ImageButton = DirectCast(e.Row.FindControl("deletecshr"), ImageButton)
deletecshr.CommandName = "Delete"
deletecshr.CommandArgument = emp_no & "," & e.Row.Cells(2).Text & "," & e.Row.Cells(1).Text & "," & cashier_no & "," & manager_no
I set the commandname and the argument.
However, in rowcommand it finds no commandname or anything else. What am i doing wrong? I believe this has worked before.
From what i can tell you never attach the event.
Dim deletecshr As New ImageButton
deletecshr.ImageUrl = "\images\bttnDeletemini.gif"
deletecshr.ToolTip = "This Will Delete All Cashiers"
deletecshr.ID = "deletecshr"
AddHandler deletecshr.Command AddressOf deletecshrCommandEventHandler
You'll have a method similar of this in your code:
Protected Sub deletecshrCommandEventHandler(sender As Object, e As CommandEventArgs)
If e.CommandName = "Delete" Then
'Do your stuff
End IF
End Sub
EDIT
Added some more code and fixed a small syntax error.
Related
I have a calendar control in my website (vb.net in asp.net)(the standard calendar control but I manipulated it and now it looks like outlook calendar)
The events are added to the calendar as dynamic buttons, and each button has uniqe ID which is the same even after postback.
This is my code to generate the button and add it to the appropiate cell in calendar:
Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
Dim nextDate As DateTime
If Not dsHearings Is Nothing Then
For Each dr As DataRow In dsHearings.Tables(0).Rows
nextDate = CType(dr(6), DateTime)
If nextDate = e.Day.Date Then
e.Cell.BackColor = System.Drawing.Color.LightGoldenrodYellow
Dim btn As New Button
btn.Text = Left(dr(7).ToString, 5) & "-" & "جلسة في ملف" & dr(1) & " " & dr(2) & " (" & dr(8) & ")"
btn.CssClass = "CalendarHearingEvent"
btn.BackColor = Drawing.Color.Red
btn.ToolTip = "جلسة في ملف" & dr(1) & " " & dr(2) & " (" & dr(8) & ")"
btn.ID = "btnHearings" & dr(9).ToString
btn.UseSubmitBehavior = True
AddHandler btn.Click, AddressOf Me.HearingButton_Click
Dim lbl As New Label
lbl.Text = "<br>"
e.Cell.Controls.Add(lbl)
e.Cell.Controls.Add(btn)
End If
Next
End If
And this is the Handling sub:
Private Sub HearingButton_Click(sender As Object, e As EventArgs)
End Sub
Everything is perfect, but the click event not firing
Please Help
I used LinkButton, and have set href property of link button to
lnkButton.Attributes("href") = e.SelectUrl
Code
lnkButton = New LinkButton()
lnkButton.CommandArgument = objScheduleDetail.SelectedDate
lnkButton.Text = <Write your text here>
lnkButton.ID = "lnkView" ' you can make it unique as well
lnkButton.Attributes("href") = e.SelectUrl
e.Cell.Controls.Add(lnkButton)
Now when this link button is clicked Calendar.SelectionChanged event is fired. and I use Calendar1.SelectedDate to get the date of the clicked button.
Protected Sub Calendar1_SelectionChanged(sender As Object, e As System.EventArgs) Handles Calendar1.SelectionChanged
Calendar1.SelectedDate ' date which is clicked.
End Sub
Thanks danish for your answer, but I need also to pass arguments. so I used your answer with extra code to meet the needs of my app.
The problem is that when a dynamic button inside calendar is clicked, it posts back the calendar event and not the button click event, so the app is not catching the button.
To bypass that, In my code behind I generated a linkbutton like this:
Dim btn As New LinkButton
btn.Text = "Text On LinkButton"
btn.CssClass = "CalendarEvent"
btn.ToolTip = "What ever"
btn.ID = "LinkButton1"
Dim str As String = "return EventonMyClientClick(" + dr(0).ToString + ");"
btn.OnClientClick = str
e.Cell.Controls.Add(btn)
This will generate a linkbutton inside calendar cell with OnClientClick pointing to a javascript function in HTML Code which goes like this:
<asp:LinkButton ID="LinkButton1" style="display:none;" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
<asp:HiddenField ID="hdEventSentId" runat="server" />
<script>
function EventonMyClientClick(val) {
document.getElementById('<%=hdEventSentId.ClientID%>').value = val;
var btn = document.getElementById('<%=LinkButton1.ClientID%>');
btn.click();
}
</script>
As you see, The dynamic linkbutton passes my argument (dr(0).ToString) to eventonmyclientclick function. the function gets the argument, saves it in the hiddenfield and then perform a click event for the linkbutton1 linkbutton.
the page posts back and catches the linkbutton1 click event, which calls the code behind handler of the linkbutton that handles the argument saved in the hiddenfield:
Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)
Try
Dim EventId As String = hdEventSentId.Value
....
Catch ex As Exception
End Try
End Sub
I hope this will help others seeking for a solution for the same problem.
I have a gridview which is going to be used to update a mysqldatabase, I only want to update 1 field,
It all works apart from getting the data from the input textbox on the gridview.
in the code below, strdocumentnumber = 77500 I want 77500 to come from the drawing No. column field of the updating row in the gridview and the value that i want to update is strAccess_Code also to come from the gridview Code column.
It all works if hard coding the values.
Private Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
'' do update..
Dim strdocumentNumber As String = 77500
Dim strAccess_Code As String = 8
dbConn = New MySqlConnection("Data Source=localhost;password=****;user id=root;database=drawlib;")
sql = "UPDATE core " & vbCrLf & _
"SET F6 ='" & strAccess_Code & "'" & vbCrLf & _
"WHERE F1 = '" & strdocumentNumber & "'"
Try
dbConn.Open()
dbcomm = New MySqlCommand(sql, dbConn)
dbread = dbcomm.ExecuteReader()
dbread.Close()
Catch myerror As MySqlException
MsgBox("Error in Saving Data: " & myerror.Message)
'dbread.Close()
Exit Sub
End Try
GridView1.EditIndex = -1
GridView1.DataSource = SqlDataSource2
GridView1.DataBind()
End Sub
thanks
I think you'll have to create a textbox in your codebehind to assign a value from your gridview textbox:
Edit
You may need to find the proper row before pulling the value from the textbox. Try this
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
If row.RowType = DataControlRowType.DataRow Then
Dim textbox As TextBox = CType(row.FindControl("nameofyourgridview textbox here"),TextBox)
strdocumentNumber = textbox.Text
End If
/Edit
In your Gridview, you'll need to change your BoundField to a TemplateField. See here for an example.
Use the designer to make the conversion (see link above) or select your column in the gridview designer and click the 'convert this column to a templatefield' button. You'll then have a textbox that you can name and grab from the code-behind.
After the DetailsView is updated and clicked the Update Command button on the DetailView; I want to set one of the fields to “Complete”. What am I doing wrong here? Pls. help.
here is my code behind:
Protected Sub Test(sender As Object, e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView1.ItemUpdated
If IsPostBack Then
Dim TextBox7 As TextBox = TryCast(DetailsView1.Rows(0).Cells(0).FindControl("TextBox7"), TextBox)
TextBox7.Text = "Complete"
End If
End Sub
I have used this method and got it to work.
Protected Sub PageDetailsView_ItemCommand(sender As Object, e As DetailsViewCommandEventArgs)
If Me.PageDetailsView.DefaultMode = DetailsViewMode.[ReadOnly] Then
If e.CommandName.Equals("GenerateModifiedDate", StringComparison.CurrentCultureIgnoreCase) Then
Dim modifiedDateLabel As Label = TryCast(Me.PageDetailsView.FindControl("ModifiedDateLabel"), Label)
modifiedDateLabel.Text = DateTime.Now.ToString()
End If
Else
If e.CommandName.Equals("GenerateModifiedDate", StringComparison.CurrentCultureIgnoreCase) Then
Dim modifiedDateTextbox As TextBox = TryCast(Me.PageDetailsView.FindControl("ModifiedDateTextbox"), TextBox)
modifiedDateTextbox.Text = DateTime.Now.ToString()
End If
End If
End Sub
I am dynamically adding a link button to every cell in a gridview. Adding the button works however the firing of the even handler doesn't. i need the linkbutton to call a function and pass some data for processing. My code is below. I have found solutions from the site that have gotten me this far.
At the moment the gridview loads the cells with buttons in blue. when you click them they go back to plain text and no function is called.
Private Sub gv_datasource_options_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_datasource_options.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow Then
For Each c As TableCell In e.Row.Cells
If Len(c.Text) > 0 And c.Text <> " " Then
Dim v_lb As New LinkButton()
v_lb.Text = c.Text
AddHandler v_lb.Click, AddressOf add_datasource
v_lb.Attributes.Add("AutoPostback", "True")
v_lb.Attributes.Add("runat", "Server")
v_lb.Attributes.Add("AutoEventWireup", "True")
v_lb.CommandName = "NumClick"
v_lb.CommandArgument = e.Row.Cells(0).ToString & "|" & gv_datasource_options.HeaderRow.Cells(e.Row.Cells.GetCellIndex(c)).Text
c.Controls.Add(v_lb)
Dim sm As ScriptManager = ScriptManager.GetCurrent(Me)
sm.RegisterAsyncPostBackControl(v_lb)
End If
Next
End If
Catch ex As Exception
cl_Error.cl_Erorr.LogError(ex, txt_userid.Value, ex.ToString)
End Try
End Sub
Private Sub add_datasource(sender As Object, e As CommandEventArgs)
Try
hf_datasource_id.Value = Left(e.CommandArgument.ToString(), (Len(e.CommandArgument.ToString) - InStr(e.CommandArgument.ToString, "|")))
hf_datasource_column.Value = Left(e.CommandArgument.ToString(), (Len(e.CommandArgument.ToString) - InStr(e.CommandArgument.ToString, "|")))
hf_datasource_tableid.Value = tv_content.SelectedValue
p_datasource.Visible = False
Catch ex As Exception
cl_Error.cl_Erorr.LogError(ex, txt_userid.Value, ex.ToString)
End Try
End Sub
Rather than adding Event ("add_datasource") to Linkbutton, trying using GridView's RowCommand Event. You will surely be able to fetch the event bubbled by linkbutton on Gridview's Row Command Event (along with the commandname and commandarguments)
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.