Solved

Change AutoPostBack setting on a datagrid column from server-side VB.NET code.

Posted on 2004-09-09
6
1,061 Views
Last Modified: 2012-08-13
I have a datagrid with columns that are bound to a datasource from SQL server table.  The HTML code is the following;

<asp:datagrid id="dgAddressCorrection" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 180px" AllowPaging="True" OnPageIndexChanged="dgAddressCorrection_Change" runat="server" CssClass="WebControl_DataGridDef" PageSize="100" Width="1468px" AutoGenerateColumns="False" AllowSorting="True">
<Columns>
      <asp:BoundColumn Visible="False" DataField="SourceID"></asp:BoundColumn>
      <asp:BoundColumn DataField="Name" SortExpression="Name" HeaderText="Name"></asp:BoundColumn>
      <asp:BoundColumn DataField="Address" SortExpression="Address" HeaderText="Address"></asp:BoundColumn>
      <asp:BoundColumn DataField="Phone" SortExpression="Phone" HeaderText="Main Phone"></asp:BoundColumn>
      <asp:TemplateColumn>
            <ItemTemplate>
                  <asp:Button id="btnEdit" Text="Edit" Runat="server" Width="50px" CommandName="EditAddressCorrection" CssClass="WebControl_ButtonDef"></asp:Button>
            </ItemTemplate>
      </asp:TemplateColumn>
      <asp:TemplateColumn HeaderText="Overridden" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                  <asp:CheckBox id="IsAddressOverriddenField" AutoPostBack="True" OnCheckedChanged="EnableSavebtn" Checked='<%# DataBinder.Eval(Container.DataItem,"IsAddressOverridden") %>' runat="server" CssClass="WebControl_CheckBox">
                  </asp:CheckBox>
            </ItemTemplate>
      </asp:TemplateColumn>
</Columns>
</asp:datagrid>

On the server-side, I turn the AutoPostBack to false on the CheckBox column (ID=IsAddressOverriddenField) with the following code;

    Sub EnableSavebtn(ByVal sender As Object, ByVal e As EventArgs)
        btnSaveOvr.Enabled = True
        Dim i As Integer
        For i = 0 To (dgAddressCorrection.Items.Count - 1)
            Dim myCheckBox2 As CheckBox = CType(dgAddressCorrection.Items(i).Cells(5).Controls(1), CheckBox)
            myCheckBox2.AutoPostBack = False
        Next
    End Sub

This works, but I have to turn the AutoPostBack to false for every row in the datagrid.  Is there a way to change the AutoPostBack for the whole column instead of doint it for every row?  I only need to have AutoPostBack set to true for the first time the user click in one of the CheckBox in the datagrid, after that first click, I do not need AutoPostBack set to true.
0
Comment
Question by:jeandoiron
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
tomv011397 earned 150 total points
ID: 12018881
you will need to write some Javascript to do this, you COULD do it in code, but it is very ugly to do. (on the if ispostback, set the control to postback = false)

the javascript can be set to do a submit on the first check only etc.

Tom
0
 
LVL 4

Assisted Solution

by:Javert93
Javert93 earned 150 total points
ID: 12022217
This code should do what you are asking:

    Public Sub dgAddressCorrection_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgAddressCorrection.ItemDataBound
        AddHandler CType(e.Item.Cells(5).Controls(1), CheckBox).CheckedChanged, OnItemCheckedChanged
    End Sub

    Public Sub OnItemCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim cb As CheckBox = CType(sender, CheckBox)
        If Not cb.Checked Then cb.AutoPostBack = False
    End Sub

I wans't able to test it, but it should work.
0
 
LVL 4

Expert Comment

by:Javert93
ID: 12022229
However, that disables the AutoPostBack for the each check box when the user clicks on it.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now