【问题标题】:Gridview not selecting Images gives errorGridview 不选择图像会出错
【发布时间】:2015-06-25 10:26:07
【问题描述】:

我有一个gridview,我必须通过FileUpload 选择图像并提交按钮单击,以便将路径和相关信息提交到数据库中。

但是当我将 gridview 保持为空并单击提交按钮时,我收到错误

必须声明标量变量“@img_path”。

我有什么方法可以提供验证,以便用户必须选择图像,然后只提交表单

下面是我的代码:-

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString);
        foreach (GridViewRow row in ImagesGrid.Rows)
        {
            var title = row.FindControl("txtTitle") as TextBox;
            var description = row.FindControl("txtDescription") as TextBox;
            var imageFile = row.FindControl("flUpload") as FileUpload;
            var chkDefault = row.FindControl("chkDefault") as CheckBox;
            using (SqlCommand cmd = conn.CreateCommand())
            {
                conn.Open();
                SqlCommand cmd1 = new SqlCommand("Insert into tbl_galleries_stack (gallery_id,img_title,img_desc,img_path,IsDefault) values (@gallery_id,@img_title,@img_desc,@img_path,@IsDefault)", conn);
                cmd1.Parameters.Add("@gallery_id", SqlDbType.Int).Value = Convert.ToInt32(ddlgallery.SelectedValue);
                cmd1.Parameters.Add("@img_title", SqlDbType.NVarChar).Value = title.Text;
                cmd1.Parameters.Add("@img_desc", SqlDbType.NVarChar).Value = description.Text;
                if (imageFile.HasFile)
                {
                    imageFile.SaveAs(Server.MapPath("~/GalleryImages/") + imageFile.FileName);
                    cmd1.Parameters.Add("@img_path", SqlDbType.NVarChar).Value = "~/GalleryImages/" + imageFile.FileName;
                }
                cmd1.Parameters.Add("@IsDefault", SqlDbType.Bit).Value = chkDefault.Checked;
                cmd1.ExecuteNonQuery();
                conn.Close();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Gallery images added sucessfully');window.location ='csrgalleriesstack.aspx';", true);
            }
        }
    }

请提出建议

【问题讨论】:

  • 我强烈怀疑你的imageFile.HasFile 如果false 并且它没有添加这个参数。您是否调试过您的代码并看到它确实将您的代码放入了这个 if 语句中?
  • @SonerGönül:是的,我调试了代码,但它仍在内部并给出了我在帖子中提到的错误

标签: c# asp.net gridview file-upload


【解决方案1】:

是的,您可以为上传控件添加 customvalidator 并在那里添加验证逻辑。如果您希望在回发之前完成,您可以编写一个简单的 javascript 验证函数并将其分配给 customvalidator 的 ClientValidationFunction 属性。

aspx 页面中的类似内容应该可以完成这项工作,但请注意,我尚未对其进行测试:

    <asp:CustomValidator ID="cv" runat="server" Display="Dynamic" ControlToValidate="imageFile"             
ErrorMessage="Please select file to upload." ValidateEmptyText="True" SetFocusOnError="true"
    ClientValidationFunction="ValidateUploadedFile">
    </asp:CustomValidator>

    <script language="javascript"> 
       function ValidateUploadedFile(source, arguments) { 
            var path = document.getElementById('<%= imageFile.ClientID %>').value;
            if (path != "")
            {
                arguments.IsValid = true;
            }       
            else
            {
                arguments.IsValid = false;
            }
        }
    </script>

【讨论】:

  • 你能用代码显示同样的东西吗?如何验证?
  • 不工作,错误为Control 'ImagesGrid' referenced by the ControlToValidate property of 'cv' cannot be validated.
猜你喜欢
  • 2016-03-05
  • 1970-01-01
  • 2019-04-17
  • 1970-01-01
  • 2020-12-26
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多