CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Eric Wise

Business & .NET

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.


Published Jan 12 2005, 08:55 AM by Eric Wise
Filed under: ,

Comments

Shankara Raman S said:

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...
# May 25, 2005 9:16 AM

Gilberto Leon said:

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
# July 20, 2005 10:40 AM

Eric Wise said:

I've recently written a similar article here:

http://codebetter.com/blogs/eric.wise/archive/2005/07/18/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.
# July 21, 2005 12:22 PM

Bob Baran said:

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
# September 29, 2005 1:34 PM

Alejandro Lococo said:

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
# October 2, 2005 10:54 AM

Eric Wise said:

Only thing I can tell you is make sure your addressof statement is correct for the footer.
# October 2, 2005 2:54 PM

jocelyn said:

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

# October 1, 2006 2:40 PM

NIL said:

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

THANKS

# January 2, 2007 6:16 AM

Null said:

Nice

# May 16, 2007 12:45 AM

Prabhmeet Kohli said:

Thank you SOOOOO much for posting this. I had been trying this in many different ways and lost almost a whole day' s worth of work.

Thanks again!!

# January 8, 2008 5:05 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!