我正在处理上传功能,我在我的Updatepanel中有我的Fileupload控件。当用户点击上传按钮时,click事件应该触发插入。在我的代码中,我放置了所有的fileupload控件,按钮在updatepanel2.On中button3_click没有事件发生。
<div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" CssClass="cpHeader">
<asp:Label ID="Label1" runat="server" />
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" CssClass="cpBody">
<asp:Label ID="Label6" runat="server" Text="You have not specified a file"
Visible="False" ForeColor="#FF3300"></asp:Label>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" Width="412px" />
<br />
<br />
<asp:Button ID="Button3" runat="server" Text="Upload" OnClick="Button3_Click" />
<asp:Button ID="Button4" runat="server" Text="Cancel" />
<asp:Label ID="Label8" runat="server" ></asp:Label>
<asp:Label ID="Label9" runat="server"></asp:Label>
</asp:Panel>
<cc1:CollapsiblePanelExtender ID="CollapsiblePanelExtender2" runat="server"
TargetControlID="Panel2" ExpandControlID="panel1"
Collapsed="true" TextLabelID="label1" CollapsedText="Click to UPload a File"
ExpandedText=" Please Select file to Upload " CollapseControlID="Button4"
CollapsedSize="0">
</cc1:CollapsiblePanelExtender>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID ="Button3" EventName ="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
My Code behind for Button3_Click.
Protected Void Button3_click(object sender,eventargs e)
{
if (!IsPostBack)
{
Button3.Attributes.Add("OnClick", "MyOnButtonClick()");
string filepath = "~/"+"Uploads/" +
Path.GetFileName(FileUpload1.PostedFile.FileName);
string filename = Path.GetFileName(filepath);
string strcon =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
if (FileUpload1.HasFile)
{
string Viewname = string.Empty;
using (SqlConnection scn = new SqlConnection(strcon))
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox check_box = (CheckBox)gvrow.FindControl("checkbox1");
if (check_box != null)
{
if (check_box.Checked)
{
// perspective = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
//Convert.ToString(gvrow.Cells[1].Text);
perspective = Convert.ToString(gvrow.Cells[1].Text);
}
else
using (SqlCommand csm = scn.CreateCommand())
{
scn.Open();
SqlCommand cmd = new SqlCommand("Insert", scn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = TextBox1.Text;
cmd.Parameters.Add("@View", SqlDbType.NVarChar).Value = Viewname;
//conn.Open();
cmd.ExecuteNonQuery();
scn.Close();
FileUpload1.SaveAs(Server.MapPath(filepath));
Label8.Text = "FileSuccessfully Uploaded !";
}
}
}
}
} }