更新图像没有在c#中的gridview中显示

时间:2015-12-29 08:34:58

标签: c# asp.net image gridview

我已经使用aspx和c#创建了学生详细信息表单。

在图片领域,我遇到了一些错误。

我做了什么:

插入,编辑和更新图片。

当我插入图片并提交时,我被设置为在同一页面上的gridview中显示该图像,并保存在我的驱动器中的“images”文件夹中。

它运行正常,但是当我编辑图像并上传新图像并提交时,更改的图像没有显示在gridview中,但是这个更改的图像保存在“images”文件夹中。

我可以知道,我的代码中的错误是什么?

这是我的源代码:

protected void btnsub_Click(object sender, EventArgs e)
        {
            SqlConnection con = Connection.DBconnection();
            if (Textid.Text.Trim().Length > 0)
            {
SqlCommand com = new SqlCommand("sp_updatestudentdetail", con);
                com.CommandType = CommandType.StoredProcedure;
try
                {
                    string filename = Image1.ImageUrl.Substring(Image1.ImageUrl.IndexOf('/')+1);
                    string[] files = Directory.GetFiles(Server.MapPath("~/Images"));
                    string uniqueFileName = string.Empty;                                    
                    if (fileupload.PostedFile.FileName.Length > 0)
                    {
                        foreach (string f in files) File.Delete(f);    
                        filename = Path.GetFileName(fileupload.PostedFile.FileName);
                        string fileExtension = Path.GetExtension(filename).ToLower();
                        uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
                        fileupload.SaveAs(Server.MapPath("~/Images/" + uniqueFileName));                 
                    }

                        com.Parameters.AddWithValue("@Image", (filename.Length > 0) ? "Images/" + filename : (uniqueFileName.Length > 0) ? "Images/" + uniqueFileName : string.Empty);
                        com.ExecuteNonQuery();   
                }

                catch (Exception ex)
                {
                    btnsub.Text = ex.Message;
                }                  
}



 else
            {
                SqlCommand com = new SqlCommand("sp_insertstudentdetail", con);
                com.CommandType = CommandType.StoredProcedure;
 try
                {
                    string filename = string.Empty;
                    string uniqueFileName = string.Empty;
                    if (fileupload.PostedFile.FileName.Length > 0)
                    {
                        filename = Path.GetFileName(fileupload.PostedFile.FileName);
                        string fileExtension = Path.GetExtension(filename).ToLower();
                        uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
                        fileupload.SaveAs(Server.MapPath("~/Images/" + uniqueFileName));
                    }                        

                    com.Parameters.AddWithValue("@Image", (filename.Length > 0) ? "Images/" + uniqueFileName : string.Empty);
                    com.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    btnsub.Text = ex.Message;
                }
}
}

这是editrow:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            SqlConnection con = Connection.DBconnection();

            if (e.CommandName == "EditRow")
            { 
                Image1.ImageUrl = ((System.Web.UI.WebControls.Image)gr.Cells[7].Controls[0]).ImageUrl;
}
}

这是我的截图输出:

http://s3.postimg.org/bpgzjlmub/untitled.jpg

任何人都可以帮我修复我的问题,我是.net。

的新手

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

asp.net中的gridview不会自动更新。您需要再次绑定gridview。你需要做的是将绑定网格的代码放在一个函数中,如 mRecycledView.setOnTouchListener(new View.OnTouchListener() { int LONG_PRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout(); float mItemViewHeight, mInitialY; boolean mIsResolved; final Handler mHandler = new Handler(Looper.getMainLooper()); final Runnable mLongPress = new Runnable() { @Override public void run() { mIsResolved = true; onLongClick(); } }; @Override public boolean onTouch(View v, MotionEvent me) { switch (MotionEventCompat.getActionMasked(me)) { case MotionEvent.ACTION_DOWN: mInitialY = me.getY(); mActiveUserView = mRecycledView.findChildViewUnder(me.getX(), mInitialY); if (mActiveUserView == null) { // clicked to RecycledView where's no item mIsResolved = true; } else { mIsResolved = false; mItemViewHeight = (float) mActiveUserView.getHeight(); mHandler.postDelayed(mLongPress, LONG_PRESS_TIMEOUT); } break; case MotionEvent.ACTION_MOVE: if (!mIsResolved) { stopLongClickHandler(); // check for vertical upward move if (mInitialY - me.getY() > mItemViewHeight) { mIsResolved = true; onVerticalDrag(); } } break; case MotionEvent.ACTION_UP: if (!mIsResolved) { stopLongClickHandler(); mIsResolved = true; onClick(); } } return !mIsResolved; } void stopLongClickHandler() { mHandler.removeCallbacksAndMessages(null); } void onVerticalDrag() { Log.e("DEBUG", "start drag"); } void onLongClick() { Log.e("DEBUG", "long click"); } void onClick() { Log.e("DEBUG", "short click"); } }); 这样。

bindgrid()

然后在网格中执行每次插入和更新后调用public void bindgrid() { con = new SqlConnection(connStr); con.Open(); da = new SqlDataAdapter("select * from Grid_Data", con); Dataset ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } 。如果这没有成功,我可能需要看到你的绑定逻辑。 谢谢