Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Autopopulating textboxes in Gridview using linqdatasource

Posted on 2008-10-09
1
Medium Priority
?
1,244 Views
Last Modified: 2013-11-11
I have several gridviews working very well with a LinqDataSource.  I am able to Insert, Update and Delete records.

In the footertemplate of one of my grids, I have created a small form to insert a new record into the database, and that is working great.  The form consists of address related textboxes such as address, city, state and zip and none of the controls are bound to any data source.

What I would like to do is provide a way for the user to simply enter a zip code in the zip code text box and have the city and state textboxes populated from a table in the DB.  When the txtZipCode control looses focus, I would like to populate the city and state text boxes with a partial post back.  I have a script manager in my master page, and the webpart I am using is wrapped in an update panel.

I have several Linq data sources.  The GridView uses ldsPersonAddresses and there is a relationship to the postal_code table which contains the zip, city, and state.  I think I could use that datasource, but I don't know how to cause the postback and use txtZipCode.Text.  I also have a stored procedure available which would return the data...

How do I bind the txtCity and txtState controls with a partial postback?
<asp:LinqDataSource ID="ldsPersonAddresses" runat="server" ContextTypeName="VBCODE.jhadbDataClassesDataContext"
            TableName="person_addresses" Where="PersonID == @PersonID" EnableDelete="True"
            EnableInsert="True" EnableUpdate="True">
            <WhereParameters>
                <asp:QueryStringParameter Name="PersonID" QueryStringField="PersonID" Type="Int32" />
            </WhereParameters>
        </asp:LinqDataSource>
        <asp:LinqDataSource ID="ldsAddressTypes" ContextTypeName="VBCODE.jhadbDataClassesDataContext"
            TableName="address_types" runat="server">
        </asp:LinqDataSource>
        <asp:LinqDataSource ID="ldsStates" runat="server" ContextTypeName="VBCODE.jhadbDataClassesDataContext"
            TableName="states">
        </asp:LinqDataSource>
        <asp:LinqDataSource ID="ldsCountries" ContextTypeName="VBCODE.jhadbDataClassesDataContext"
            TableName="countries" runat="server">
        </asp:LinqDataSource>
        <asp:LinqDataSource ID="ldsZipCodeLookup" runat="server" 
            ContextTypeName="VBCODE.jhadbDataClassesDataContext" Select="new (City, State)" 
            TableName="postal_codes" Where="Zip == @Zip">
            <WhereParameters>
                <asp:FormParameter FormField="txtNewZip" Name="Zip" Type="String" />
            </WhereParameters>
        </asp:LinqDataSource>

Open in new window

0
Comment
Question by:abenage
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
novynov earned 2000 total points
ID: 22690048
I may be misunderstanding your problem, and there are lots of ways to tackle this, here's an approach that comes to mind:

- Put the footer row content in it's own updatepanel. I believe this works fine with the gridview and will minimize the postback payload for the zip lookup.
- Put a LinqDataSource in the footer row updatepanel that is bound to the postal_code table. For its whereparameter, bind it to the zip text field.
- Place the city and state text boxes (not the zip) in a FormView. This will allow both controls to be easily databound to the LinqDataSource. Set the DataSourceID to point at the LDS.
- Make the zip text field AutoPostback = true. This will cause the postback to happen on focus loss.

Because the value has changed when the text box postback happens, the LDS will rebind and grab the right data.

I did a small demo of this (see snippet), not in a grid view, and it worked fairly well. The only issue that I saw was that the two fields of the formview didn't show up if there was no data. I imagine there is an easy way around this - though I haven't investigated. If it's a pain, I'd ditch the FormView and just place the TextBox controls in my footer, and manage the databinding by hand in my OnTextChanged handler. In fact, I might not even use a LDS at that point, I'd just fire of a query to the db, grab the results, and stuff the text boxes.

If you do your insert logic in this same updatepanel, you'll need to call UpdatePanel.Update() on the UpdatePanel that owns the whole gridview. Otherwise, your new row will be in the db, but won't show up in the gridview until you refresh the whole page.

BTW, it may be none of my business, but aren't there cases where one zip code can map to multiple cities. It seems like I've run into this before. Does your db schema, and your approach to this problem account for that?

I hope this half-baked answer helps. Let me know.

  <asp:UpdatePanel runat="server" ID="upZip" UpdateMode="Conditional">
    <ContentTemplate>
     <asp:LinqDataSource runat="server" ID="lds" 
        ContextTypeName="WebApplication2.TestDataContext" TableName="ZipCodes" 
        Where="Zip == @Zip">
        <WhereParameters>
            <asp:ControlParameter ControlID="textZip" Name="Zip" PropertyName="Text" 
                Type="String" />
        </WhereParameters>
    </asp:LinqDataSource>
    
    <asp:FormView runat="server" DataSourceID="lds" ID="fv" DataKeyNames="Zip" DefaultMode="ReadOnly">
        <ItemTemplate>
            City:
            <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' />
            <br />
            State:
            <asp:TextBox ID="StateTextBox" runat="server" Text='<%# Bind("State") %>' />
            <br />
          
        </ItemTemplate>
    </asp:FormView>
    <asp:TextBox runat="server" ID="textZip" AutoPostBack="true" Text="" />
    </ContentTemplate>
    </asp:UpdatePanel>

Open in new window

0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

688 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