0

我想使用UpdatePanel我的GridView,但是即使我添加了触发器, FileUploadinGridView也不起作用......因为它找不到FileUpload Button,解决方案是什么?

<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>
<Triggers>
  <asp:PostBackTrigger ControlID="btnupload" />
</Triggers>
</asp:UpdatePanel>
4

2 回答 2

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>

代码背后(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";
        }
于 2012-06-14T05:47:43.887 回答
1

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

  • 尝试将 GridView 放在 UpdatePanel 中。
  • 设置gridview的OnRowCommand="GridView1_RowCommand"事件
  • 将命令名设置为按钮 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
    }
}
于 2012-06-14T05:54:43.150 回答