Solved

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

Posted on 2009-04-15
6
1,041 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
[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
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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