本文介绍了如何提供链接提交按钮和网格视图链接按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有以下gridview下载选项(链接按钮),当我点击下载我发送otp到手机, 然后输入otp然后提交和下载应该开始 我的问题是 当我提交otp下载应该开始, 我怎么能给链接提交按钮和网格视图linkbutton aspx代码 < asp:GridView ID = GridView1 runat = server HeaderStyle-BackColor = #cccccc HeaderStyle-ForeColor = 黑色 RowStyle-BackColor = white RowStyle-ForeColor = #003366 AlternatingRowStyle-BackColor = 白色 AlternatingRowStyle-ForeColor = #000 AutoGenerateColumns = false Font-Bo ld = False > < 列 > < asp:BoundField DataField = UserName HeaderText = 用户名 / > < asp:BoundField DataField = DocumentType HeaderText = 文档类型 / > < asp:BoundField DataField = FormType HeaderText = 表单类型 / > < asp:BoundField DataField = 描述 HeaderText = 描述 / > < asp:TemplateField ItemStyle-Horizo​​ntalAlign = 中心 > < ItemTemplate > < asp: LinkBut​​ton ID = lnkDownload runat = server 文字 = 下载 OnClick = document.getElementById('light')。style.display ='block'; document.getElementById('fade')。style.display ='block' CommandArgument =' <% #Eval( Id)%> ;' > < / asp :LinkBut​​ton > < / ItemTemplate > < / asp:TemplateField > < / Columns > < / asp:GridView > 文件下载代码 protected void DownloadFile(object sender,EventArgs e) { int id = int.Parse((sender as LinkBut​​ton).CommandArgument); byte [] bytes; string DocumentType,FormType,fileName,Description,contentType; string constr = ConfigurationManager.ConnectionStrings [constr]。ConnectionString; using(SqlConnection con = new SqlConnection(constr)) { using(SqlCommand cmd = new SqlCommand()) { cmd.CommandText =从tblFiles中选择UserName,DocumentType,FormType,Name,Data,ContentType,Description; cmd.Parameters.AddWithValue(@ Id,id); cmd.Connection = con; con.Open(); 使用(SqlDataReader sdr = cmd.ExecuteReader()) { sdr.Read(); bytes =(byte [])sdr [Data]; DocumentType = sdr [DocumentType]。ToString(); FormType = sdr [FormType ] .ToString(); contentType = sdr [ContentType]。ToString(); fileName = sdr [Name]。ToString(); 描述= sdr [描述] .ToString(); } con.Close(); } } Response.Clear(); Response.Buffer = true; Response.Charset =; Response.Cache.SetCacheability(HttpCacheability.NoCache); 回复.ContentType = contentType; Response.AppendHeader(Content-Disposition,附件; filename =+ fileName); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } otp提交按钮代码 if(txtOtp.Text == txtName.Text) { Response.Write(< script> alert( '匹配......')< / script>); //这里我想下载文件 } else { Response.Write(< script> alert('wrong otp .......')< / script>); } 解决方案 在你的代码中,它是在链接按钮的OnClick属性上触发javascript代码,这是不正确的。你需要使用OnClientClick来激活Javascript函数和OnClick来激活服务器端功能。 第1步:为Gridview添加以下代码 < asp:GridView ID = GridView1 runat = server HeaderStyle-BackColor = #cccccc HeaderStyle-ForeColor = 黑色 RowStyle-BackColor = white RowStyle-ForeColor = #003366 AlternatingRowStyle-BackColor = 白色 AlternatingRowStyle-ForeColor = #000 AutoGenerateColumns = false 字体粗体 = 错误 > < 列 > < asp:BoundField DataField = UserName HeaderText = 用户名 / > < asp:BoundField DataField = DocumentType HeaderText = 文档类型 / > < asp:BoundField DataField = FormType HeaderText = 表单类型 / > < asp:BoundField DataField = 描述 HeaderText = 描述 / > < asp:TemplateField ItemStyle-Horizo​​ntalAlign = 中心 > < ItemTemplate > < asp:LinkBut​​ton ID = lnkDownload runat = server 文字 = 下载 OnClick = DownloadFile OnClientClick = document.getElementById('light')。style.display ='block'; document.getElementById('fade ')。style.display ='block'; CommandArgument =' <% #Eval( Id)%>' > < / asp:LinkBut​​ton > < / ItemTemplate > < / asp:TemplateField > < /列 > < / asp:GridView > 第2步:添加以下代码以解雇服务器端事件。 受保护 void DownloadFile( object sender,EventArgs e) { string filePath =(sender as LinkBut​​ton).CommandArgument; Response.ContentType = ContentType; Response.AppendHeader( Content-Disposition, attachment; filename = + Path.GetFileName(filePath)); Response.WriteFile(filePath); Response.End(); } i have the following gridview with download option(link button) , when i click download am sending otp to mobile,then am entering otp then submit and download should startmy problem iswhen i submit otp download should start,how can i give link to submit button and grid view linkbuttonaspx code <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#cccccc" HeaderStyle-ForeColor="Black" RowStyle-BackColor="white" RowStyle-ForeColor="#003366" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" AutoGenerateColumns="false" Font-Bold="False"> <Columns> <asp:BoundField DataField="UserName" HeaderText="User Name" /> <asp:BoundField DataField="DocumentType" HeaderText="Document Type" /> <asp:BoundField DataField="FormType" HeaderText="Form Type" /> <asp:BoundField DataField="Description" HeaderText="Description" /> <asp:TemplateField ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'" CommandArgument='<%# Eval("Id") %>'></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>file download codeprotected void DownloadFile(object sender, EventArgs e) { int id = int.Parse((sender as LinkButton).CommandArgument); byte[] bytes; string DocumentType, FormType, fileName, Description, contentType; string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select UserName,DocumentType,FormType,Name, Data, ContentType,Description from tblFiles "; cmd.Parameters.AddWithValue("@Id", id); cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { sdr.Read(); bytes = (byte[])sdr["Data"]; DocumentType = sdr["DocumentType"].ToString(); FormType = sdr["FormType"].ToString(); contentType = sdr["ContentType"].ToString(); fileName = sdr["Name"].ToString(); Description = sdr["Description"].ToString(); } con.Close(); } } Response.Clear(); Response.Buffer = true; Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = contentType; Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); }otp submit button codeif (txtOtp.Text == txtName.Text) { Response.Write("<script> alert('matched......')</script>"); //here i want to download file } else { Response.Write("<script> alert('wrong otp.......')</script>"); } 解决方案 In your code, it is firing javascript code on "OnClick" property of Link button which is not correct. You need to use OnClientClick to fire Javascript function and OnClick to fire Server side function.Step 1: Add following code for Gridview<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#cccccc" HeaderStyle-ForeColor="Black"RowStyle-BackColor="white" RowStyle-ForeColor="#003366" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"AutoGenerateColumns="false" Font-Bold="False"><Columns><asp:BoundField DataField="UserName" HeaderText="User Name" /><asp:BoundField DataField="DocumentType" HeaderText="Document Type" /><asp:BoundField DataField="FormType" HeaderText="Form Type" /><asp:BoundField DataField="Description" HeaderText="Description" /><asp:TemplateField ItemStyle-HorizontalAlign="Center"><ItemTemplate><asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick = "DownloadFile" OnClientClick="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block';"CommandArgument='<%# Eval("Id") %>'></asp:LinkButton></ItemTemplate></asp:TemplateField></Columns></asp:GridView>Step 2: Add following code to fire server side event.protected void DownloadFile(object sender, EventArgs e){ string filePath = (sender as LinkButton).CommandArgument; Response.ContentType = ContentType; Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath)); Response.WriteFile(filePath); Response.End();} 这篇关于如何提供链接提交按钮和网格视图链接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 22:55
查看更多