Solved

How to stuff a value into a DetailsView?

Posted on 2009-04-09
6
362 Views
Last Modified: 2012-05-06
Hello experts.  I have a DetailsView on an ASP.NET page.  There is also a textbox that generates a value.  I'd like to take this generated value and stuff it into the proper cell of the DetailsView (it happens to be cell[2]), so the user can see it before they hit Insert or Update.  I've messed with this way too much without much progress.  Thanks!
0
Comment
Question by:jvalescu
  • 3
  • 3
6 Comments
 

Author Comment

by:jvalescu
ID: 24109655
Correction.  After looking at the DetailsView more closely, the correct reference would be DetailsView1.Rows[4]  
0
 
LVL 8

Expert Comment

by:ppittle
ID: 24110236
jvalescu,

Could you elaborate on your situation; preferably with the markup you are using.
0
 

Author Comment

by:jvalescu
ID: 24110799
Sure.  I've attached the ASP code for the DetailsView.  The field in the DetailsView that I am working with is:
<asp:BoundField DataField="CoilNumber" HeaderText="CoilNumber"
                SortExpression="CoilNumber" />

I have a textbox control, txtSplitCoil on the form.  If anything is entered in this textbox, I'd like the contents to display in the  CoilNumber field of the DetailsView, before the user hits Insert.  I know I can change the value in the Inserting event using the eventargs, but I want this to display before they Insert.  Did I just make this more confusing?
Thanks!

 <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 

        BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" 

        Caption="COIL FINISHED WITH CLEANING" CellPadding="3" DataKeyNames="ID" 

        DataSourceID="DetailViewSqlDataSource1" DefaultMode="Insert" ForeColor="Black" 

        GridLines="Vertical" 

        

        

        style="z-index: 1; left: 385px; top: 300px; position: absolute; height: 476px; width: 412px" 

        CaptionAlign="Top" oniteminserted="DetailsView1_ItemInserted">

        <FooterStyle BackColor="#CCCCCC" />

        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

        <Fields>

            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 

                ReadOnly="True" SortExpression="ID" />

            <asp:BoundField DataField="WorkCenterID" HeaderText="WorkCenterID" 

                SortExpression="WorkCenterID" Visible="False" />

            <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" 

                SortExpression="EmployeeID" />

            <asp:BoundField DataField="Control" HeaderText="Control" 

                SortExpression="Control" />

            <asp:BoundField DataField="CoilNumber" HeaderText="CoilNumber" 

                SortExpression="CoilNumber" />

            <asp:BoundField DataField="TranDt" HeaderText="TranDt" 

                SortExpression="TranDt" Visible="False" />

            <asp:BoundField DataField="Weight" HeaderText="Weight" 

                SortExpression="Weight" />

            <asp:TemplateField HeaderText="Coil on hold" 

                SortExpression="Status">

                <EditItemTemplate>

                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>

                </EditItemTemplate>

                <InsertItemTemplate>

                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Status") %>' />

                </InsertItemTemplate>

                <ItemTemplate>

                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Status") %>'></asp:Label>

                </ItemTemplate>

            </asp:TemplateField>

            <asp:TemplateField HeaderText="FinishedGood" SortExpression="FinishedGood">

                <EditItemTemplate>

                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("FinishedGood") %>'></asp:TextBox>

                </EditItemTemplate>

                <InsertItemTemplate>

                    <asp:CheckBox ID="CheckBox2" runat="server" 

                        Checked='<%# Bind("FinishedGood") %>' />

                </InsertItemTemplate>

                <ItemTemplate>

                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("FinishedGood") %>'></asp:Label>

                </ItemTemplate>

            </asp:TemplateField>

            <asp:TemplateField HeaderText="Notes" SortExpression="Notes">

                <EditItemTemplate>

                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Notes") %>'></asp:TextBox>

                </EditItemTemplate>

                <InsertItemTemplate>

                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Notes") %>' 

                        TextMode="MultiLine"></asp:TextBox>

                </InsertItemTemplate>

                <ItemTemplate>

                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Notes") %>'></asp:Label>

                </ItemTemplate>

            </asp:TemplateField>

            <asp:CommandField InsertText="Process Complete" ShowEditButton="True" 

                ShowInsertButton="True" >

            <HeaderStyle Font-Bold="True" />

            <ItemStyle Font-Bold="True" />

            </asp:CommandField>

            <asp:BoundField DataField="TranType" HeaderText="TranType" 

                SortExpression="TranType" Visible="False" />

        </Fields>

        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />

        <EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

        <AlternatingRowStyle BackColor="#CCCCCC" />

    </asp:DetailsView>

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

by:ppittle
ID: 24111065
jvalescu,

Take a look at the Code Snippet below.  First, you'll need to change the BoundField to a TemplateField, so can specify the ID of the TextBox to be used.  Then, add an event handler for the DetailsView's DataBound Event.  After the DetailView data binds, and builds all of it's controls, we can get a reference to the TextBox in the CoilNumber field.  The reference to the TextBox gets the ClientID (the ID of the HTML object on the client page).  The ClientID can be used to create JavaScript that updates the txtCoilNumber TextBox with the contents of the txtSplitCoil TextBox.   In the example below I use the onblur javascript event, which fires when the control loses focus.  

Let me know if you have any questions.

PJ
<form id="form1" runat="server" >   

        <asp:TextBox ID="txtSplitCoil" runat="server" />

        <br />

        <br />

        

        <asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Insert" 

            OnDataBound="DetailsView1_DataBound" DataSourceID="ObjectDataSource1" AutoGenerateRows="false" >

            <Fields>

                <asp:TemplateField HeaderText="CoilNumber" 

                SortExpression="CoilNumber" >

                    <InsertItemTemplate>

                        <asp:TextBox ID="txtCoilNumber" runat="server" Text='<%# Bind("CoilNumber") %>' />

                    </InsertItemTemplate>

                </asp:TemplateField>
 

            </Fields>

        </asp:DetailsView>        

    </form>
 
 

<%-- Code-Behind --%>

protected void DetailsView1_DataBound(object sender, EventArgs e)

        {

            #region Get a reference to the txtCoilNumber TextBox

            int columnIndexOfCoilNumberField = 0;

            TextBox txtCoilNumber = (DetailsView1.Rows[0].Cells[columnIndexOfCoilNumberField].FindControl("txtCoilNumber") as TextBox);

            if (null == txtCoilNumber)

            {

                throw new Exception("Could find the TextBox txtCoilNumber in the DetailsView");

            }

            #endregion
 

            #region Add JavaScript to the txtSplitCoil text box to update txtCoilNumber
 

            txtSplitCoil.Attributes.Add("onblur", "document.getElementById('" + txtCoilNumber.ClientID + "').value = this.value;");
 

            #endregion

        }

Open in new window

0
 

Author Comment

by:jvalescu
ID: 24115057
Wow, thanks for such a concise response.  I put your code in exaxctly as you described.  But the new value is not displaying in the DetailsView.  I noticed that the Databound event fires before the page even renders; so the value from txtSplitCoil would never be filled in.  I even tried inserting the code at the end of the Text_Changed event of txtSplitCoil (code is listed below).  I watched it run through properly with the debugger, but the same result.  Am I doing something wrong?
        protected void txtSplitCoilNumber_TextChanged(object sender, EventArgs e)

        {
 

            if (this.txtSplitCoilNumber.Text == string.Empty)

                return;
 

            SqlConnection cnn = new SqlConnection(strConn);
 

            string strSQL = string.Empty;
 

        

            SqlCommand cmdSQL = new SqlCommand();

            cmdSQL.Connection = cnn;

            cmdSQL.CommandType = CommandType.StoredProcedure;

            cmdSQL.CommandText = "sp_SplitCoil";

            cmdSQL.Parameters.Add("@CoilNumber", SqlDbType.NVarChar, 25);
 

            cmdSQL.Parameters["@CoilNumber"].Value = this.txtSplitCoilNumber.Text;
 
 

     

            cmdSQL.Parameters.Add("@return_value", SqlDbType.NVarChar, 25);

            

            cmdSQL.Parameters["@return_value"].Direction = System.Data.ParameterDirection.Output;
 

              string strResult = string.Empty;

            try

            {
 

                cnn.Open();

                cmdSQL.ExecuteNonQuery();

                strResult = cmdSQL.Parameters["@return_value"].Value.ToString();
 
 
 
 

            }

            catch (SqlException ex)

            {

                MessageBox(ex.ToString());

            }

            finally

            {

                if (cnn != null)

                    cnn.Close();

            }
 

            if (strResult != string.Empty && strResult != null)

            {
 

                this.txtSplitCoilNumber.Text = strResult;
 

                #region Get a reference to the txtCoilNumber TextBox

                int columnIndexOfCoilNumberField = 0;

                TextBox txtCoilNumber = (DetailsView1.Rows[0].Cells[columnIndexOfCoilNumberField].FindControl("txtCoilNumber") as TextBox);

                if (null == txtCoilNumber)

                {

                    throw new Exception("Could not find the TextBox txtCoilNumber in the DetailsView");

                }

                #endregion
 

                #region Add JavaScript to the txtSplitCoil text box to update txtCoilNumber
 

                this.txtSplitCoilNumber.Attributes.Add("onblur", "document.getElementById('" + txtCoilNumber.ClientID + "').value = this.value;");
 

                #endregion

            }
 
 

        }

Open in new window

0
 
LVL 8

Accepted Solution

by:
ppittle earned 500 total points
ID: 24116220
jvalescu,

The solution I originally proposed uses client side javascript to populate the txtCoilNumber TextBox which is in the DetailsView based on what's present on the txtSplitCoilNumber TextBox.  The value is transfered when the user first clicks on txtSplitCoilNumber and the clicks elsewhere on the page.  ASP.NET server side code does not execute at this time.  However, the client side code is generated with ASP.NET on the following line:

#region Add JavaScript to the txtSplitCoil text box to update txtCoilNumber
 
                this.txtSplitCoilNumber.Attributes.Add("onblur", "document.getElementById('" + txtCoilNumber.ClientID + "').value = this.value;");
#endregion

The above code needs to be executed in the DetailsView1.DataBound event.  The reasoning is, the code to add the javascript can only be added after DetailsView1 is fully built, and it must be sent to the client on their initial request.

For you to adapt my code for your situation you'll need to change line 25 of my original post:
int columnIndexOfCoilNumberField = 0;

and set columnIndexOfCoilNumberField to the appropriate column index.  Because I built the example using a simplified DetailsView that only had one Field, the column index of the CoilNumber field is zero.  In your code, you have 12 Fields, and CoilNumber is the 5th field.  Because the Column Collection is a 0 based Index (ie the 1st element has an index of 0, the 2nd element has an index of 1, the 3rd element has an index of 2, etc), modify the line as so:

int columnIndexOfCoilNumberField = 4;

That should take care of it.  If  you're still having issues, can you send me the markup for the DetailsView, what you have in the DetailsView1_DataBound event handler (code-behind) as well as the rendered markup you get in the browser (In Internet Explorer right click on the page and select View Source)

PJ
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

11 Experts available now in Live!

Get 1:1 Help Now