Link to home
Start Free TrialLog in
Avatar of Gaz124
Gaz124Flag for United Kingdom of Great Britain and Northern Ireland

asked on

DataGrid Template Column

Hi,

I have a DataGrid with a TemplateColumn which contains a textbox. I want to be able to bring back the value of the textbox to a string. Can anyone help me do this?

Thanks
Avatar of Gaz124
Gaz124
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER


I have tried the following code with little success!
protected void Button1_Click(object sender, EventArgs e)
        {
            //Get Username
        string[] username = Request.ServerVariables["LOGON_USER"].ToString().Split('\\');
            globalfunctions getAttribute = new globalfunctions();

            string storeEmail = getAttribute.getADattribute(username[1], "mail");

            DataGridItem item = (sender as Button).NamingContainer as DataGridItem;
            string Name = ((TextBox)(item["QTY"].FindControl("TextBox2"))).Text;

            TextBox Qty = (TextBox)((Button)sender).NamingContainer.FindControl("TextBox2");

Open in new window

Avatar of Bob Learned
It looks like you are trying to use FindControl with the item, but I don't believe that you have the right reference, but it is difficult to be sure, without seeing the HTML declaration for the DataGrid.
Avatar of Gaz124

ASKER

Ok, heres the code asp and c#. I can get every other part of the datagrid to my .csv but not the template column, Frustrating!!


<asp:GridView ID="GridView1" runat="server" DataSourceID="as400" 
        AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
        GridLines="None">
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <Columns>
         <asp:BoundField DataField="IMSKU" HeaderText="SKU"  />
         <asp:BoundField DataField="IMDESC" HeaderText="Description"  />
         <asp:BoundField DataField="JFFXQT" HeaderText="Required" 
                ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundField>
            <asp:TemplateField HeaderText="Qty"> 
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
           
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>


protected void Button1_Click(object sender, EventArgs e)
        {

            //Get Username
        string[] username = Request.ServerVariables["LOGON_USER"].ToString().Split('\\');
            globalfunctions getAttribute = new globalfunctions();

            string storeEmail = getAttribute.getADattribute(username[1], "mail");

            //append to csv file
            StreamWriter sw = File.AppendText("e:\\results\\decdiylayout.csv");

            //seperate datagrid to comma seperated values
             for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            string strRowVal = "";
            for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
            {
                if (strRowVal == "")
                {
                    strRowVal = DateTime.Now + "," + username[1].Remove(3)+ "," + Layout.Text + "," + GridView1.Rows[i].Cells[j].Text;
                }
                else
                {
                    strRowVal =  strRowVal + "," + GridView1.Rows[i].Cells[j].Text;
                }
            }
            sw.WriteLine(strRowVal);
        }
        sw.Close();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gaz124

ASKER

WOW TheLearnedOne Thats Great!!