Blog Stats
  • Posts - 298
  • Articles - 0
  • Comments - 1512
  • Trackbacks - 0

 

HOWTO: Configure a datagrid template postback from a textbox

I wrote this bit of code today and I thought it was neat so I figured I'd post it here.

Lets say you have a datagrid filled with addresses which are editable in-grid.  In addition you have a footer with an add button that displays the same style of edit boxes as the line items.  This is all well and good and fairly well documented in the community so let's add a twist: When a user types in a zip code, grab a list of cities in that zip code and display them as a dropdownlist and populate the city and county for that zipcode.

Here's a screenshot of the grid as it comes out of the gate:

Here's what it looks like in add mode.  Edit mode would be the same except the data items would be filled in.

Now, let's talk code.

  1. We need to set the autopostback property on the zip code textboxes (add and edit) to True.
  2. We need to wire up the TextChanged event for the zipcode controls
  3. We need to be able to parse the datagrid editindex row or the footer row depending on which onchanged event was fired.

Wiring up the onchanged event is fairly simple, we do it in the ItemCreated() event of the datagrid.

   Private Sub Grid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles Grid1.ItemCreated
        If e.Item.ItemType = ListItemType.EditItem Then
            AddHandler CType(e.Item.FindControl("ZipCode"), TextBox).TextChanged, AddressOf ZipCode_TextChanged
        End If

        If e.Item.ItemType = ListItemType.Footer Then
            AddHandler CType(e.Item.FindControl("AddZipCode"), TextBox).TextChanged, AddressOf AddZipCode_TextChanged
        End If
   End Sub

Notice I have two sub routines to handle the TextChanged for the zipcode boxes.  Here's the code for them.  You will see a difference between how to manipulate controls in the footer versus how to manipulate controls in the current edititemindex.  The FooterIndex is always the last record in the controls(0) collection found as such:

Dim FooterIndex As Integer = Grid1.Controls(0).Controls.Count - 1

Note that I have stripped out the code populating myZipCodes, just accept that myZipCodes is a collection of ZipCode objects that have the properties necessary to populate the grid.  =)

   Private Sub ZipCode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myZipCodes As New UltraCareBLL.ZipCodes

        If myZipCodes.Count >= 1 Then
            CType(Grid1.Items(Grid1.EditItemIndex).FindControl("City"), TextBox).Visible = False
            Dim myDDL As DropDownList = CType(Grid1.Items(Grid1.EditItemIndex).FindControl("ddlCity"), DropDownList)
            myDDL.DataSource = myZipCodes
            myDDL.DataTextField = "City"
            myDDL.DataBind()
            myDDL.Visible = True
            CType(Grid1.Items(Grid1.EditItemIndex).FindControl("State"), TextBox).Text = myZipCodes(0).State
            CType(Grid1.Items(Grid1.EditItemIndex).FindControl("County"), TextBox).Text = myZipCodes(0).District
        Else
            CType(Grid1.Items(Grid1.EditItemIndex).FindControl("ddlCity"), DropDownList).Visible = False
            CType(Grid1.Items(Grid1.EditItemIndex).FindControl("City"), TextBox).Visible = True
        End If
   End Sub

   Private Sub AddZipCode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If myZipCodes.Count >= 1 Then
            CType(Grid1.Controls(0).Controls(FooterIndex).FindControl("AddCity"), TextBox).Visible = False
            Dim myDDL As DropDownList = CType(Grid1.Controls(0).Controls(FooterIndex).FindControl("ddlAddCity"), DropDownList)
            myDDL.DataSource = myZipCodes
            myDDL.DataTextField = "City"
            myDDL.DataBind()
            myDDL.Visible = True
            CType(Grid1.Controls(0).Controls(FooterIndex).FindControl("AddState"), TextBox).Text = myZipCodes(0).State
            CType(Grid1.Controls(0).Controls(FooterIndex).FindControl("AddCounty"), TextBox).Text = myZipCodes(0).District
        Else
            CType(Grid1.Controls(0).Controls(FooterIndex).FindControl("ddlAddCity"), DropDownList).Visible = False
            CType(Grid1.Controls(0).Controls(FooterIndex).FindControl("AddCity"), TextBox).Visible = True
        End If
   End Sub

So the final result is that if the zipcode collection has 1 or more records, it will hide the edit textbox and present the user with a dropdownlist of choices for city and auto populate the county and state.  If the zipcode is not found in the collection (empty collection) it leaves edit textbox there and hides the dropdown list so the user can type in a city manually.


Feedback

# re: HOWTO: Configure a datagrid template postback from a textbox

Gravatar

WELL I HAVE READ UR CODE AND IT IS IN VB....WELL I WANT SUCH KIND OF CODE IS C#........ALSO WANT TO KNOW THAT WHEN WE CLICK ON EDIT BUTTON.......IT CONVERTS THE FIELD IN TO TEXTBOX BYDEFAULT........SO I WANT TO KNOW THAT HOW CAN I CONVERT IT TO OTHER CONTROLS......IF U HAVE A LINKS OR SOME THING ELSE TO HELP ME TO UNDERSTAND IT.......PLZ JUST TEll Me my email address is ch.adeel@gmail.com

3/10/2005 7:58 AM |

# re: HOWTO: Configure a datagrid template postback from a textbox

Gravatar

Hi,

 I just need to populate a DropDownList in a datagrid when a postback occurs due to another DropDownList in the DataGrid..Any idea how to do?..

it'd be of great help..

my mail id is shansulak_2001@yahoo.co.in

regs,

Shanu...

5/25/2005 1:16 PM |

# re: HOWTO: Configure a datagrid template postback from a textbox

Gravatar

Awesome piece of code, very concise and right to the point. As Eric said, there are a lot of explaining on how to deal with datagrid, but this is the only help I had found on dealing internally with a row once the data is posted.

Superb...

Gilbert

gilberto.leon@glcconsultants.com

7/20/2005 2:40 PM |

# re: HOWTO: Configure a datagrid template postback from a textbox

Gravatar

I've recently written a similar article here:

codebetter.com/.../129344.aspx

It deals with comboboxes to comboboxes which takes these concepts a step further since you have to handle setting the selectedindex.

It also has a downloadable code sample containing the user control I used.  You just have to "fake" the data coming in.

7/21/2005 4:22 PM |

# re: HOWTO: Configure a datagrid template postback from a textbox

Gravatar

I have spent the last 3 days trying to figure this out. I wanted to auto-fill two adjacent textboxes once the initial textbox was filled in.

Your code worked perfectly!!!

Thanks a lot,

Bob

9/29/2005 5:34 PM |

# re: HOWTO: Configure a datagrid template postback from a textbox

Gravatar

Hi Eric,

I have tried to implement it, but cannot get textchanged event firing from a footer textbox. Although postback and addhandler codes are executed, it seems the delegate not working. Any idea?

Regards,

Alejandro  

10/2/2005 2:54 PM |

# re: HOWTO: Configure a datagrid template postback from a textbox

Gravatar

Only thing I can tell you is make sure your addressof statement is correct for the footer.

10/2/2005 6:54 PM |

# re: HOWTO: Configure a datagrid template postback from a textbox

Gravatar

This sample in using viewstate works great ! www.codeproject.com/.../AspNetEditGrid2003.asp

10/1/2006 6:40 PM |

# re: HOWTO: Configure a datagrid template postback from a textbox

Gravatar

THIS IS SUCH A NICE AND USEFUL CODE....

THANKS

1/2/2007 11:16 AM |

# re: HOWTO: Configure a datagrid template postback from a textbox

Gravatar

Nice

5/16/2007 4:45 AM |

Post a comment





 

Please add 7 and 3 and type the answer here:

 

 

Copyright © Eric Wise