p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/forumdisplay.php?f=136)
-   -   Setting a DetailsView Insert default value (http://p2p.wrox.com/showthread.php?t=49731)

jpullam November 1st, 2006 06:49 PM

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
        End If
    End Sub

This of course doesn't work.

fadhil November 8th, 2006 09:28 AM

no answer???

wirerider November 10th, 2006 05:22 PM

don't know if this will help or not

davidrodrigues40 November 10th, 2006 06:31 PM

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

David Rodrigues
"There is no box"

GlenM November 15th, 2006 12:33 PM

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!


jam80920 November 16th, 2006 05:52 PM

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.


jpullam November 19th, 2006 10:51 AM

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

Hormigo August 9th, 2007 02:53 AM

Thanks a lot jpullam, your post solved my problem.

aboundweb September 11th, 2007 04:58 PM

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:


<asp:TemplateField HeaderText="COMPANY_ID" SortExpression="COMPANY_ID">
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("COMPANY_ID") %>'></asp:Label>
                    <asp:Label ID="Label2" runat="server" Text='<%# GetCompanyID() %>'></asp:Label>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("COMPANY_ID") %>'></asp:Label>

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


protected String GetCompanyID()
        String sCompany = "";
        if (Session["company_id"] != null)
            sCompany = (string)Session["company_id"];
            sCompany = "0";
        return sCompany;

I hope this helps

Joe Werner
Abound Inc.

gerardo_garza January 29th, 2008 06:37 PM

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

            <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" />

Hope this Helps...

All times are GMT -4. The time now is 07:41 AM.

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