Checkbox OnCheckedChanged event not firing - asp.net

I have a datagrid that is populated through user input, and in that grid I have a checkbox that should remove the row from the datagrid if checked. I have set the EnableViewState to true and false with no results. It appears that if the checkbox is clicked, the event does not fire. Debugging never gets to the event. Can anyone point me? Here's the markup:
<asp:TemplateColumn HeaderText="Remove">
<ItemTemplate>
<asp:checkbox ID="chkSelection" runat="server" AutoPostBack="True" Checked="false" OnCheckChanged="EPACheck_Clicked"/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
And here's the code behind for the event:
Protected Sub EPACheck_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim ckbx As CheckBox = CType(sender, CheckBox)
Dim dgItem As DataGridItem = CType(ckbx.NamingContainer, DataGridItem)
Dim rcDelete As String = dgItem.ItemIndex
CType(Session("mytable"), DataTable).Rows.RemoveAt(Convert.ToInt32(rcDelete))
BindgrdEPA()
End Sub

Your method doesn't handle the event. Try the below snippet.
Protected Sub EPACheck_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Handles chkSelection.CheckedChanged

The reason OnCheckedChanged wasn't firing for me was because I disabled IE8's Protected Mode.
Once I enabled Protected Mode OnCheckedChanged worked.
Previously, I had disabled Protected Mode to test a cookie problem with one of our web applications. I didn't re-enable Protected Mode when I finished that task.
To re-enable Protected Mode I did this:
Clicked on Tools\Internet Options.
Clicked on Security tab.
Clicked on Reset all zones to default level.
I suppose I could have clicked "Enable Protected Mode" checkbox.

Related

odd behavior when when changing linkbutton text

In my project, I have a datarepeater, in an updatepanel, with a linkbutton. When the user clicks the linkbutton, a partial post back occurs and changes the text property of the linkbutton. The problem is: when the user clicks the same button again, even though i can physically see the new text value on the web page, when the debugger hits the event, the sender object says the text value is the old value, not the last updated. Anyone know why? Here is the code:
<asp:LinkButton runat="server" ID="lbEdit" Text="Edit" EnableViewState="true" OnClick="edit_click" CommandArgument='<%# Eval("user.networkId") %>'></asp:LinkButton>
codebehind:
Protected Sub edit_click(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As LinkButton = CType(sender, LinkButton)
Dim userId As String = btn.CommandArgument
If (btn.Text = "Edit") Then
btn.Text = "Save"
else
btn.Text = "Edit"
end if
end sub
Most likely you are rebinding your data on postback, so it is resetting your value. Then the button click event is firing, so yes you are setting Text correctly. But when you postback again, the text is getting reset.
Wrap your DataBind event in a If Not IsPostBack:
If Not IsPostBack
'Bind your datasource
End If

Add Row Click Event to Asp.Net GridView Rows

Is there a way to add a RowClick Event to Gridview Rows without adding a button to each one? I want to be able to click anywhere in the row and raise the SelectedIndexChanged event.
I tried the following but I need to add pages enableEventValidation="true" and I really don't want to do that.
Private Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Try
Select Case e.Row.RowType
Case DataControlRowType.DataRow
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" & e.Row.RowIndex))
End Select
Catch ex As Exception
End Try
End Sub
The RowCommands wont work either because you have to have a button.
You did step 1 of 2. First part was registering the onclick in the RowDataBound. Now you need to handle the SelectedIndexChanged event. I'm not a VB.Net guy, so I'm not 100% sure on how to wire it up. The event handler will look like this, though:
Private Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
' Do stuff here.
End Sub
Also, change your onclick wire up code to use Page.ClientScript.GetPostBackEventReference instead of Page.ClientScript.GetPostBackClientHyperlink.
Here is some example markup:
<asp:GridView ID="GridView1"
runat="server"
Width="665px"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<HeaderStyle BackColor="#0033CC" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>

trigger asp:button's click event by clicking another asp:button

i have two asp:buttons.. say button1 and button2, what i would like to do is fire button2's click event when I click button1.. is there a way to trigger click event on an asp:button through code behind? please help, newbie here.
Well, you can put the same event for both buttons, something like this:
<asp:Button ID="btn1" runat="server" Text="Button 1" CommandName="Save" />
<asp:Button ID="btn2" runat="server" Text="Button 2" CommandName="Cancel" />
and in code behind (vb.net):
Protected Sub btn_event(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click, btn2.Click
Dim btn As Button = CType(sender, Button)
'now you have the button instance on sender object, and you can check the ID property or CommandName property do solve what you want to do!
End Sub
C# code:
protected void btn_event(object sender, EventArgs e) {
Button btn = (Button)sender;
//now you have the button instance on sender object, and you can check the ID property or CommandName property do solve what you want to do!
}
If you're using C#, remember to set the event click on asp:button tag.

add an initial line-through text-decoration on a label in a asp listview

My goal is to display a list of tasks with checkboxes. Checking the checkbox for a task will update the display of the task so a line-through text decoration will appear. Everythign is working great, except I can't figure out how to initially show the list of tasks with a line-through if they are completed, or normal if the task is not completed. Here's code excerpts:
<asp:Listview .../>
...
<ItemTemplate>
<asp:HiddenField ID="TaskCompleted" runat="server" Value='<%#Bind("TaskCompleted")%>'/
<asp:Checkbox ID="CompletedCheckbox" runat="server" AutoPostBack="True" OnCheckedChanged="CompletedCheckboxChange" Checked='<%#IIf(Eval("TaskCompleted"), "True", "False")%>' />
<asp:Label id="TaskLabel" text='<%#Eval("TaskDesc")%>' runat="server" />
</ItemTemplate>
...
</asp:ListView>
Then the code behind (minus the database stuff which works fine):
Protected Sub CompletedCheckboxChange( ByVal sender As Object, ByVal e As EventArgs )
Dim Completed As CheckBox = TryCast( sender, CheckBox )
Dim AnnualProgramTasksId as HiddenField = TryCast(Completed.Parent.FindControl("AnnualProgramTasksId"), HiddenField)
Dim TaskLabel As Label = TryCast(Completed.Parent.FindControl("TaskLabel"), Label)
If Completed.Checked Then
'update task displayed, give it a line-through
TaskLabel.Style("text-decoration") = "line-through"
Else
'update task displayed, give it a line-through
TaskLabel.Style("text-decoration") = "none"
End If
End Sub
So this works great, when I click on a checkbox the lable gets a line-through or none based on the checkbox. Only problem is when I initially load the page, I can't figure out how to update the style of TaskLabel to show a line-through or not. I've tried some various routes, but nothing is panning out. Any ideas?
This is how I have always done something like whay you are trying to.
Try
<asp:Label id="TaskLabel" text='<%#Eval("TaskDesc")%>' runat="server"
OnDataBinding="TaskLabel_DataBinding" />
and
Protected Sub TaskLabel_DataBinding( ByVal sender As Object, ByVal e As EventArgs )
Dim Completed As CheckBox = TryCast(DirectCast( sender, Control).Parent.FindControl("CompletedCheckbox"), CheckBox)
CompletedCheckboxChange(Completed, EventArgs.Empty)
End Sub

Button Click and LinkButton Click

I am having a strange day! I am trying to access values inside the Datagrid control. There are textboxes in each row of the Datagrid control. If I use a asp.net button control outside the datagrid control I can access all the values. But if I use LinkButton outside the datagrid control then I cannot access any value.
I have to use LinkButton.
UPDATE 1: Code
//This works
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim indexes() As Integer = dgReceipts.SelectedIndexNumbers
End Sub
//This does not work and I get 0 items in the SelectedIndexNumbers
Protected Sub LinkButtonUpdateReceipts_Click(sender As Object, e As EventArgs) Handles LinkButtonUpdateReceipts.Click
Dim indexes() As Integer = dgReceipts.SelectedIndexNumbers
End Sub
<asp:Button ID="Button1" runat="server" Text="Button" />
<c1:LinkButton ID="LinkButtonUpdateReceipts" runat="server" Text="Update Totals" Icon="images/go-blue-right.gif"></c1:LinkButton>
I'm suprised that either of those work. You do not appear to have an OnClick event for either of those buttons. At a minimum I would add an OnClick event to your LinkButton that fires LinkButtonUpdateReceipts_Click.
Also, why aren't you just using <asp:LinkButton? what is <c1:LinkButton?

Resources