ASP.net datagrid save values to text field

logicsolutions
logicsolutions used Ask the Experts™
on
Hi Experts,

The following code does:

1. Create and ODBC Connection to the DSN
2. Lists the values on the page in a table
3. Adds a checkbox to the grid next to each row.

My question is this.

The DSN is read only but I still want the ability to save the checkbox selection I have made. At the moment the code has the following issues.

1. The checkboxes I am sure are not bound to each row. They appear on each line but I don't think they do anything. What I am after is when I select the check box it will automatically save the whole row which has the checkbox selected and output it in a friendly way.

Example. If I select a row which has two columns. Column A = 1 & Column B = 2 then by checking the checkbox it will give an output of 1 - 2

Is it possible to save the values to a hidden text field within the page rather then the DSH database as the there are no write permissions on the database. Hopefully by saving it to a text field of some sought it can read the selection and save it.

2. A save button which will allow me to save the results.

Looking forward to hearing your thoughts.
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<%@ Register TagPrefix="chkbox" Namespace="DataGridControls" Assembly="DataGridCheckbox" %>
 
 
<script runat="server">
 
    void Page_Load(object sender, EventArgs e) {
    
        // TODO: Update the ConnectionString and CommandText values for your application
        string ConnectionString = @"DSN=server;uid=username;pwd=password";
        string CommandText = "select Items.ItemNumber, Items.ItemName FROM Items Items ORDER BY Items.ItemNumber ASC";
    
        OdbcConnection myConnection = new OdbcConnection(ConnectionString);
        OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
    
        myConnection.Open();
    
        DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        DataGrid1.DataBind();
    
        // close the connection
        myConnection.Close();
    }
 
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <form id="Form1" runat="server">
        <asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" 
 
ForeColor="Black" EnableViewState="False">
<Columns>
			<chkbox:CheckBoxColumn/>
			</Columns>
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
As you are uisng DataGridCheckBox. Instead of that use TemplateColumn . Put checkbox control inside that.

Now handle checkbox event.

Author

Commented:
HI Jinal,

I'm a newbie at ASP.net

Is it possible to show me how this is done using the code I have pasted.

Appreciate all the help.

:)

Commented:
In following sample i used my database.

Please configure your connnection string , grid column so you can test this.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DataGrid ID="grid1" runat="server" AutoGenerateColumns="false"             >
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="chkID" runat="server" 
                        oncheckedchanged="chkID_CheckedChanged" AutoPostBack="true" />
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:BoundColumn DataField="lockid"></asp:BoundColumn>
            <asp:BoundColumn DataField="personid"></asp:BoundColumn>
        </Columns>
    </asp:DataGrid>
    </div>
    </form>
</body>
</html>
/* Codebehind */
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
 
public partial class Default6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string conn_string = @"Data Source=.\SQLEXPRESS08;Initial Catalog=AdventureWorks;Integrated Security=True";
            SqlConnection con = new SqlConnection();
            con.ConnectionString = conn_string;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select * from testtable";
            cmd.Connection = con;
            con.Open();
            grid1.DataSource = cmd.ExecuteReader(System.Data.CommandBehavior.Default);
            grid1.DataBind();
        }
    }
   
    protected void chkID_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk != null)
        {
            DataGridItem item = chk.NamingContainer as DataGridItem;
            Response.Write(item.Cells[1].Text + " -- " + item.Cells[2].Text);
        }
 
    }
}

Open in new window

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Author

Commented:
Excuse my lack of knowledge jinal. How do I get what I wrote orginally working with your code?

Commented:

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<%@ Register TagPrefix="chkbox" Namespace="DataGridControls" Assembly="DataGridCheckbox" %>
 
 
<script runat="server">
 
    void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack)
        {
        // TODO: Update the ConnectionString and CommandText values for your application
        string ConnectionString = @"DSN=server;uid=username;pwd=password";
        string CommandText = "select Items.ItemNumber, Items.ItemName FROM Items Items ORDER BY Items.ItemNumber ASC";
    
        OdbcConnection myConnection = new OdbcConnection(ConnectionString);
        OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
    
        myConnection.Open();
    
        DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        DataGrid1.DataBind();
    
        // close the connection
        myConnection.Close();
        }
    }
 
    public void chkID_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk != null)
        {
            DataGridItem item = chk.NamingContainer as DataGridItem;
            Response.Write(item.Cells[1].Text + " -- " + item.Cells[2].Text);
        }
 
    }
 
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <form id="Form1" runat="server">
        <asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" 
 
ForeColor="Black" EnableViewState="False">
<Columns>
                        <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="chkID" runat="server" 
                        oncheckedchanged="chkID_CheckedChanged" AutoPostBack="true" />
                </ItemTemplate>
            </asp:TemplateColumn>
 
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

Open in new window

Author

Commented:
I received an error.

In the event viewer I got something like this

Exception information:
    Exception type: HttpParseException
    Exception message: System.Web.UI.WebControls.DataGridColumnCollection must have items of type 'System.Web.UI.WebControls.DataGridColumn'. 'HeaderStyle' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.

Commented:

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<%@ Register TagPrefix="chkbox" Namespace="DataGridControls" Assembly="DataGridCheckbox" %>
 
 
<script runat="server">
 
    void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack)
        {
        // TODO: Update the ConnectionString and CommandText values for your application
        string ConnectionString = @"DSN=server;uid=username;pwd=password";
        string CommandText = "select Items.ItemNumber, Items.ItemName FROM Items Items ORDER BY Items.ItemNumber ASC";
    
        OdbcConnection myConnection = new OdbcConnection(ConnectionString);
        OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
    
        myConnection.Open();
    
        DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        DataGrid1.DataBind();
    
        // close the connection
        myConnection.Close();
        }
    }
 
    public void chkID_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk != null)
        {
            DataGridItem item = chk.NamingContainer as DataGridItem;
            Response.Write(item.Cells[1].Text + " -- " + item.Cells[2].Text);
        }
 
    }
 
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <form id="Form1" runat="server">
        <asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" 
 
ForeColor="Black" EnableViewState="False">
<Columns>
                        <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="chkID" runat="server" 
                        oncheckedchanged="chkID_CheckedChanged" AutoPostBack="true" />
                </ItemTemplate>
            </asp:TemplateColumn>
 </Columns>
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

Open in new window

Author

Commented:
Getting closer.

No errors anymore but when I select a text box I see the status bar go blue and the whole page disappears like it just saved something then went blank.

How do I get it to save the checked values into another text box somewhere on the page. That way when I save the values they can be read again.

Commented:
make EnableViewState = true for gridview and test.

Author

Commented:
It's doing the same thing even with EnableViewState=True

I have created an ntext field on a parent frame to save the values. How does it save the values to the field?

Author

Commented:
Just wanted to thank you for all your patience Jinal.

Commented:
your problem get resolved or not ?

Author

Commented:
Found the following code whilst searching the web

Is this useful to assist in solving the problem.
To add text from the TextBox via a button call:
 
====
 
private void btnInsrt_Click(object sender, EventArgs e)
    {
        datagridview1.CurrentCell.Value = textBox1.Text;
    }
 
To add the cell value to a TextBox use either of the DataGridView events handelers
 
void datagridview1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        string CurrentValueHere;
        CurrentVauleHere = datagridview1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        textBox1.Text = this.CurrentValueHere;
    }

Open in new window

Author

Commented:
Problem is still there Jinal.

I'm up to the bit where I would like to save my checked items into a text box. Once there saved in the text box then the page when reloaded should read the text box contents and put the check boxes in the table based on what is in the text box. The text box will be hidden, like a minature database. When the checked items are cleared or added then it should hopefully modify the text box to reflect the changes.

Any help is greatly appreciated.

Commented:
datagridview1_CellEnter all events are for window based grid.

Author

Commented:
sorry Jinal,

I don't really understand alot of ASP.net. Is it possible to read the table values from the DSN as I am doing and just store the checkbox information in a text box field?

I created a textbox field called 'new_hardware_list_saved' in the parent window which calls the IFRAME that has our code.

It feels like where very close to the answer.

Thanking you in advamce

Commented:
no if you open iframe inside your code it is not directly related to main page in asp.net.

Author

Commented:
In one of the applications we use we have two text fields which store data for the child iframe. They store Longitude and Latitude points for Virtual Earth. When you opne the child I frame it somehow read the value using parent.form.?.?.field.DataValue. When you change the GPS coordinates in the child Iframe it updates those two fields on the parent window.

Is it possible to achieve this as the text box in the parent window which will hold this value is unique to each client?

Author

Commented:
Any other idea's team. I feel we are very close to solving this one. All we need is to save the checked values in the page to a text box.

Author

Commented:
In my continuing search for the answer to my question I found another Experts Exchange article from a user needing a similar solution.

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_21337690.html?sfQueryTermInfo=1+datagrid+textbox

I have tried applying that same principle to my problem but to no avail.

Help!!!!

Commented:
Hello ,

As checkbox checked in each row  you want store all checked checkbox information.

Am i write ?

In that case .

 public void chkID_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk != null)
        {
            DataGridItem item = chk.NamingContainer as DataGridItem;
hiddenField1.Value = hiddenField1.Value + "#" + item.Cells[1].Text + " -- " + item.Cells[2].Text);
        }
    }


and in ASPX page
add

<asp:HiddenField ID="hiddenField1" runat="server" />

Now Hiddnefield1.Value contain all selected checkbox value . Make sure that your checkbox inside in grdiview is autopostback="ture"

Author

Commented:
Hi Jinal,

I'm glad your with me and still havn't given up. :)

I am getting the following error in event viewer after applying the above.

====

Exception information:
    Exception type: HttpParseException
    Exception message: Type 'System.Web.UI.WebControls.DataGrid' does not have a public property named 'HiddenField'.

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<%@ Register TagPrefix="chkbox" Namespace="DataGridControls" Assembly="DataGridCheckbox" %>
 
 
<script runat="server">
 
    void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack)
        {
        // TODO: Update the ConnectionString and CommandText values for your application
        string ConnectionString = @"DSN=server;uid=username;pwd=password";
        string CommandText = "select Items.ItemNumber, Items.ItemName FROM Items Items ORDER BY Items.ItemNumber ASC";
    
        OdbcConnection myConnection = new OdbcConnection(ConnectionString);
        OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
    
        myConnection.Open();
    
        DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        DataGrid1.DataBind();
    
        // close the connection
        myConnection.Close();
        }
    }
 
public void chkID_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox chk = sender as CheckBox;
       if (chk != null)
       {
           DataGridItem item = chk.NamingContainer as DataGridItem;
hiddenField1.Value = hiddenField1.Value + "#" + item.Cells[1].Text + " -- " + item.Cells[2].Text);
       }
   }
 
 
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <form id="Form1" runat="server">
        <asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" 
 
ForeColor="Black" EnableViewState="True">
<asp:HiddenField ID="hiddenField1" runat="server" />
<Columns>
                        <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="chkID" runat="server" 
                        oncheckedchanged="chkID_CheckedChanged" AutoPostBack="True" />
                </ItemTemplate>
            </asp:TemplateColumn>
 </Columns>
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

Open in new window

Author

Commented:
If possible the text box which contains the records must have each record on a new line.

Example.

Line 1 (Field 1 Field 2)
Line 2 (Field 1 Field 2)
Line 3 (Field 1 Field 2)

Where each line contains both fields.

Commented:

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<%@ Register TagPrefix="chkbox" Namespace="DataGridControls" Assembly="DataGridCheckbox" %>
 
 
<script runat="server">
 
    void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack)
        {
        // TODO: Update the ConnectionString and CommandText values for your application
        string ConnectionString = @"DSN=server;uid=username;pwd=password";
        string CommandText = "select Items.ItemNumber, Items.ItemName FROM Items Items ORDER BY Items.ItemNumber ASC";
    
        OdbcConnection myConnection = new OdbcConnection(ConnectionString);
        OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
    
        myConnection.Open();
    
        DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        DataGrid1.DataBind();
    
        // close the connection
        myConnection.Close();
        }
    }
 
public void chkID_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox chk = sender as CheckBox;
       if (chk != null)
       {
           DataGridItem item = chk.NamingContainer as DataGridItem;
hiddenField1.Value = hiddenField1.Value + "#" + item.Cells[1].Text + " -- " + item.Cells[2].Text);
           TextBox1.Text=  hiddenField1.Value;
       }
   }
 
 
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <form id="Form1" runat="server">
 <asp:HiddenField ID="hiddenField1" runat="server" />              
 <asp:TextBox ID="TextBox1" runat="server" />                
        <asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" 
 
ForeColor="Black" EnableViewState="True">
<Columns>
                        <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="chkID" runat="server" 
                        oncheckedchanged="chkID_CheckedChanged" AutoPostBack="True" />
                </ItemTemplate>
            </asp:TemplateColumn>
 </Columns>
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

Open in new window

Author

Commented:
Different error now. Looks like some ; symbol is missing

    Exception type: HttpCompileException
    Exception message: c:\Inetpub\wwwroot\myob\items.aspx(35): error CS1002: ; expected

Author

Commented:
Hi Jinal

I found line 35 had a ')' at the end so I removed it and now the code is executing.

There is now a new text bx at the top of the page with all the fields below it. The problem is now when you click on each row it thinks for a while then loads a blank page.

Author

Commented:
The way it should work is when you click on check box for each row the text box should begin to fill with the records.

Author

Commented:
The datagrid disappears once you check a value and the text box stays on the screen. The text box does not contain any value that was checked.

Commented:

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
 
 
<script runat="server">
 
    void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack)
        {
            // TODO: Update the ConnectionString and CommandText values for your application
            string ConnectionString = @"DSN=server;uid=username;pwd=password";
            string CommandText = "select Items.ItemNumber, Items.ItemName FROM Items ORDER BY Items.ItemNumber ASC";
 
            OdbcConnection myConnection = new OdbcConnection(ConnectionString);
            OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
 
            myConnection.Open();
 
            DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            DataGrid1.DataBind();
 
            // close the connection
            myConnection.Close();
            
        }
    }
 
public void chkID_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox chk = sender as CheckBox;
       if (chk != null)
       {
           DataGridItem item = chk.NamingContainer as DataGridItem;
hiddenField1.Value = hiddenField1.Value + "#" + item.Cells[1].Text + " -- " + item.Cells[2].Text;
           TextBox1.Text=  hiddenField1.Value;
       }
   }
 
 
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <form id="Form1" runat="server">
 <asp:HiddenField ID="hiddenField1" runat="server" />              
 <asp:TextBox ID="TextBox1" runat="server" />                
        <asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" 
 
ForeColor="Black" EnableViewState="True">
<Columns>
                        <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="chkID" runat="server" 
                        oncheckedchanged="chkID_CheckedChanged" AutoPostBack="True" />
                </ItemTemplate>
            </asp:TemplateColumn>
 </Columns>
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

Open in new window

Author

Commented:
Hi Jinal,

When you click on a checkbox the page begins to do something then the datagrid disappears. The text box remains at the top of the page without any data in it.

Commented:
Can you please capture datagrid image and put over here ?

Author

Commented:
Here you go. Hopefully comes out clearly.
datagrid.JPG

Commented:
One morething . Are you doing anything other than code you put over here ?

Commented:
I have same grid and it works my side so i am asking this.

Author

Commented:
Nothing outside the code I have posted.

When it AutoPostBacks it looks like it refreshes the datarid with a blank page.

Commented:
just test this and check that response.write print test on page or not.

Verify that checkbox is clicked ?

Also are you doin g ny type of pagging or sorting ?

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
 
 
<script runat="server">
 
    void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack)
        {
            // TODO: Update the ConnectionString and CommandText values for your application
            string ConnectionString = @"DSN=server;uid=username;pwd=password";
            string CommandText = "select Items.ItemNumber, Items.ItemName FROM Items ORDER BY Items.ItemNumber ASC";
 
            OdbcConnection myConnection = new OdbcConnection(ConnectionString);
            OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
 
            myConnection.Open();
 
            DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            DataGrid1.DataBind();
 
            // close the connection
            myConnection.Close();
            
        }
    }
 
public void chkID_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox chk = sender as CheckBox;
       if (chk != null)
       {
           DataGridItem item = chk.NamingContainer as DataGridItem;
           if(item != null)
           {
                Response.Write("Checkbox clicked");
           }
       }
   }
 
 
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <form id="Form1" runat="server">
 <asp:HiddenField ID="hiddenField1" runat="server" />              
 <asp:TextBox ID="TextBox1" runat="server" />                
        <asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" 
 
ForeColor="Black" EnableViewState="True">
<Columns>
                        <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="chkID" runat="server" 
                        oncheckedchanged="chkID_CheckedChanged" AutoPostBack="True" />
                </ItemTemplate>
            </asp:TemplateColumn>
 </Columns>
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

Open in new window

Author

Commented:
The only sorting taking place is the SQL statement in the code. After making the above changes it looks like it's still doing the same thing.

Author

Commented:
Hi Jinal,

I replaced the top line with the following code and it fixed the blank AutoPostBack issue.

It seems you must add EnableViewState="true" right at the beginning.

Now the text box is validating the checkboxed rows. The only problem is it will only validate 1 check boxed item. It will not list each row on a new line of the text box. Attached also is the screen dump.
<%@ Page Language="C#" EnableViewState="true" %>

Open in new window

error2.JPG
Commented:

<%@ Page Language="C#" EnableViewState="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
 
 
<script runat="server">
 
    void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack)
        {
            // TODO: Update the ConnectionString and CommandText values for your application
            string ConnectionString = @"DSN=server;uid=username;pwd=password";
            string CommandText = "select Items.ItemNumber, Items.ItemName FROM Items ORDER BY Items.ItemNumber ASC";
 
            OdbcConnection myConnection = new OdbcConnection(ConnectionString);
            OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
 
            myConnection.Open();
 
            DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            DataGrid1.DataBind();
 
            // close the connection
            myConnection.Close();
            
        }
    }
 
public void chkID_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox chk = sender as CheckBox;
       if (chk != null)
       {
           DataGridItem item = chk.NamingContainer as DataGridItem;
          
           TextBox1.Text=  TextBox1.Text +  item.Cells[1].Text + " -- " + item.Cells[2].Text + "\n" ;
       }
   }
 
 
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <form id="Form1" runat="server">
 <asp:HiddenField ID="hiddenField1" runat="server" />              
 <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Rows="6" Width="500px" />                
        <asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" 
 
ForeColor="Black" EnableViewState="True">
<Columns>
                        <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="chkID" runat="server" 
                        oncheckedchanged="chkID_CheckedChanged" AutoPostBack="True" />
                </ItemTemplate>
            </asp:TemplateColumn>
 </Columns>
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

Open in new window

Author

Commented:
Many Thanks Jinal. Perfect. The multiline command fixed the problem. Looks all good now.

Is it possible to get the values into a parent form located outside the iframe?

Author

Commented:
And just a quick one. How do I add a custom field as the last column. I need a field in the last column with a header price so I can manually input a price total.

Author

Commented:
Hi Jinal,

I also found that when I unselect the check box items it doesn't remove them from the text box. Is there a quick and dirty way to do this.

Commented:

<%@ Page Language="C#" EnableViewState="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
 
 
<script runat="server">
 
    void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack)
        {
            // TODO: Update the ConnectionString and CommandText values for your application
            string ConnectionString = @"DSN=server;uid=username;pwd=password";
            string CommandText = "select Items.ItemNumber, Items.ItemName FROM Items ORDER BY Items.ItemNumber ASC";
 
            OdbcConnection myConnection = new OdbcConnection(ConnectionString);
            OdbcCommand myCommand = new OdbcCommand(CommandText, myConnection);
 
            myConnection.Open();
 
            DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            DataGrid1.DataBind();
 
            // close the connection
            myConnection.Close();
            
        }
    }
 
public void chkID_CheckedChanged(object sender, EventArgs e)
   {
       CheckBox chk = sender as CheckBox;
       if (chk != null)
       {
            
           DataGridItem item = chk.NamingContainer as DataGridItem;
           //TextBox1.Text=  TextBox1.Text +  item.Cells[1].Text + " -- " + item.Cells[2].Text + "\n" ;
           if(chk.Checked)
           {
               Listbox1.Items.Add(item.Cells[1].Text + " -- " + item.Cells[2].Text);
           }
	   else 
	   {
               ListItem li =  Listbox1.Items.FindItemByText(item.Cells[1].Text + " -- " + item.Cells[2].Text);
               Listbox1.Items.Remove(li);
	   }	
           
       }
   }
 
 
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
    <form id="Form1" runat="server">
 <asp:HiddenField ID="hiddenField1" runat="server" />              
 <asp:ListBox ID="Listbox1" runat="server" Width="500px">
 <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Rows="6" Width="500px" />                
        <asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" 
 
ForeColor="Black" EnableViewState="True">
<Columns>
                        <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="chkID" runat="server" 
                        oncheckedchanged="chkID_CheckedChanged" AutoPostBack="True" />
                </ItemTemplate>
            </asp:TemplateColumn>
 </Columns>
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

Open in new window

Author

Commented:
Getting this error after the above code

Exception information:
    Exception type: HttpParseException
    Exception message: System.Web.UI.WebControls.ListItemCollection must have items of type 'System.Web.UI.WebControls.ListItem'. 'asp:TextBox' is of type 'System.Web.UI.WebControls.TextBox'.

Author

Commented:
Thank you very very much.

Author

Commented:
All good now. Many Thanks Jinal for your patience.

Take care and God Bless.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial