我想将UpdatePanel
用于我的GridView
,但是即使我添加了触发器,GridView
中的FileUpload
也不起作用...因为它找不到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>
这是我通过代码隐藏来做到这一点的一种方法,它只是虚拟模型,可以给你一个想法:
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";
}
FileUpload需要完整的回发才能工作。
- 尝试将 GridView 放入 UpdatePanel 中。
- 设置 网格视图的行命令="GridView1_RowCommand"事件
-
将命令名称设置为按钮命令名称="上传"
<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>
现在甚至通过行命令事件获取按钮。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "upload")
{
//your stuff
}
}