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

x
?
Solved

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

Posted on 2009-04-15
6
Medium Priority
?
1,058 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

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