Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView FindControl Help

Status
Not open for further replies.

acent

Technical User
Feb 17, 2006
247
US
Greetings,

First, let me say that by now, I'm extremely frustrated and pissed that this seemingly simple idea is not so seemingly simple.

I have a GridView:
Code:
<asp:GridView ID="gvPTPs" runat="server"
  AutoGenerateColumns="false"
  AutoGenerateEditButton="true"
  AlternatingRowStyle-BackColor="#E9E9E9"
  DataKeyNames="ptp_rowid"
  CellPadding="4">
  <columns>
    <asp:TemplateField
      HeaderText="Due Date">
      <itemTemplate>
        <%#DataBinder.Eval(Container.DataItem,"Due") %>
        </itemTemplate>
        <edititemtemplate>
        <asp:TextBox ID="txtDue" MaxLength="10" Columns="10" Text='<%# DataBinder.Eval(Container.DataItem, "Due") %>' runat="server" />
      </edititemtemplate>
    </asp:TemplateField>
    ...
</asp:GridView>
When the gridview goes into edit mode, instead of my textbox being ID'd "txtDue", renamed to "gvPTPs_ctl02_txtDue". Therefore, when I run this:
Code:
  Protected Sub gvPTPs_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvPTPs.RowUpdating

  Dim txtNewDue As TextBox
  txtNewDue =  gvPTPs.Rows(e.RowIndex).FindControl("txtDue")
    lblError.Text=txtNewDue.Text
  ...
End Sub
The Null value is returned. And that makes sense because the ID of the control was changed. Forgive my attitude when I say, if .NET had left my id alone, I don't think we'd have this problem. I thought the whole purpose for IDing a control was so that you could easily reference it?!

Is there a way to easily reference this value that is posted back to the handler?

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
What exactly are you trying to do? Most likely you are tying to impliment logic in the wrong event. Describe what you want to do and maybe we can point you in the right direction.
 
you're confusing ID with ClientID. ID is the id on the server. however when the html is rendered the control is given a ClientID. the ClientID ensures that every control has a unique id. the pattern is to prepend the parent controls id to the current control.

as you could imagine this can get out of hand quickly. imagine
a control
within a repeater
within a user control
within a content place holder
within a page
within a master page!

as for the error, your syntax looks correct. I know VB has always been forgiving about type casting, but can it really cast a WebControl to a TextBox automatically?

if not try this
Code:
Dim converted = Convert.ToType(typeof(TextBox), txtNewDue)
If Converted = Nothing Then
   Throw new NullReferenceException("could not find control 'txtDue', or it is not a text box.");
EndIf
lblError.Text=converted .Text


One other quick note. I would stay away from gridviews and user a repeater instead. they produce less viewstate garbage and give you more control over the html.

I would also separate the data view (readonly) and data entry (create, edit, delete) into separate pages/user controls. this gives you a clear separation of concerns at a presentation level.

you can then add some ajax to the page to produce a sleek user experience where everything looks like it is a single page.

it's more plumbing on your part, but you get full control and better flexiblity.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
If you research you problem on line, you will find that many people have had the same problem when using template columns as you are. And as Jason says, it has nothing to do with the name of the control you see rendered in the HTML.
 
Thanks for the reply. I do appreciate the help.

I'm simply trying to allow the user to click on a row, update the value in the textbox, and presto, update the database.

Later in the sub, I'm want to use the value from that textobx to build the SQL and then use a command.executenonquery.
Code:
Dim oleCMD As New OleDbCommand
oleCMD = Conn.CreateCommand
oleCMD.CommandType = CommandType.Text
oleCMD.CommandText = "UPDATE tblPTPs SET due_date=#" & txtNewDue.Text & "# WHERE ptp_rowid=" & Trim(rowid) & " "
If conn.State = ConnectionState.Closed Then conn.Open()
oleCMD.ExecuteNonQuery()
Of course, the SQL statement will get a lot more complicated once I can return the values from the grid.



"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
Thanks alot! This is good information.

jmeckley, if I were to use a repeater instead, How would I go about it? I'm trying to stay away from have seperate pages where a user has to go back and forth. I thought repeaters didn't have the ability to easily switch to and from editmodes and delete rows the way a datagrid/gridview did.

As for ajax... yes, that is something I want to implement, though unless I'm on the powerful stuff, that would add a layer of complexity. I'm trying (desperately) to get this done yesterday to keep the big cheeses of the office happy.

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
I'm trying to stay away from have seperate pages where a user has to go back and forth.
how the application delivers the content and how the content is displayed on screen can be independent of one another. Webforms makes this very difficult to understand because
1. it's very linear thinking
2. it's not easily extensible
3. it have very poor separation of concerns
so developers think becuase I want it on this page, it must be in the code behind.

the mechanics of ajax and the use of separate pages for reading and writing are beyond the scope of this post. here are the key concepts though

ajax with webforms (not updatepanels, but true ajax requests to other URIs)
separation of concerns at an architectural level
command query seperation at an architectural level

I say "architectural level" because the same concepts apply to objects/members as well, but I'm talking about a higher level in the application.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top