gpt4 book ai didi

c# - 如何在 asp.net 应用程序中实现编辑功能?

转载 作者:太空宇宙 更新时间:2023-11-03 14:20:19 26 4
gpt4 key购买 nike

下面 2 个链接提供了我的示例应用程序的预览。

http://img812.imageshack.us/i/image1adl.jpg/ :显示我的示例应用程序。所有字段都是 self 解释的(如果有疑问,请告诉我)

http://img834.imageshack.us/i/image2vc.jpg/ :显示,当单击网格中的“编辑”按钮时,时间显示正确但顺序受到干扰。 (请参阅顶部的 7:00,然后可以看到时间列表)。

我的问题

  1. 如何纠正计时问题? (链接 # 2)

下面是“编辑”的代码

protected void lnkEdit_Click(object sender, EventArgs e)
{
int imageid = Convert.ToInt16((sender as Button).CommandArgument);

DataSet ds = new DataSet();

SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["TestConn"].ConnectionString;
string sql = @"SELECT * FROM Images WHERE IsDeleted=0 and Imageid='"+ imageid +"'";

SqlCommand sqlcommand = new SqlCommand(sql, sqlconn);
sqlcommand.CommandType = CommandType.Text;
sqlcommand.CommandText = sql;

SqlDataAdapter da = new SqlDataAdapter(sqlcommand);
da.Fill(ds);

if (ds.Tables[0].Rows.Count > 0)
{
txtImageName.Text = ds.Tables[0].Rows[0].ItemArray[1].ToString();
chkIsActive.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"].ToString());
ddlStartTime.DataSource = ds;
ddlStartTime.DataTextField = ds.Tables[0].Columns["StartTime"].ColumnName.ToString();
ddlStartTime.DataValueField = ds.Tables[0].Columns["ImageId"].ColumnName.ToString();
ddlStartTime.DataBind();
ddlEndTime.DataSource = ds;
ddlEndTime.DataTextField = ds.Tables[0].Columns["EndTime"].ColumnName.ToString();
ddlEndTime.DataValueField = ds.Tables[0].Columns["ImageId"].ColumnName.ToString();
ddlEndTime.DataBind();
BindDropDownList();
IsEdit = true;
}
  1. 当我编辑网格中的现有记录时,我正在获取值,但该记录并未更新,而是作为新记录添加到数据库中。我知道我想写更新脚本。但是在哪里写呢?

下面代码同理;

protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
try
{
string strImageName = txtImageName.Text.ToString();
int IsActive = 1;
if (chkIsActive.Checked)
IsActive = 1;
else
IsActive = 0;

string startDate = ddlStartTime.SelectedItem.Text;
string endDate = ddlEndTime.SelectedItem.Text;

if ( Convert.ToDateTime(endDate) - Convert.ToDateTime(startDate) > new TimeSpan(2, 0, 0) || Convert.ToDateTime(endDate)- Convert.ToDateTime(startDate) < new TimeSpan(2,0,0))
{
//Response.Write(@"<script language='javascript'> alert('Difference between Start Time and End Time is 2 hours'); </script> ");
lblHours.Visible = true;
lblHours.Text = "Difference between Start Time and End Time should be 2 hours";
return;
}

if (checkConflictTime())
{
lblMessage.Visible = true;
lblMessage.Text = "Time Conflict";
return;
}

//if (checkTimeBetween())
//{
//}

if (fuFileUpload.PostedFile != null && fuFileUpload.PostedFile.FileName != "")
{
lblHours.Visible = false;
byte[] imageSize = new Byte[fuFileUpload.PostedFile.ContentLength];
HttpPostedFile uploadedImage = fuFileUpload.PostedFile;
uploadedImage.InputStream.Read(imageSize, 0, (int)fuFileUpload.PostedFile.ContentLength);

SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["TestConn"].ConnectionString;

SqlCommand cmd = new SqlCommand();
if (IsEdit == false)
{
cmd.CommandText = "Insert into Images(FileName,FileContent,IsDeleted,IsActive,StartTime,EndTime) values (@img_name, @img_content,@IsDeleted,@IsActive,@StartTime,@EndTime)";
}
else
{
cmd.CommandText = "Update Images set FileName=@img_name, FileContent=@img_content, IsDeleted= @IsDeleted,IsActive= @IsActive, StartTime=@StartTime,EndTime=@EndTime";
}
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlconn;

SqlParameter ImageName = new SqlParameter("@img_name", SqlDbType.NVarChar, 50);
ImageName.Value = strImageName.ToString();
cmd.Parameters.Add(ImageName);

SqlParameter ActualImage = new SqlParameter("@img_content", SqlDbType.VarBinary);
ActualImage.Value = imageSize;
cmd.Parameters.Add(ActualImage);

SqlParameter DeletedImage = new SqlParameter("@IsDeleted", SqlDbType.Bit);
DeletedImage.Value = 0;
cmd.Parameters.Add(DeletedImage);

SqlParameter IsActiveCheck = new SqlParameter("@IsActive", SqlDbType.Bit);
IsActiveCheck.Value = IsActive;
cmd.Parameters.Add(IsActiveCheck);

SqlParameter StartDate = new SqlParameter("@StartTime", SqlDbType.NVarChar, 100);
StartDate.Value = startDate;
cmd.Parameters.Add(StartDate);

SqlParameter EndDate = new SqlParameter("@EndTime", SqlDbType.NVarChar, 100);
EndDate.Value = endDate;
cmd.Parameters.Add(EndDate);

sqlconn.Open();
int result = cmd.ExecuteNonQuery();
sqlconn.Close();

if (result > 0)
{
lblMessage.Visible = true;
lblMessage.Text = "File Uploaded!";
gvImages.DataBind();
}
}
}
catch (Exception ex)
{
lblMessage.Text = ex.ToString();
}
}

}

请帮忙!

最佳答案

你在哪里定义Bool/Bolean IsEdit?我认为它的值在页面回发时被重置,这就是为什么它总是 false 并且正在插入记录。我建议您使用一个隐藏字段来跟踪它并在那里设置它的值并在插入/更新时检查该值。最后它会像...

if (ds.Tables[0].Rows.Count > 0)
{
//your code
hiddenField.Value = "true"; // you can set default value to false
}

之后

if (hiddenField.Value == "false")
{
cmd.CommandText = "Insert into Images(FileName,FileContent,IsDeleted,IsActive,StartTime,EndTime) values (@img_name, @img_content,@IsDeleted,@IsActive,@StartTime,@EndTime)";
}
else
{
cmd.CommandText = "Update Images set FileName=@img_name, FileContent=@img_content, IsDeleted= @IsDeleted,IsActive= @IsActive, StartTime=@StartTime,EndTime=@EndTime";
}

关于c# - 如何在 asp.net 应用程序中实现编辑功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5612398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com