Solved

DataGrid <HeaderTemplate> question

Posted on 2004-03-22
10
563 Views
Last Modified: 2008-01-09
Hi guys,

I have a datagrid with a few columns, everything working fine, when I added the code below I start running into trouble.

I want to use the header row as my datagrid filter, thus when the user enter a value in Textbox2 (Drawing Number Header) and click my search button I want to run sql with a WHERE clause that includes only that drawing number.  However it seems like Textbox2 just is NOT RECOGNIZED in my codebehind page.


<asp:TemplateColumn HeaderText="DrawingNumber">
      <HeaderTemplate>
            <asp:TextBox id="Textbox2" runat="server" Text="enter drawing number"></asp:TextBox>
      </HeaderTemplate>
      <ItemTemplate>
            <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DrawingNumber") %>'>
            </asp:Label>
      </ItemTemplate>
      <EditItemTemplate>
            <asp:TextBox id=TextBox1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DrawingNumber")             %>'></asp:TextBox>
      </EditItemTemplate>
</asp:TemplateColumn>



My code run into my catch block when I set my sql string, below is a listing of my code-behind for further reference:



    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        PopDataGrid1()
    End Sub

    Function PopDataGrid1()
        Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("connStr"))
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet
        Dim dr As SqlDataReader = Nothing

        Try
            conn.Open()
            Dim sql As String

            sql = "SELECT PartNumber, PartDescription, DrawingNumber, ChangeLevel, ParentDrawing, " & _
                    " DrawingLocation, DrawingOrigin, StatusCode, UsageFlag, PPSNumber, PCONumber," & _
                    " EffectivePoint, WFUO, StockDisposition, DesignResponsibility, EngineerCode, UserID " & _
                    " FROM tblItemMaster WHERE DrawingNumber = '" & TextBox2.Text.ToString() & "' ORDER BY PartNumber"
            Response.Write("clicked search btn - 4")

            Dim rs As New SqlCommand(sql, conn)
            rs.CommandType = CommandType.Text
            da.SelectCommand = rs

            da.Fill(ds)
            DataGrid1.DataSource = ds
            DataGrid1.DataBind()

            If DataGrid1.Items.Count = 0 Then
                DataGrid1.Visible = False
            Else
                DataGrid1.Visible = True
            End If
        Catch
            Response.Write("catch blocked entered...")
        Finally
            If Not conn Is Nothing Then
                conn.Close()
                conn.Dispose()
                conn = Nothing
            End If
            If Not SqlDataAdapter1 Is Nothing Then
                da.Dispose()
                da = Nothing
            End If
        End Try
    End Function
0
Comment
Question by:africaoh
  • 5
  • 3
  • 2
10 Comments
 
LVL 7

Expert Comment

by:dante469
ID: 10649464
Since TextBox2 is part of the datagrid believe you will need to ref as follows:

CType(e.Item.FindControl("TextBox2"), textbox).Text


   sql = "SELECT PartNumber, PartDescription, DrawingNumber, ChangeLevel, ParentDrawing, " & _
                    " DrawingLocation, DrawingOrigin, StatusCode, UsageFlag, PPSNumber, PCONumber," & _
                    " EffectivePoint, WFUO, StockDisposition, DesignResponsibility, EngineerCode, UserID " & _
                    " FROM tblItemMaster WHERE DrawingNumber = '" & CType(e.Item.FindControl("TextBox2"), textbox).Text  & "' ORDER BY PartNumber"

BTW...  Why 125 points???

Have Fun,
Dante
0
 

Author Comment

by:africaoh
ID: 10649538
Hi Dante,

Makes sense, however my function is called:
Function PopDataGrid1()

The above function is called on the button clicked event that is:

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        PopDataGrid1()
End Sub


I would need a function with something like:
(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand

How would I go about getting "e.Item" to work in my function ??

Please help.
0
 
LVL 7

Expert Comment

by:dante469
ID: 10650261
Good question....

Typically e.item points to the selected item in the dg...  Since TextBox2 is part of the header and not a repeater may be able to access as follows:


CType(dg1.FindControl("TextBox2"), TextBox).Text  'This assumes dg1 is the name of your datagrid...

Please let me know if any issues...

Have Fun,
Dante


0
 

Author Comment

by:africaoh
ID: 10650392
Hi Dante,

Good idea, but it runs into my catch block, when I saw your code: dg1.FinControl("id") I thought off course that would work, but it didnt..

If you have any other ideas how to pickup that Textbox2 in my header please let me know.

Thank you.

Guess this question is worst 125p after all ;)
0
 
LVL 4

Accepted Solution

by:
locitt earned 125 total points
ID: 10654004
Dim txt As TextBox = IIf(AllowPaging,  CType(CType(CType(DataGrid1.Controls(0), Table).Rows(1), DataGridItem).FindControl("TextBox2"), TextBox), CType(CType(CType(DataGrid1.Controls(0), Table).Rows(0), DataGridItem).FindControl("TextBox2"), TextBox))

I assumed that the TemplateColumn is the first column with 0 index.

HTH.
locitt.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:africaoh
ID: 10657619
locitt,

Worked 100%, thanks alot!, btw did you just come up with that?, do you have some or other really good book your using or where do you get the solutions?

Thanks again!
0
 
LVL 4

Expert Comment

by:locitt
ID: 10663256
I spent 2 months to make a custom control inherited from MS DataGrid control, so I know it well. You can access to many sources about DataGrid control by search engine. Thanks for the points.

locitt.
0
 

Author Comment

by:africaoh
ID: 10663301
That's what im trying to do now with my datagrid (custom control), have to pack alot of functionality into the grid, including  header filters, scrollable, fixed headers, dynamic columns, sorting, paging, edit, ect.

Just need some advise on the following from you:
1. My tables that my dg queries has about 30000 records, what would be the most effective manner to get the data from the db, I thought about a disconnected dataset, have you worked with that some?  Or I have to cut back on my viewstate controls, at the moment it is quite slow.

2. Also I want to display a form over my dg when the user click on the row in order to update the row, dont want to do it in the record itself, here's an example:
http://www.infragistics.com/products/grids.asp (see second image from the top)

Hope to hear from you.


0
 
LVL 4

Expert Comment

by:locitt
ID: 10663413
wow, you try the same thing i've made. until now, i've got some of the features but not all.

>> 1. My tables that my dg queries has about 30000 records, what would be the most effective manner to get the data from the db, I thought about a disconnected dataset, have you worked with that some?  Or I have to cut back on my viewstate controls, at the moment it is quite slow.

I use disconnect dataset.
at the design time, it might need viewstate to store the dataset but runtime, i did with session object and unique key by GUID stored in viewstate.

with 3000 records, and multiple-editing mode (all are editable, with textboxes, dropdown,...controls), it's really heavy load.  But single editing mode is fine.

===> 2. Also I want to display a form over my dg when the user click on the row in order to update the row, dont want to do it in the record itself, here's an example:

if so, i will make a hidden div with all the controls needed for every controls in datagrid's columns. Then show the div when user clicks on the arrow button. And also, i think you need to hide all intersect SELECT element cause it will overlap your div.

here are some scripts those you might need:


function fx_elementTop(eSrc)
{
      var iTop = 0;
      var eParent;
      eParent = eSrc;
      while (eParent != null && eParent.tagName.toUpperCase() != "BODY")
      {
            iTop += eParent.offsetTop;

            if(eParent.scrollTop != null)
                  iTop -= eParent.scrollTop;

            eParent = eParent.offsetParent;
            if (eParent == null)
                  break;
      }
      return iTop;
}

function fx_elementLeft(eSrc)
{      
      var iLeft = 0;
      var eParent;
      eParent = eSrc;
      while (eParent  != null&& eParent.tagName.toUpperCase() != "BODY")
      {
            iLeft += eParent.offsetLeft;
            if(eParent.scrollLeft != null)
                  iLeft -= eParent.scrollLeft;
            eParent = eParent.offsetParent;
            if (eParent == null)
                  break;
      }
      return iLeft;
}


function fx_hideItsElement(elmID, eHost)
{
      var obj;

      // Hide any element that overlaps with the control eHost
      for (i = 0; i < fx_getTags(elmID).length; i++)
      {
            obj = fx_getTags(elmID)[i];
            if (fx_elementTop(obj) > parseInt(eHost.style.top) + eHost.offsetHeight)
            {
                  //if element is below bottom of control then do nothing
            }
            else if (fx_elementLeft(obj) > parseInt(eHost.style.left) + eHost.offsetWidth)
            {
                  //if element is to the right of control then do nothing
            }
            else if (fx_elementLeft(obj) + obj.offsetWidth < parseInt(eHost.style.left))
            {
                  //if element is to the left of control then do nothing
            }
            else if (fx_elementTop(obj) + obj.offsetHeight < parseInt(eHost.style.top))
            {
                  //if element is to the top of control then do nothing
            }
            else
            {
                  var n = obj.id == undefined ? obj.name : obj.id;
                  var o = fx_getChild(eHost, n);

                  if (o == null)
                  {
                        obj.style.visibility = "hidden";
                        hiddens[hiddenCount] = obj.uniqueID;
                        hiddenCount = hiddenCount + 1;
                  }
            }
      }
}

function fx_showItsElement()
{
      for (var i=0;i<hiddenCount;i++ )
      {
            var obj = fx_getById(hiddens[i]);
            if (obj != null)
            {
                  obj.style.visibility = "visible";
            }
      }

      hiddens = new Array();
      hiddenCount = 0;
}

function fx_clickInElement(elmID, x, y)
{
      var obj = fx_getById(elmID);
      var eleTop = fx_elementTop(obj);
      var eleLeft = fx_elementLeft(obj);
      var eleHeight = obj.offsetHeight;
      var eleWidth = obj.offsetWidth;
      return ((y >= eleTop) && (y <= (eleTop + eleHeight)) && (x >= eleLeft) && (x <= (eleLeft + eleWidth)));
}

locitt.
0
 

Author Comment

by:africaoh
ID: 10663456
locitt,

thanks for the scripts i will be sure to look at them, can you send me an email to upe79@hotmail.com, then we exchange ideas ect, also my grid sits on the intranet at the moment will move it to an internet host so you can have a look within a week or so from today.

let me know, it's good to know there is someone trying to do the same thing at the moment.

cheers
Cornelius

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 78
.NET RegEx to find all occurrences of substring matching specific pattern 4 51
Class object 2 27
Not seen Link button 5 15
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

13 Experts available now in Live!

Get 1:1 Help Now