Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I added checkbox control to the GridView column dynamically. On each GridView_RowBound() event, checkbox is being added to the column. Also defined, CheckBox_CheckedChanged event in the RowBound() event as below



VB
<pre>Protected Sub GridviewChildItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow AndAlso Not String.IsNullOrEmpty(CRMSignCond) Then
        Dim lbValue As Label = DirectCast(e.Row.Cells(5).FindControl("lbValue"), Label)
        e.Row.Cells(5).Attributes.Add("onmousemove", "Show('" + lbValue.Text + "')")
        e.Row.Cells(5).Attributes.Add("onmouseout", "this.style.backgroundColor=this.oldcolor;Hide();")
    End If

    AddTemplateControls(Nothing, e)


End Sub
Private Sub AddTemplateControls(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
    Dim cbTargetSign As New CheckBox
    Dim rbConsolidate As New RadioButtonList
    Dim tbSignGrp As New TextBox

    cbTargetSign.ID = "chkSelect"
    cbTargetSign.AutoPostBack = False
    cbTargetSign.Checked = True
    rbConsolidate.ID = "rbConsolidate"
    tbSignGrp.ID = "tbSigningGroup"
    tbSignGrp.Width = 25
    If Not e.Row.RowIndex = -1 Then
        e.Row.Cells(6).Controls.Add(cbTargetSign)
        e.Row.Cells(4).Controls.Add(tbSignGrp)
        e.Row.Cells(7).Controls.Add(rbConsolidate)
    End If
    rbConsolidate.RepeatDirection = RepeatDirection.Horizontal
    rbConsolidate.Items.Add("Yes")
    rbConsolidate.Items.Add("No")
    rbConsolidate.Items(1).Selected = CBool(True)
    If cbTargetSign.Checked Then
        rbConsolidate.Enabled = False
    End If
    **AddHandler cbTargetSign.CheckedChanged, AddressOf cbTargetSign_CheckedChanged**
End Sub


Checkbox- CheckedChanged event.

<pre lang="vb">Public Sub cbTargetSign_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Every time, when i check the checkbox in the grid, checkedChanged event doesn't trigger. Anyone guide me how to resolve this ?

Note: I don't want to set AutoPostBack of checkbox to TRUE since it reloads the entire grid with default values.

What I have tried:

I am not having any clue to proceed further, expecting guidance.
Posted
Updated 17-May-17 4:08am

You really have a couple of options and the right one depends on what you are doing in your CheckedChanged event.

1. Set AutoPostBack to True, let the page complete its PostBack and handle any logic that needs to be done.

2. Possibly look at placing your grid in an UpdatePanel. You can then follow step 1, but only the contents of the panel will be refreshed not the entire page.

3. Looking at using client side jQuery or JavaScript to control the change.
 
Share this answer
 
You need to get a better understanding on web and asp.net architecture. Your server code only runs to generate html\js which is then sent to the client to execute, your .net code isn't running inside the browser, it can't respond to client events. It can only run when you do a postback which means setting AutoPostBack to true and having the whole page re-post. If you don't want the page refreshing in the browser you can use an UpdatePanel which converts your checkbox event into an ajax call instead. The page lifecycle will still run but the browser will only update the components in the UpdatePanel. Other than that it's a case of writing your own ajax code.
 
Share this answer
 
Comments
VinKot 17-May-17 10:21am    
Thanks for your comments, i am actually a Windows app developer and new to web app. I better understand what you mentioned.

Could you please clarify below Query ,

I have placed the Grid (which has the Checkbox as template field) inside the updatePanel, so on AutoPostBack Page_Load() event is fired, does it mean entire page is loading again?

2) After setting AutoPostBack to TRUE, on CheckBox click, CheckedChanged event is fired. Upon UnCheck it's not triggred.

Later, i set Checked = True on page load, now event is triggered when Unchecked and not when Checked.

May i know the reason for this behavior ?
F-ES Sitecore 17-May-17 10:29am    
With an updatepanel the page load event etc will still trigger as the html for the page is still being built as normal, the only difference is that rather than sending the whole html to the page it works out what needs updated just for what is in the updatepanel and manages that via javascript.

As for the other issue if you are manually attaching the autopostback=true property in your code then you have to attach that event with every postback, it won't be "remembered" automatically. So it could be you are setting it on page load, but after the autopostback you are not setting it again so the checkboxes no longer autopostback.
VinKot 17-May-17 10:46am    
After PostBack i am setting the event in GridView_RowBound().

Everytime on PostBack , template controls are getting created by Grid and it calls then RowBound event. But not hitting the CheckedChanged().

Copied my source code above for your reference.
F-ES Sitecore 17-May-17 10:54am    
Is GridView_RowBound being called on postback though? ie have you set a breakpoint there in the debugger and confirmed that code is being executed rather than just assuming it is?
VinKot 17-May-17 11:00am    
Yes, it's being called on postback. I verified by putting breakpoint while debugging.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900