【问题标题】:How to edit image from GridView如何从 GridView 编辑图像
【发布时间】:2015-11-13 09:05:48
【问题描述】:

这是我的 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 编辑特定行时,它将编辑预期图像。

所以图像不可编辑。我可以知道如何编辑图像吗?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    这样试试,

    首先在Gridview控件和表单中的其他图像控件旁边添加2个隐藏(hd_gv,hd_update)字段,并使用图像src设置隐藏字段值。

    现在在 Gridview_RowCommand get src 和 set 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";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      相关资源
      最近更新 更多