2015-11-13 22 views
0

編輯圖像這裏是我的aspx:如何從GridView的

<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編輯特定的行,它會修改期望圖像。

所以圖像是不可編輯的。我可以知道如何編輯圖像嗎?

回答

0

嘗試這樣,

首先加2隱藏(hd_gv,hd_update)領域一個形式旁邊的圖像控制GridView控件內等,並設置與圖像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(hidden field)值。

更新代碼看起來是這樣的按鈕,點擊

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"; 
} 

Some useful Gridview Articles