gpt4 book ai didi

c# - 如何在回发后将文本保存到绑定(bind)数据库中数据的文本框

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:06 27 4
gpt4 key购买 nike

下面是组成 GridView 的模板字段之一。标签和文本框的所有值都来自数据库,我想关注这个特定的文本框,这里是 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>

注意 txtEditServiceStart 是 type=date

这里是程序运行时的初始样子:

图1 Picture 1

现在,当用户点击编辑,编辑文本框的日期(txtEditServiceStart),文本框看起来像这样:

图2 Picture 2

它显示:mm/dd/yyyy,不显示数据库中的记录。

但是 当用户没有更改该文本框的值并单击:UPDATE(基本上是为了保存更改)时,它将再次加载 gridview,问题是它不会保留文本框的日期,而是默认为:1/1/1900。它应该在哪里显示来自图片 1 的数据再次

请注意,当用户编辑日期时没有问题,它只会更新数据库并且 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

我试过使用 txtEditServiceStart.Attributes.Add("readonly", "readonly"); 但它无法识别我在 C# 代码上的文本框,我认为这是因为我 从数据库绑定(bind) 数据,程序可能无法运行。我也尝试过使用 Eval,而不是 Bind,但仍然无法正常工作

编辑:这是我将数据放在网格上的方式

 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();

编辑、取消编辑、更新的代码如下:

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 = 更新合约:

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();
}
}

请建议我可以在 aspx 或 c# 代码上添加的方式/方法谢谢

最佳答案

您的问题是您总是更新您的ContractDate。您必须编辑查询字符串。添加 COALESCE 将为 ContractDate 分配一个值,即:@ContractDate 如果已填充,否则它会分配 ContractDate 的现有值。将方法中的 ContractDate 类型更改为 ?DateTime 它允许您将方法放入 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();
}
}

关于c# - 如何在回发后将文本保存到绑定(bind)数据库中数据的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39739475/

27 4 0