Mas

Mas

  • NA
  • 478
  • 68.5k

How to bind the drop down value to grid

Feb 14 2020 3:54 AM
Hello Members,
Hope you are doing good!!
I am trying to add the dropdown value to grid...
Can any any guide me here
am adding the ASPX,CS code below
  1. <body>  
  2. <form id="form1" runat="server">  
  3. <asp:FileUpload ID="FileUpload1" runat="server" />  
  4. <asp:DropDownList ID="Dvisa" runat="server">  
  5. <asp:Listitem Value="1" Text="select"></asp:Listitem>  
  6. <asp:Listitem Value="2" >1</asp:Listitem>  
  7. <asp:Listitem Value="3" >2</asp:Listitem>  
  8. <asp:Listitem Value="4" >3</asp:Listitem>  
  9. <asp:Listitem Value="5" >4</asp:Listitem>  
  10. <asp:Listitem Value="6" >5</asp:Listitem>  
  11. <asp:Listitem Value="7" >6</asp:Listitem>  
  12. <asp:Listitem Value="8" >7</asp:Listitem>  
  13. <asp:Listitem Value="9" >8</asp:Listitem>  
  14. <asp:Listitem Value="10" >9</asp:Listitem>  
  15. <asp:Listitem Value="11" >10</asp:Listitem>  
  16. <asp:Listitem Value="12" >11</asp:Listitem>  
  17. <asp:Listitem Value="13" >12</asp:Listitem>  
  18. <asp:Listitem Value="14" >13</asp:Listitem>  
  19. <asp:Listitem Value="15" >14</asp:Listitem>  
  20. </asp:DropDownList>  
  21. <asp:Button ID="btnUpload" runat="server" Text="Submit" onclick="btnUpload_Click" />  
  22. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  
  23. EmptyDataText = "No files uploaded" CellPadding="4"  
  24. EnableModelValidation="True" ForeColor="#333333" GridLines="None">  
  25. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
  26. <Columns>  
  27. <asp:BoundField DataField="Text" HeaderText="File Name" />  
  28. <%--<asp:TemplateField>  
  29. <ItemTemplate>  
  30. <asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>  
  31. </ItemTemplate>  
  32. </asp:TemplateField>--%>  
  33. <asp:TemplateField>  
  34. <ItemTemplate>  
  35. <asp:LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "DeleteFile" />  
  36. </ItemTemplate>  
  37. </asp:TemplateField>  
  38. </Columns>  
  39. <EditRowStyle BackColor="#999999" />  
  40. <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  41. <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  42. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
  43. <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
  44. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
  45. </asp:GridView>  
  46. </form>  
  47. </body>  
CS Code
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. if (!IsPostBack)  
  4. {  
  5. BindGrid();  
  6. }  
  7. }  
  8. protected void BindGrid()  
  9. {  
  10. string[] filePaths = Directory.GetFiles(Server.MapPath("~/images/"));  
  11. List files = new List();  
  12. foreach (string filePath in filePaths)  
  13. {  
  14. files.Add(new ListItem(Path.GetFileName(filePath), filePath));  
  15. }  
  16. GridView1.DataSource = files;  
  17. GridView1.DataBind();  
  18. }  
  19. protected void btnUpload_Click(object sender, EventArgs e)  
  20. {  
  21. if (FileUpload1.HasFile)  
  22. {  
  23. FileUpload1.SaveAs(Server.MapPath("~/images/") + FileUpload1.FileName);  
  24. BindGrid();  
  25. }  
  26. else  
  27. {  
  28. Response.Write("Please select file to upload");  
  29. }  
  30. }  
  31. protected void DownloadFile(object sender, EventArgs e)  
  32. {  
  33. string filePath = (sender as LinkButton).CommandArgument;  
  34. Response.ContentType = ContentType;  
  35. Response.AppendHeader("Content-Disposition""attachment; filename=" + Path.GetFileName(filePath));  
  36. Response.WriteFile(filePath);  
  37. Response.End();  
  38. }  
  39. protected void DeleteFile(object sender, EventArgs e)  
  40. {  
  41. string filePath = (sender as LinkButton).CommandArgument;  
  42. File.Delete(filePath);  
  43. BindGrid();  
  44. }  
Early response, Highly appriciated

Answers (1)