Solved

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

Posted on 2009-04-15
6
1,046 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

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

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…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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