locked
How to make this box default value = 1 before insert into database RRS feed

  • Question

  • User-775831949 posted

    I have DetailView table in insert mode and one column has code like this

    <asp:BoundField DataField="SpreadMonths"  ControlStyle-Width="250px" HeaderText="SpreadMonths" SortExpression="SpreadMonths" >

    How to code it so that when the DetailView is loaded, this box is pre-populated with a value of 1 ?

    Thanks

    Wednesday, December 20, 2017 10:16 AM

Answers

  • User2103319870 posted

    How to code it so that when the DetailView is loaded, this box is pre-populated with a value of 1

    You can use the DataBound event of DetailsView to insert default value

     protected void DetailView1_DataBound(object sender, EventArgs e)
            {
                if (DetailView1.CurrentMode == DetailsViewMode.Insert)
                {
                    DetailsView view = (DetailsView)sender;
                    DetailsViewRowCollection rows = view.Rows;
                    //First Row index is zero, change this as per your design
                    DetailsViewRow row = rows[0];
                    ((TextBox)row.Cells[1].Controls[0]).Text = "1";
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 20, 2017 12:39 PM
  • User-707554951 posted

    Hi hkbeer, 

    Working code as below:

       <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false"  
            AutoGenerateInsertButton="true" OnModeChanging="DetailsView1_ModeChanging" OnDataBound="DetailsView1_DataBound">
        <Fields>
            <asp:BoundField DataField="Id" HeaderText="Id" HeaderStyle-Font-Bold="true" />
            <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Font-Bold="true" />
            <asp:BoundField DataField="SpreadMonths"  ControlStyle-Width="250px" HeaderText="SpreadMonths" SortExpression="Description" />
        </Fields>
    </asp:DetailsView>

    Code behind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("SpreadMonths",typeof(string)) });
                    dt.Rows.Add(1, "John Hammond", "7");
                    DetailsView1.DataSource = dt;
                    DetailsView1.DataBind();
                }
            }
    
            protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
            {
    
                DetailsView1.ChangeMode(e.NewMode);
            }
    
            protected void DetailsView1_DataBound(object sender, EventArgs e)
            {
                if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
                {
                    DetailsView view = (DetailsView)sender;
                    DetailsViewRowCollection rows = view.Rows;
                    DetailsViewRow row = rows[2];
                    ((TextBox)row.Cells[1].Controls[0]).Text = "1";
                }
            }

    Output:

    Best regards 

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 21, 2017 2:33 AM

All replies

  • User2103319870 posted

    How to code it so that when the DetailView is loaded, this box is pre-populated with a value of 1

    You can use the DataBound event of DetailsView to insert default value

     protected void DetailView1_DataBound(object sender, EventArgs e)
            {
                if (DetailView1.CurrentMode == DetailsViewMode.Insert)
                {
                    DetailsView view = (DetailsView)sender;
                    DetailsViewRowCollection rows = view.Rows;
                    //First Row index is zero, change this as per your design
                    DetailsViewRow row = rows[0];
                    ((TextBox)row.Cells[1].Controls[0]).Text = "1";
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 20, 2017 12:39 PM
  • User-707554951 posted

    Hi hkbeer, 

    Working code as below:

       <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false"  
            AutoGenerateInsertButton="true" OnModeChanging="DetailsView1_ModeChanging" OnDataBound="DetailsView1_DataBound">
        <Fields>
            <asp:BoundField DataField="Id" HeaderText="Id" HeaderStyle-Font-Bold="true" />
            <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Font-Bold="true" />
            <asp:BoundField DataField="SpreadMonths"  ControlStyle-Width="250px" HeaderText="SpreadMonths" SortExpression="Description" />
        </Fields>
    </asp:DetailsView>

    Code behind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("SpreadMonths",typeof(string)) });
                    dt.Rows.Add(1, "John Hammond", "7");
                    DetailsView1.DataSource = dt;
                    DetailsView1.DataBind();
                }
            }
    
            protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
            {
    
                DetailsView1.ChangeMode(e.NewMode);
            }
    
            protected void DetailsView1_DataBound(object sender, EventArgs e)
            {
                if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
                {
                    DetailsView view = (DetailsView)sender;
                    DetailsViewRowCollection rows = view.Rows;
                    DetailsViewRow row = rows[2];
                    ((TextBox)row.Cells[1].Controls[0]).Text = "1";
                }
            }

    Output:

    Best regards 

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 21, 2017 2:33 AM