Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 29th, 2006, 06:10 AM
Registered User
 
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Newbie - Weird Question

Hi All .. New to this .NET stuff and wonder if you can help me?

I have a detailsview control and in it I have asp:textbox fields.. I have created this sub and want it to auto populate the textbox's.

Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then
            dteCommentDate.Text = My.Computer.Clock.LocalTime.Date
            txtPostId.Text = Request.QueryString("news")
        End If
End Sub
Now the weird this is its asking me to declare dteCommentDate & txtPostId which are textbox's in my Details view..

BUT when I tried it out this code with just normal asp:textbox not in a details view control

Code:
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBox1.Text = My.Computer.Clock.LocalTime.Date
    End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:Content>
This works fine ... I am very confused can someone shed some light please??

Hmmm .NET ...
 
Old September 29th, 2006, 01:06 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Since you have it embedded in a details view, you have to use the detailsview to get to the textbox. Use the DataBound event of the detialsview, and get a reference to the textbox, then set it's value

    Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBound
        Dim tb As New TextBox
        tb = DetailsView1.FindControl("txtPostId.Text")
        tb.Text = Request.QueryString("news")
    End Sub

 
Old September 30th, 2006, 08:19 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

To make this work properly, you need to make a slight change to Jim's code:
Quote:
quote:Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBound
  Dim tb As <s>New </s>TextBox
  tb = CType(DetailsView1.FindControl("txtPostId"), TextBox)
  tb.Text = Request.QueryString("news")
End Sub
First, you don't need the new keyword, because tb is used to hold the textbox returned from FindControl.

It's also useful to cast the control returned by FindControl to a TextBox using CType, so you can access TextBox specific properties.

Finally, I changed txtPostId.Text to txtPostId in the FindControl argument as I am sure that's what Jim meant anyway....

Cheers

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie question pbb Ajax 0 September 26th, 2006 05:47 AM
Newbie question savoym JSP Basics 1 August 16th, 2006 03:15 AM
Newbie Question - Sorry! KP Crystal Reports 1 June 13th, 2006 02:45 AM
C# Newbie Question jazzcatone ASP.NET 2.0 Basics 1 May 25th, 2006 10:18 PM
newbie question Warbird XML 0 May 11th, 2005 08:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.