如何从GridView编辑图像
本文关键字:编辑 图像 GridView | 更新日期: 2023-09-27 18:21:50
这是我的aspx:
<td>
<asp:FileUpload ID="fileupload" runat="server" />
</td>
cs:
这是插入:
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 = Path.GetFileName(fileupload.PostedFile.FileName);
fileupload.SaveAs(Server.MapPath("~/Images/" + filename));
com.Parameters.AddWithValue("@Image", "Images/" + filename);
com.ExecuteNonQuery();
Response.Redirect("studententry.aspx");
}
catch (Exception ex)
{
btnsub.Text = ex.Message;
}
插入所有详细信息后,它显示为this:
这是从gridview编辑的:
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;
}
}
当我编辑GridView
中的特定行时,它将编辑图像。
因此图像是不可编辑的。我可以知道如何编辑图片吗?
这样试试,
首先添加2个隐藏(hd_gv,hd_update)字段,一个在Gridview控件内的图像控件旁边,另一个在表单中,并使用image src设置隐藏字段值。
现在,在Gridview_RowCommand上,获取src并设置hd_update隐藏字段值,因此您的代码看起来像以下
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
SqlConnection con = Connection.DBconnection();
if (e.CommandName == "EditRow")
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
HiddenField imgSRC = (HiddenField)row.FindControl("hd_gv");
hd_update.Value=imgSRC.Value;
}
现在,在您的更新功能上,首先检查Fileupload控件是否有任何文件(如果有),然后上传文件并用新上传的文件更新图像src,否则设置hd_update(隐藏字段)值。
更新代码如下所示点击按钮
string Filename = hd_update.Value; // "HIDDEN_FIELD_IMAGE_SRC";
if (fileupload.HasFile)
{
//code to upload and set Filename variable with new file
Filename="NEW FILE NAME";
}