在c#dons';中编辑并提交图像;t工作
本文关键字:提交 图像 工作 c#dons 编辑 | 更新日期: 2023-09-27 18:22:19
我添加了上传图像字段,并将其存储在数据库(保存路径)和硬盘驱动器(图像)中。
当我编辑图片字段并提交时,它没有提交,而是在警报中显示"上传你的图片"。
所以我想我需要检查图像是否包括在硬盘驱动器或数据库中的图像路径中。
我不能这样做,因为我在.net上更新鲜。
这是我的密码;
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Image"></asp:Label>
</td>
<td>
</td>
<td>
<asp:Image ID="Image1" runat="server" ControlStyle-Width="50" ControlStyle-Height = "50" />
</td>
<td>
<asp:FileUpload ID="fileupload" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnsub" runat="server" Text="Submit" OnClick="btnsub_Click" OnClientClick="return register();" />
<asp:Button ID="btnrst" runat="server" Text="Reset" OnClick="btnrst_Click" />
</td>
</tr>
cs:
protected void btnsub_Click(object sender, EventArgs e)
{
SqlConnection con = Connection.DBconnection();
if (Textid.Text.Trim().Length > 0)
{
SqlCommand com = new SqlCommand("StoredProcedure3", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@id", Textid.Text.Trim());
com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
com.Parameters.AddWithValue("@Address", Textaddress.Text.Trim());
try
{
string filename = string.Empty;
if (fileupload.PostedFile.FileName.Length > 0)
{
filename = Path.GetFileName(fileupload.PostedFile.FileName);
fileupload.SaveAs(Server.MapPath("~/Images/" + filename));
}
com.Parameters.AddWithValue("@Image",(filename.Length>0)? "Images/" + filename:string.Empty);
com.ExecuteNonQuery();
}
catch (Exception ex)
{
btnsub.Text = ex.Message;
}
Response.Redirect("studententry.aspx");
}
else
{
SqlCommand com = new SqlCommand("StoredProcedure1", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
com.Parameters.AddWithValue("@Address", Textaddress.Text.Trim());
try
{
string filename = string.Empty;
if (fileupload.PostedFile.FileName.Length > 0)
{
filename = Path.GetFileName(fileupload.PostedFile.FileName);
fileupload.SaveAs(Server.MapPath("~/Images/" + filename));
}
com.Parameters.AddWithValue("@Image",(filename.Length>0)? "Images/" + filename:string.Empty);
com.ExecuteNonQuery();
}
catch (Exception ex)
{
btnsub.Text = ex.Message;
}
Response.Redirect("studententry.aspx");
}
}
protected void btnrst_Click(object sender, EventArgs e)
{
Textid.Text = string.Empty;
Textusername.Text = string.Empty;
Textclass.Text = string.Empty;
Textsection.Text = string.Empty;
Textaddress.Text = string.Empty;
Image1.ImageUrl = string.Empty;
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
SqlConnection con = Connection.DBconnection();
if (e.CommandName == "EditRow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
int index = gr.RowIndex;
hiddenfield.Value = index.ToString();
Textid.Text = gr.Cells[0].Text;
Textusername.Text = gr.Cells[1].Text;
Textclass.Text = gr.Cells[2].Text;
Textsection.Text = gr.Cells[3].Text;
Textaddress.Text = gr.Cells[4].Text;
Image1.ImageUrl = ((System.Web.UI.WebControls.Image)gr.Cells[5].Controls[0]).ImageUrl;
}
else if (e.CommandName == "Deleterow")
{
SqlCommand com = new SqlCommand("StoredProcedure4", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@ID", Convert.ToInt32(e.CommandArgument));
var id = Int32.Parse(e.CommandArgument.ToString());
com.ExecuteNonQuery();
Response.Redirect("studententry.aspx");
}
}
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
int index = GridView1.SelectedIndex;
hiddenfield.Value = index.ToString();
}
验证:
function register() {if (document.getElementById("<%=fileupload.ClientID%>").value == "") {
alert("Upload Your image !");
document.getElementById("<%=fileupload.ClientID%>").focus();
return false;
}
}
在文件上传控制中,您得到的是文件路径,而不是文件。所以它正在启动对文件的验证。如果您不想更改该记录的图像,请在编辑时禁用验证,或者您可以上传新的文件。
您的验证码应该是
function register() {
if (document.getElementById("<%=hiddenfield.ClientID%>").value == "") {
if (document.getElementById("<%=fileupload.ClientID%>").value == "") {
alert("Upload Your image !");
document.getElementById("<%=fileupload.ClientID%>").focus(); return false;
}
}
}