Gridview / autopostback problem

P

postings

Hi

I have problems with autopostback and the gridview control. Please
continue reading.

The code below turns a row red when a tickbox is ticked in
edititemtemplate. Note autopostback on the tickbox is enabled and the
code works perfectly.

-------------------------------------------------------------------------------------------------------------------
Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e
As System.EventArgs)

Dim checkbox As CheckBox = CType(sender, CheckBox)
Dim row As GridViewRow = CType(checkbox.NamingContainer, GridViewRow)

If checkbox.Checked = True Then
row.BackColor = Drawing.Color.Red

'mygridview.Columns(0).Visible = False

end if
end sub
-------------------------------------------------------------------------------------------------------------------

However if I uncomment:
mygridview.Columns(0).Visible = False

....then the behaviour changes.

The first time I click the tickbox the column hides and the tickbox
doesn't get ticked like it should. The second time I click the
textbox the row turns red (column remains hidden).

I need the column hidden and the row colour to change on one
autopostback, not two. How can I achieve this?

Many thanks

Alex
 
S

S. Justin Gengo

Alex,

I haven't been able to replicate the behaviour you describe. But I do have a
small example that works exactly the way you describe:

<asp:GridView ID="MyGridView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="MyCheckBox" runat="server"
AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Not Page.IsPostBack Then
Dim DataTable As New Data.DataTable
DataTable.Columns.Add("Column1", GetType(String))
DataTable.Columns.Add("Column2", GetType(String))
Dim DataRow As Data.DataRow = DataTable.NewRow
DataRow.Item(0) = "Test1"
DataRow.Item(1) = "Test2"
DataTable.Rows.Add(DataRow)
MyGridView.DataSource = DataTable
MyGridView.DataBind()
End If
End Sub

Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim checkbox As CheckBox = CType(sender, CheckBox)
Dim row As GridViewRow = CType(checkbox.NamingContainer, GridViewRow)
If checkbox.Checked = True Then
row.BackColor = Drawing.Color.Red
mygridview.Columns(0).Visible = False
End If
End Sub


Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top