[英]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.下面是组成 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:
标签和文本框的所有值都来自数据库,我想关注这个特定的文本框,这里是 aspx 代码:
<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注意 txtEditServiceStart 是 type=date
when the program runs here is the initial look:当程序在这里运行时是最初的样子:
Now, when the user clicks the Edit, to edit the said date of textbox(txtEditServiceStart),the textbox looks like this:现在,当用户单击编辑时,要编辑文本框(txtEditServiceStart)的所述日期,文本框如下所示:
it shows: mm/dd/yyyy and does not show the record from the database.它显示:mm/dd/yyyy 并且不显示数据库中的记录。
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.但是当用户没有更改该文本框的值并单击: UPDATE (基本上是为了保存更改)时,它将再次加载 gridview,问题是它不会保留文本框的日期,而是默认为: 1/1/1900。 Where it should show the data like from the Picture 1 again
它应该再次显示图 1 中的数据
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.请注意,当用户编辑日期时没有问题,它只会更新数据库,gridview 将显示新的值/日期。
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
基本上,如果用户未编辑数据而不是显示 1/1/1900,我只需要再次向 gridview 显示数据
I've tried using the txtEditServiceStart.Attributes.Add("readonly", "readonly");
我试过使用
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.但它不会识别我的 c# 代码上的文本框,我认为这是因为我从数据库绑定数据,也许程序不会运行。 I've also tried using Eval, instead of Bind but still not working
我也尝试过使用 Eval 而不是 Bind 但仍然无法正常工作
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: AccessLayer = 更新合约:
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请建议我可以添加到 aspx 或 c# 代码的方法/方法谢谢
Your problem is you always update your ContractDate
.你的问题是你总是更新你的
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
.添加
COALESCE
将为 ContractDate 分配一个值: @ContractDate
如果已填充,否则分配ContractDate
的现有值。 Change type of ContractDate in method to ?DateTime
it allow you put into method null
.将方法中的 ContractDate 类型更改为
?DateTime
它允许您将其放入方法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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.