我无法使用UpdatePanel中的Gridview中的FileUpload,也使用了触发器,我该如何解决?

时间:2012-06-14 05:01:04

标签: asp.net ajax gridview updatepanel

我想对UpdatePanel使用GridView,但即使我添加了触发器,FileUpload中的GridView也无效...因为它无法找到{ {1}} FileUpload,解决方案是什么?

Button

2 个答案:

答案 0 :(得分:3)

这是我通过代码隐藏的方式,它只是一个虚拟样机给你一个想法:

ASPX代码:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">        
    </asp:ScriptManager>  

<asp:UpdatePanel runat="server">
<ContentTemplate>
    <asp:Label runat="server" ID="Label1" Text=""></asp:Label>    
<asp:GridView runat="server" ID="GridView1" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
    <asp:TemplateField HeaderText="Upload Kundli">
                                <ItemTemplate>
                                    <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                                    <asp:Button ID="btnupload" runat="server" Text="Upload"       OnClick="btnupload_Click" />
                                </ItemTemplate>
                                 <ItemStyle HorizontalAlign="Center" />
                                <HeaderStyle HorizontalAlign="Center" />
                            </asp:TemplateField>

          </Columns>

    </asp:GridView>

    </ContentTemplate>

        </asp:UpdatePanel>
        </form>

Code Behind(ASPX.CS):

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<int> gridItems = new List<int>();
                gridItems.Add(1);
                gridItems.Add(2);
                GridView1.DataSource = gridItems;
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            //May not need this if. So check depending on what and how you are binding.
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button UploadButon = (Button) e.Row.Cells[0].FindControl("btnupload");
                if(UploadButon != null)
                {
                    if (this.ScriptManager1 != null)
                    {
                        this.ScriptManager1.
                            RegisterAsyncPostBackControl(UploadButon);
                    }
                }

            }

        }
        protected void btnupload_Click(object sender, EventArgs e)
        {
            Label1.Text = Label1.Text + "a";
        }

答案 1 :(得分:1)

FileUpload需要完整的回发才能正常工作。

  • 尝试将GridView放在UpdatePanel中。
  • 设置GridView的OnRowCommand =“GridView1_RowCommand”事件
  • 将命令名设置为Button CommandName =“upload”

     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
       <Triggers>
         <asp:PostBackTrigger ControlID="btnupload" />
       </Triggers>
       <ContentTemplate>
          <asp:GridView ID="GridView1" Runat="server" OnRowCommand="GridView1_RowCommand">
          <Columns>
            <asp:TemplateField HeaderText="Upload Kundli">
             <ItemTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                <asp:Button ID="btnupload" runat="server" Text="Upload" CommandName="upload" />
             </ItemTemplate>
           </asp:TemplateField>
      </Columns>
    

     

现在通过rowcommand事件获取Button。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "upload")
    {
       //your stuff
    }
}