如何在DetailsView中使用按钮onclick?到目前为止,我有这个。
<asp:DetailsView ID="DetailsView1"
DefaultMode= "Edit" AutoGenerateEditButton="False" AutoGenerateInsertButton="True"
AutoGenerateDeleteButton="True" DataSourceID="SqlDataSource2" runat="server"
AutoGenerateRows="False" DataKeyNames="fileID" >
<Fields>
<asp:BoundField DataField="fileID" HeaderText="fileID" InsertVisible="False" ReadOnly="True" SortExpression="fileID" />
<asp:BoundField DataField="filenameName" HeaderText="filenameName" SortExpression="filenameName" />
<asp:TemplateField HeaderText="filePath" SortExpression="filePath">
<EditItemTemplate>
<asp:FileUpload ID="FileEditUpload1" Width="300px" runat="server" /> <br />
<asp:Button ID="Button1" runat="server" Text="Upload file" onclick="Button1_Click"/>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("filePath") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("filePath") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Createdby" HeaderText="Createdby" SortExpression="Createdby" Visible="False" />
<asp:BoundField DataField="CreatedDt" HeaderText="CreatedDt" SortExpression="CreatedDt" Visible="False" />
<asp:BoundField DataField="Updatedby" HeaderText="Updatedby" SortExpression="Updatedby" Visible="False" />
<asp:BoundField DataField="UpdatedDt" HeaderText="UpdatedDt" SortExpression="UpdatedDt" Visible="False" />
<asp:BoundField DataField="Active" HeaderText="Active" SortExpression="Active" Visible="False" />
</Fields>
</asp:DetailsView>
代码隐藏
protected void Button1_Click(object sender, EventArgs e)
{
//Get path from web.config file to upload
string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
bool blSucces = false;
string filename = string.Empty;
string pathname = string.Empty;
string filePathName = string.Empty;
if (FileEditUpload1.HasFile)
{
}
答案 0 :(得分:1)
您可以从DetailsView访问fileUpload控件,如下所示:
protected void Button1_Click(object sender, EventArgs e)
{
//Get path from web.config file to upload
string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
bool blSucces = false;
string filename = string.Empty;
string pathname = string.Empty;
string filePathName = string.Empty;
//To access the file upload control
//First get the clicked button
Button btn = (Button)sender;
//Then get the detailsview row
DetailsViewRow drv = (DetailsViewRow)btn.Parent.Parent;
//Now you can access the FileUpload control
FileUpload FileEditUpload1 = (FileUpload)drv.FindControl("FileEditUpload1");
if (FileEditUpload1.HasFile)
{
//Do the rest of your code
}
}
试一试,如果您遇到任何问题,请通知我。