简体   繁体   中英

How to keep text to a texbox that binds data from a database after Postback

Below is one of the template field that composes a gridview. All of the value of the label and textboxes are from a database, and I want to focus on this specific textbox, here is the aspx code:

<asp:TemplateField HeaderText="ServiceStart" SortExpression="ServiceStart">
        <EditItemTemplate>
            <asp:TextBox ID="txtEditServiceStart" runat="server" type="date" Text='<%# Bind("ServiceStart","{0:d}") %>'  Width="128px"></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="LblItemServiceStart" runat="server" Text='<%# Bind("ServiceStart","{0:d}") %>' ></asp:Label>
        </ItemTemplate>
</asp:TemplateField>

Note that txtEditServiceStart is type=date

when the program runs here is the initial look:

Picture 1 图片1

Now, when the user clicks the Edit, to edit the said date of textbox(txtEditServiceStart),the textbox looks like this:

Picture 2 图2

it shows: mm/dd/yyyy and does not show the record from the database.

BUT when the user DID NOT change the value of that textbox and clicks: UPDATE (to basically to save changes), it will load the gridview again, and problem is it DOES'NT keeps the date of the textbox instead it will default to: 1/1/1900. Where it should show the data like from the Picture 1 again

Note that there is no problem when the user did edit the date, it will just update the database and gridview will show the new value/date.

Basically, I just need to show the data again to the gridview if its not edited by the user instead of showing 1/1/1900

I've tried using the txtEditServiceStart.Attributes.Add("readonly", "readonly"); but it wont recognize my textbox on the c# code, i think it's because I Bind data from a database, perhaps the program wont run. I've also tried using Eval, instead of Bind but still not working

EDIT: Here is how I put data on the grid

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid();
    }
}

public void BindGrid()
{
    SQLConnect.ConnectionString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
    SQLConnect.Open();
    string sql = "Select  LastName+', '+FirstName+' '+MiddleName  AS EmployeeName, EMPLOYEE.EmpID, ContractNo, ContractDate, ContractDuration, ServiceStart, ContractDetails, ContractValue, Served from EMPLOYEE join SCHOLARSHIPCONTRACT ON EMPLOYEE.EmpID = SCHOLARSHIPCONTRACT.EmpID";
    SqlCommand cmd = new SqlCommand(sql, SQLConnect);
    SqlDataAdapter GridDA = new SqlDataAdapter(cmd);
    DataSet GridDS = new DataSet();
    GridDA.Fill(GridDS);
    //Bind data to gridview
    GridView1.DataSource = GridDS;
    GridView1.DataBind();
    //close connection
    cmd.Dispose();
    SQLConnect.Close();

Here is how the edit, cancel edit, update's code:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditRow")
    {
        int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
        GridView1.EditIndex = rowIndex;

        BindGrid();
    }
    else if (e.CommandName == "CancelUpdate")
    {
        GridView1.EditIndex = -1;
        BindGrid();
    }
    else if (e.CommandName == "UpdateRow")
    {
        int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;

        string ContractNo = ((TextBox)GridView1.Rows[rowIndex].FindControl("txtEditContractNo")).Text;
        string contractDate = ((TextBox)GridView1.Rows[rowIndex].FindControl("txtEditContractDate")).Text;  
        string contractDuration = ((TextBox)GridView1.Rows[rowIndex].FindControl("txtEditContractDuration")).Text;
        string serviceStart = ((TextBox)GridView1.Rows[rowIndex].FindControl("txtEditServiceStart")).Text;
        string contractDetails = ((TextBox)GridView1.Rows[rowIndex].FindControl("txtEditContractDetails")).Text;
        string contractValue = ((TextBox)GridView1.Rows[rowIndex].FindControl("txtEditContractValue")).Text;
        string DropDownServed = ((DropDownList)GridView1.Rows[rowIndex].FindControl("DropDownServed")).Text;

        AccessLayer.UpdateContract(ContractNo, contractDate, contractDuration, serviceStart, contractDetails, contractValue, DropDownServed);
        GridView1.EditIndex = -1;
        BindGrid();

    }
}

AccessLayer = update contract:

public static int UpdateContract(string ContractNo, string ContractDate, string ContractDuration, string ServiceStart, string ContractDetails, string ContractValue, string Served)
{
    string css = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
    using (SqlConnection con = new SqlConnection(css))
    {
        string UpdateQuery = "UPDATE SCHOLARSHIPCONTRACT SET ContractDate= @ContractDate, ContractDuration = @ContractDuration, ServiceStart = @ServiceStart, ContractDetails = @ContractDetails, ContractValue= @ContractValue, Served = @Served WHERE ContractNo =@ContractNo";
        SqlCommand UpdateCmd = new SqlCommand(UpdateQuery, con);

        SqlParameter paramOriginalContractNo = new SqlParameter("ContractNo", ContractNo);
        UpdateCmd.Parameters.Add(paramOriginalContractNo);
        SqlParameter paramContractDate = new SqlParameter("@ContractDate", ContractDate);
        UpdateCmd.Parameters.Add(paramContractDate);
        SqlParameter paramContractDuration = new SqlParameter("@ContractDuration", ContractDuration);
        UpdateCmd.Parameters.Add(paramContractDuration);
        SqlParameter paramServiceStart = new SqlParameter("@ServiceStart", ServiceStart);
        UpdateCmd.Parameters.Add(paramServiceStart);
        SqlParameter paramContractDetails = new SqlParameter("@ContractDetails", ContractDetails);
        UpdateCmd.Parameters.Add(paramContractDetails);
        SqlParameter paramContractValue = new SqlParameter("@ContractValue", ContractValue);
        UpdateCmd.Parameters.Add(paramContractValue);
        SqlParameter paramServed = new SqlParameter("@Served", Served);
        UpdateCmd.Parameters.Add(paramServed);
        con.Open();
        return UpdateCmd.ExecuteNonQuery();
    }
}

Please suggest a way/method that i can add on the aspx or c# code thank you

Your problem is you always update your ContractDate . You have to edit query string. Add COALESCE will assign a value to ContractDate that is: @ContractDate if it was populated, otherwise it assigns the existing value of ContractDate . Change type of ContractDate in method to ?DateTime it allow you put into method null . When you call method just check textbox if date was edited put it in method if not set null.

 public static int UpdateContract(string ContractNo, DateTime? ContractDate, string ContractDuration, string ServiceStart, string ContractDetails, string ContractValue, string Served)
    {
        string css = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
        using (SqlConnection con = new SqlConnection(css))
        {
            string UpdateQuery = "UPDATE SCHOLARSHIPCONTRACT SET ContractDate=COALESCE(@ContractDate=ContractDate), ContractDuration = @ContractDuration, ServiceStart = @ServiceStart, ContractDetails = @ContractDetails, ContractValue= @ContractValue, Served = @Served WHERE ContractNo =@ContractNo";
            SqlCommand UpdateCmd = new SqlCommand(UpdateQuery, con);


            UpdateCmd.Parameters.AddWithValue("ContractNo", ContractNo);
            //Here you have to convert string to DateTime
            UpdateCmd.Parameters.AddWithValue("@ContractDate", ContractDate);
            UpdateCmd.Parameters.AddWithValue("@ContractDuration", ContractDuration);
            UpdateCmd.Parameters.AddWithValue("@ServiceStart", ServiceStart);
            UpdateCmd.Parameters.AddWithValue("@ContractDetails", ContractDetails);
            UpdateCmd.Parameters.AddWithValue("@ContractValue", ContractValue);
            UpdateCmd.Parameters.AddWithValue("@Served", Served);

            con.Open();
            return UpdateCmd.ExecuteNonQuery();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM