Eric Wise

Sponsors

The Lounge

Wicked Cool Jobs

Blogs I Read

Fun & Games

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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.


Posted Wed, Jan 12 2005 8:55 AM by Eric Wise
Filed under: ,

[Advertisement]

Comments

CH.ADEEL wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Thu, Mar 10 2005 2:58 AM
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
Shankara Raman S wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Wed, May 25 2005 9:16 AM
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...
Gilberto Leon wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Wed, Jul 20 2005 10:40 AM
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
Eric Wise wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Thu, Jul 21 2005 12:22 PM
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.
Bob Baran wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Thu, Sep 29 2005 1:34 PM
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
Alejandro Lococo wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Sun, Oct 2 2005 10:54 AM
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
Eric Wise wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Sun, Oct 2 2005 2:54 PM
Only thing I can tell you is make sure your addressof statement is correct for the footer.
jocelyn wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Sun, Oct 1 2006 2:40 PM

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

NIL wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Tue, Jan 2 2007 6:16 AM

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

THANKS

Null wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Wed, May 16 2007 12:45 AM

Nice

Prabhmeet Kohli wrote re: HOWTO: Configure a datagrid template postback from a textbox
on Tue, Jan 8 2008 5:05 PM

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!!

Devlicio.us