Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在数据集中引用fileupload控件_C#_Asp.net_Sql - Fatal编程技术网

C# 如何在数据集中引用fileupload控件

C# 如何在数据集中引用fileupload控件,c#,asp.net,sql,C#,Asp.net,Sql,在我的程序中,当用户想要编辑记录并按下编辑按钮时,会打开一个包含所有字段的新窗口,并将记录信息呈现到相应的字段中,使用户可以选择编辑所需的任何字段信息 我已将fileupload控件添加到我的webform字段中。但我不知道如何在新弹出的窗口中引用fileupload控件。。我不确定我是否非常清楚地解释了我的问题,但我将尝试借助以下代码来解释它: protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

在我的程序中,当用户想要编辑记录并按下编辑按钮时,会打开一个包含所有字段的新窗口,并将记录信息呈现到相应的字段中,使用户可以选择编辑所需的任何字段信息

我已将fileupload控件添加到我的webform字段中。但我不知道如何在新弹出的窗口中引用fileupload控件。。我不确定我是否非常清楚地解释了我的问题,但我将尝试借助以下代码来解释它:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
     lblSet.Text = GridView1.Rows[e.NewEditIndex].Cells[2].Text;
     MultiView1.SetActiveView(vRecord);

     btnSave.Visible = false;
     btnBacktoHome.Visible = true;
     //this.lblMedium.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text;


     using (SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True"))
     {
         using (SqlCommand cmd = new SqlCommand())
         {
             String sql = "select [DocumentID],[Ref],[Subject],[Src],[Dst],[Medium],[Date_Printed],[Date_Received],[Document_Type],[Action_Required],[Due_Date],[Actual_Date],[Content],[Tag],[Issue_No],[Attachment],[Notes],[Assigned_To],[Reply_Ref],[Priority],[Status],[Response],[Physical_File_No],[Physical_Rack_Location] from dbo.Documents1 where [DocumentId]=N'" + GridView1.Rows[e.NewEditIndex].Cells[2].Text + "'";
             cmd.Connection = con;
             cmd.CommandText = sql;
             con.Open();

             //SqlDataAdapter da = new SqlDataAdapter(sql,con);
             //DataTable dt = new DataTable();
             DataSet ds = new DataSet();
             using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
             {
                 adp.Fill(ds);
             }


             this.txtRef.Text = ds.Tables[0].Rows[0][1].ToString();
             this.txtSubject.Text = ds.Tables[0].Rows[0][2].ToString();
             this.ddlSource.Text = ds.Tables[0].Rows[0][3].ToString();
             this.ddlDestination.Text = ds.Tables[0].Rows[0][4].ToString();
             this.ddlMedium.Text = ds.Tables[0].Rows[0][5].ToString();
             this.txtDatePrinted.Text = ds.Tables[0].Rows[0][6].ToString();
             this.txtDateReceived.Text = ds.Tables[0].Rows[0][7].ToString();
             this.ddlDocumentType.Text = ds.Tables[0].Rows[0][8].ToString();
             this.cbxAction.Checked = ds.Tables[0].Rows[0][9].Equals(cbxAction.Checked);
             this.txtDueDate.Text = ds.Tables[0].Rows[0][10].ToString();
             this.txtActualDate.Text = ds.Tables[0].Rows[0][11].ToString();
             this.txtContent.Text = ds.Tables[0].Rows[0][12].ToString();
             this.txtTag.Text = ds.Tables[0].Rows[0][13].ToString();
             this.txtIssue.Text = ds.Tables[0].Rows[0][14].ToString();

             //this.fileupload1 = ds.Tables[0].Rows[0][15] ;

             this.txtNotes.Text = ds.Tables[0].Rows[0][16].ToString();
             this.ddlAssignedTo.Text = ds.Tables[0].Rows[0][17].ToString();
             this.txtReplyRef.Text = ds.Tables[0].Rows[0][18].ToString();
             this.ddlPriority.Text = ds.Tables[0].Rows[0][19].ToString();
             this.ddlStatus.Text = ds.Tables[0].Rows[0][20].ToString();
             this.ddlResponse.Text = ds.Tables[0].Rows[0][21].ToString();
             this.txtPhysicalFileNo.Text = ds.Tables[0].Rows[0][22].ToString();
             this.txtPhysicalRackLocation.Text = ds.Tables[0].Rows[0][23].ToString();

             if (con != null)
             {
                 con.Close();
             }
             btnUpdate.Visible = true;
             btnSearch.Visible = false;
             BindGrid();
         }
     }
}
基本上,当用户单击“编辑”时,我的代码所做的就是读取sql server中的相关记录,并将其加载到我的Web表单中新弹出的窗口中。。将所有信息放在相关字段中。 我在网上读到,从sql读取varbinary数据并将其绑定到webform不像调用文本数据那么简单。(也许我错了,如果我错了,请纠正我)。我并不是真的担心从sql server获取数据到webform,我担心的是在新窗口中引用upload控件,因为如果用户在弹出窗口的fileupload控件中添加一个新文件,并且如果它没有在我的代码中引用,我的程序会忽略新上传的文件,这是我代码中的一个大缺陷。 这行代码有问题:

//this.fileupload1 = ds.Tables[0].Rows[0][15] ;
我已经注释掉它,让其他代码运行。
我被这件事困扰了整整一个星期。任何帮助都将不胜感激。提前感谢。

您不能将记录绑定到文件上载控件,此控件用于上载文件 不是 下载

看看这个,了解如何下载文件

当用户选择替换现有文件时,即用户将上载新文件,并且在您的业务逻辑中,您需要使用现有记录ID更新此记录时,应使用上载控件替换现有文件

在您的情况下,我会将附件的ID绑定到网格中的一个隐藏字段,而不使用上载控件。更新记录时,检查文件上载控件是否有文件,然后使用附件的值更新附件

编辑:我相信您需要添加以下内容:

FileUpload file = ((FileUpload)(GridView1.Rows[e.NewEditIndex].FindControl("myFileUploadControl")));
你需要给你的文件上传控件一个myFileUploadControl的ID(或者任何你想要的)


这还讨论了在gridview中使用fileupload控件。

是的,我知道我无法将记录绑定到fileupload控件,因为它用于上载而不是下载。我真的不需要下载这个文件。如果用户像您所说的“应该使用上载控件替换现有文件”那样添加新文件,我很高兴fileupload删除了旧文件。但我如何在新弹出窗口中引用fileupload控件?正如您所看到的,当用户单击编辑按钮时弹出的新弹出窗口中没有引用fileupload字段,这意味着fileupload控件在新窗口中不存在,因为我们的代码没有引用它。所以当用户添加一个文件时,它不会被读取。非常感谢Damon为您提供的最大帮助。我会仔细阅读你提供的材料,如果有任何疑问,我会回复你。再次感谢。非常感谢。很抱歉,我无法从你提供的文章中找到任何帮助。你能帮我做点什么吗?谢谢。您提供的链接为网格中的行编辑提供了帮助。但正如我的代码所示,我已经将行编辑带入了一个新窗口,并通过代码引用了所有字段。如果有人以某种方式帮助我如何通过代码引用文件上传,我想我可以让这个函数工作。