Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 1st, 2006, 06:49 PM
Authorized User
 
Join Date: Oct 2006
Location: Toronto, Ontario, Canada.
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
Default Setting a DetailsView Insert default value

I'm trying to do what would seem like a fairly straightforward thing: I am using a GridView/DetailsView combo and programmming it to insert a new record.

I'd like to specify default values for some fields so that when the user clicks on "New", it shows those values when the input form opens, but I can't figure out how to do it. There are many events and properties, but nothing I can manage to make work. I've found a few other queries like this on the net and the only suggestion was to use the ItemCreated event. That author tried to set the value with this statement

Ctype(DetailsView1.Rows(1).Cells(1).Controls(0), TextBox).Text= GridView1.SelectedRow.Cells(2).Text ===>>>??????

I don't understand it, it didn't work for me, and am surprised at how archaic coding row and column by number would be. In old ASP, I'd just place the default in the field before displaying it, and then get the user-entered value back after with objRS.

Is there anything like that in .Net?

For what its worth, my current version of the function call is

    Protected Sub DetailsView1_ItemCreated(ByVal sender As Object, _
        ByVal e As EventArgs)
        If DetailsView1.CurrentMode = DetailsViewMode.Insert Then
        DetailsView1.Values("InsertName")="hello"
        End If
    End Sub


This of course doesn't work.

Reply With Quote
  #2 (permalink)  
Old November 8th, 2006, 09:28 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

no answer???

Reply With Quote
  #3 (permalink)  
Old November 10th, 2006, 05:22 PM
Authorized User
Points: 514, Level: 8
Points: 514, Level: 8 Points: 514, Level: 8 Points: 514, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

don't know if this will help or not
http://dotnetjunkies.com/QuickStartv.../advanced.aspx

Reply With Quote
  #4 (permalink)  
Old November 10th, 2006, 06:31 PM
Authorized User
 
Join Date: Nov 2006
Location: Bristol, TN, USA.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to davidrodrigues40
Default

I would create templates then the code will be able to see the textbox control.

David Rodrigues
"There is no box"
Reply With Quote
  #5 (permalink)  
Old November 15th, 2006, 12:33 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was facing a similar problem. I coded the DataBound event on the Details view. I still had to access the specific Row/Cell/Control I needed.

protected void DetailsView1_DataBound(object sender, EventArgs e) {
    if (DetailsView1.CurrentMode == DetailsViewMode.Insert) {
        CheckBox cbx = (CheckBox)DetailsView1.Rows[3].Cells[1].Controls[0];
        cbx.Checked = true;
    }
}

If there are other controls you need defaulted, just obtain a reference and set.

Hope this works for you!

Glen

Reply With Quote
  #6 (permalink)  
Old November 16th, 2006, 05:52 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The easiest thing I found was to put the default value in the InsertQuery parameters for the DataSource. It's not perfect, since it doesn't show the default value in the DetailsView.

The more correct way to do it is to make your field a Template Field and do the data binding there. I used this for several DropDownList style fields where I want to display a specific set of user friendly names, but I have an internal ID that needs to get stored in the database. The template field lets you bind to another DataSource and have the Text and Value feature of the DropDownList. Since my app is an internal administrative tool, I'm not going this far for fields that are just text fields.

Jeff
Reply With Quote
  #7 (permalink)  
Old November 19th, 2006, 10:51 AM
Authorized User
 
Join Date: Oct 2006
Location: Toronto, Ontario, Canada.
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
Default

After a lot of trial, error and searching of the Internet, I found a fairly elegant solution to this. Here is the code I now use:

Protected Sub DetailsView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.PreRender
If DetailsView1.CurrentMode = DetailsViewMode.Insert Then
    Dim ThisControl As TextBox
    ThisControl = DetailsView1.FindControl("InsertEventName")
    ThisControl.Text = "new event"
    ThisControl = DetailsView1.FindControl("InsertDays")
    ThisControl.Text = "1"
    End if
End Sub



Reply With Quote
  #8 (permalink)  
Old August 9th, 2007, 03:53 AM
Registered User
 
Join Date: Aug 2007
Location: Barcelona, Barcelona, Spain.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hormigo
Default

Thanks a lot jpullam, your post solved my problem.

Reply With Quote
  #9 (permalink)  
Old September 11th, 2007, 05:58 PM
Registered User
 
Join Date: Sep 2007
Location: West Springfield, Massachusetts, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey there,

This is Joe from Abound Inc. I spent some time on this issue and found the simple solution. You simply need to create a template field for the field you want to set a default value for. In the template field ASP.Net is very flexible in that you can put any type of control in there whether it be a label, text box, checkbox etc... You set the value for that template field by making a call to a method you create in the code-behind file. I have a page called Clients.aspx where I have a gridview and detailsview. In this page I set it up so that when the user clicks to add a new client, the COMPANY_ID field is set to session variable and cannot be changed by the user.

In the Clients.aspx file the code for the template field looks like this:

Code:
<asp:TemplateField HeaderText="COMPANY_ID" SortExpression="COMPANY_ID">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("COMPANY_ID") %>'></asp:Label>
                </EditItemTemplate>
                <InsertItemTemplate>
                    &nbsp;
                    <asp:Label ID="Label2" runat="server" Text='<%# GetCompanyID() %>'></asp:Label>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("COMPANY_ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>


Notice the call to GetCompanyID() in the InsertItemTemplate. That method is defined in the code-behind file (Clients.aspx.cs):

Code:
protected String GetCompanyID()
    {
        String sCompany = "";
        if (Session["company_id"] != null)
        {
            sCompany = (string)Session["company_id"];
        }
        else
        {
            sCompany = "0";
        }
        return sCompany;
    }


I hope this helps

Joe Werner
Abound Inc.
www.aboundweb.com

Reply With Quote
  #10 (permalink)  
Old January 29th, 2008, 06:37 PM
Registered User
 
Join Date: Jan 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gerardo_garza
Default

Hey guys i've found this way 4 setting a non-modifiable default value within the FormView guess it's the same 4 details view.

I currently have 2 FormViews 1 wich as a master record and the other one holds detail records i'm using the asp:ControlParameter 4 setting this up within the ObjectDataSource, So Here it goes

<InsertParameters>
            <asp:ControlParameter ControlID="FormView1" Name="IDClienteProveedor"
                PropertyName="SelectedValue" Type="Int32" />
            <asp:Parameter Name="Calle" Type="String" />
            <asp:Parameter Name="NumeroInterior" Type="String" />
            <asp:Parameter Name="Colonia" Type="String" />
            <asp:Parameter Name="Ciudad" Type="String" />
            <asp:Parameter Name="Estado" Type="String" />
            <asp:Parameter Name="CodigoPostal" Type="String" />
        </InsertParameters>

Hope this Helps...

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving the new/insert button on DetailsView jpullam ASP.NET 2.0 Basics 3 September 27th, 2007 06:29 PM
DropDownList value insert to MS DB(DetailsView) yukijocelyn ASP.NET 2.0 Basics 2 August 23rd, 2007 02:32 AM
Insert DropDownList(DetailsView) value toMS Access yukijocelyn ASP.NET 2.0 Professional 0 August 22nd, 2007 10:49 PM
DetailsView checkbox default appearance jpullam ASP.NET 2.0 Professional 2 November 29th, 2006 02:37 PM
setting default values prithvi28 XSLT 1 October 3rd, 2006 02:38 PM



All times are GMT -4. The time now is 02:20 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.