Solved

How can I pre-fill fields within the asp.net 3.5 listview control?

Posted on 2009-04-15
6
1,023 Views
Last Modified: 2013-12-17
I want to have some of my ListView InsertItemTemplate textbox fields pre-filled from code-behind with some data taken from other controls or from my linqdatasource to make the user aware of some key info when he/she fills in the other input fields which are not pre-filled. I have written the attached code which allows me to access these controls in my listviews insertitemtemplate but I can't seem to pre-fill the controls with data from my database? I have written a query against my linq data context (commented out below) that returns the correct data for the control but I don't have a way of getting that data into the control within my insertitemtemplate? I simply want to mimic the Eval or Bind techniques to fill these controls in code behind.
protected void lvAccounts_ItemCreated(object sender, ListViewItemEventArgs e)

    {

        //TrustSystemDataContext db = new TrustSystemDataContext();
 

        //var query = from ta in db.TrustAccounts

                    //join tt in db.Trusts on ta.TrustID equals tt.TrustID

                    //select new { tt.ClientName };
 

        if (e.Item.ItemType == ListViewItemType.InsertItem)

        {

            TextBox box = e.Item.FindControl("ClientNameInsertTextBox") as TextBox;

            Label lbl = e.Item.FindControl("ClientNameLabelItem") as Label;
 

            box.Text = lbl.Text;

        }

    }

Open in new window

0
Comment
Question by:Shepwedd
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24147660
Hello Shepwedd,

I just tried a simple example that seemed to work. (though it follows what you were trying to do above) Maybe this sample helps? I find the textbox and set the text to "good bye". If im missing what you are trying to accomplish let me know.

Default.aspx

        <asp:ListView ID="LV1" runat="server" InsertItemPosition="LastItem" onitemcreated="LV1_ItemCreated">
            <LayoutTemplate>
                <div id="itemPlaceholder" runat="server"></div>
            </LayoutTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text="Hello"></asp:Label>
            </ItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </InsertItemTemplate>
        </asp:ListView>

Code Behind:

        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> l = new List<string>(new string[] { "a", "b", "c" });
            this.LV1.DataSource = l;
            this.LV1.DataBind();
        }

        protected void LV1_ItemCreated(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.InsertItem)
            {
                TextBox txt = (TextBox)e.Item.FindControl("TextBox1");
                txt.Text = "Good bye";
            }
        }
0
 

Author Comment

by:Shepwedd
ID: 24147950
wht1986,

Like yourself I was able to set the txt.Text property but what I'm trying to do is pre-fill controls in my insertitemtemplate as if they were databound (to make them dynamic) like how you would normally use the Eval or Bind techniques. I want to replace your "Good Bye" with a database value or the value that is pulled into the itemTemplate.
0
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 total points
ID: 24148610
Guess I'm a little confused as to the difference of what you are trying to achieve. There is only (1) instance if the InsertItemTemaplte thus assigning the values of the controls in the item create event is essentially dynamically prefilling them.  If you had a textboxe in the insert item, you could fetch the DB contexts object and assign in the same way like:

protected void lvAccounts_ItemCreated(object sender, ListViewItemEventArgs e)
    {
var query = from ta in db.TrustAccounts
                    join tt in db.Trusts on ta.TrustID equals tt.TrustID
                    select new { tt.ClientName };

TextBox1.Text = query.First().ClientName;
}

Otherwise I am not sure what you mean by prefill, unless you are trying to avoid any code behind and just have the declarations inline on the aspx markup.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Shepwedd
ID: 24155758
Hi,

I thought your suggested code worked but then I realised that the data it pre-fills my insertitemtemplate controls with is always the same, i.e. it's not dynamic. What I have is two listviews on seperate pages. When a user clicks a link from the first listview I am passing a variable through the query string to populate the second listview. Now it is here in my second listview that I want to pre-fill the controls in my insertitemtemplate with some of the data from my first listview. Your code allows me to pre-fill the controls but always with the same data, it's not dynamic data depending on the ID getting passed through within the query string? i.e. Say the name "Joe Bloggs" has an ID = 1 and the name "Steven Spielberg" has an ID = 2, when the ID = 2 is getting passed through the query string "Joe Bloggs" is still the name pre-filling my control?
0
 

Author Comment

by:Shepwedd
ID: 24156193
For those out there interested in my solution I solved this by setting the below filter on my linq query:

var query = from ta in db.TrustAccounts
                    join tt in db.Trusts on ta.TrustID equals tt.TrustID
                    where tt.TrustID == Convert.ToInt32(Request.QueryString["TrustID"])
                    select new { tt.ClientName };
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24156710
That is true, you never said what you were varying the selection based on some other parameter.  I took your "i want a dynamic fill" to be just filling from a database rather than TextBox.Text="something" hard coded. Glad you figured it out what you needed.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

706 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

21 Experts available now in Live!

Get 1:1 Help Now