locked
fileupload in gridview RRS feed

  • Question

  • User186897 posted

    Hello

    I have add,update,delete functions gridview

    In the grid view there is column where if the user clicks yes, it should display upload file dialogue below and upload button

    Below is the code

    How do I achieve this?

    If the user selects yes then display these two(i mean make visible=true)

    <asp:TemplateField HeaderText=" Tables">
        <ItemTemplate><%# Eval("CRTables") %></ItemTemplate>
                                
         <EditItemTemplate>
        <asp:RadiobuttonList runat="server" ID="Tables" SelectedValue='<%# Bind("Tables")%>' >
             <asp:ListItem Text="Yes"></asp:ListItem>
             <asp:ListItem Text="No"></asp:ListItem>
            </asp:RadiobuttonList>
             <asp:FileUpload runat="server" ID="FileUpload1" visible="false" />
        <asp:Button runat="server" ID="Button1" Text="Upload"
    visible="false"
    /> </EditItemTemplate> <FooterTemplate> <asp:RadiobuttonList runat="server" ID="tables" SeletedValue='' > <asp:ListItem Text="Yes"></asp:ListItem> <asp:ListItem Text="No"></asp:ListItem> </asp:RadiobuttonList> <asp:FileUpload runat="server" ID="FileUpload1" /> <asp:Button runat="server" ID="Button1" Text="Upload" /> </FooterTemplate> </asp:TemplateField>

    Monday, May 9, 2016 12:18 PM

Answers

  • User1559292362 posted

    Hi Lexi85,

    Lexi85

    it gives error near "as" and says end of statement expected???

    Please modify your code as below by using DirectCast method.

     Dim rbl As RadioButtonList = DirectCast(sender, RadioButtonList)
            Dim gr As GridViewRow = DirectCast(rbl.Parent.Parent, GridViewRow)
            Dim fu As FileUpload = TryCast(gr.FindControl("FileUpload1"), FileUpload)
            Dim btn As Button = TryCast(gr.FindControl("Button1"), Button)
            If rbl.SelectedValue = "Yes" Then
                fu.Visible = True
                btn.Visible = True
            Else
                fu.Visible = False
                btn.Visible = False
            End If

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 23, 2016 8:57 AM

All replies

  • User-1780421697 posted

    1: There should be SelectedIndexChanged event on radio button which let you know about the event or there should be some client side function which let you know that event is occured so that you can display controls accordingly.like display the File uploader control.

    2: On click of Upload you need to fire postback event if you are in update panel and then in event handler you can get the file which is uploaded.

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------

    It is possible that you have a hidden filed on your web page and when user select a record for edit you need to set hiddenfieldvalue like RecordId=1 in hidden field.
    On your page you have model popup control which contains fileupload control which is visible when user click on Yes button.

    In that case you get ID from hidden field and update record.you can use AjaxControllToolkit Modal popup control with fake button for target and can show popup by its javascript show function like model.show().

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------

    Monday, May 9, 2016 12:29 PM
  • User186897 posted

    Thanks for your reply, yes i get that idea but I want to know how it is done because i dont know how to write selectindex changed event for radiobutton list

    I want something like radiobuttonListitem value is yes then make 2tools visible...

    how do I do that?any example code?

    Monday, May 9, 2016 12:32 PM
  • User-1780421697 posted

    I will update you

    Monday, May 9, 2016 12:38 PM
  • User-1780421697 posted
     <asp:RadiobuttonList runat="server" ID="Tables" SelectedValue='<%# Bind("Tables")%>' OnSelectedIndexChanged="Tables_SelectedIndexChanged" >
             <asp:ListItem Text="Yes"></asp:ListItem>
             <asp:ListItem Text="No"></asp:ListItem>
            </asp:RadiobuttonList>
             <asp:FileUpload runat="server" ID="FileUpload1" visible="false" />
             <asp:Button runat="server" ID="Button1" Text="Upload" OnClick="Button1_Click" />
           protected void Tables_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (Tables.SelectedItem.Text.Equals("Yes"))
                {
                    FileUpload1.Visible = true;
                }
                else 
                {
                    FileUpload1.Visible = false;
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                if (FileUpload1.HasFiles)
                {
                    //Map path to your upload folder and save the file
                }
            }

    I think this will help you:

    http://forums.asp.net/t/987024.aspx?GridView+and+FileUpload+control+and+EditItemTemplate

    Monday, May 9, 2016 12:42 PM
  • User186897 posted

    Thanks for the reply but it says the following error that Tables has already declared as sql parameter.

    I want to declare it again how do I do that?

    the code is below

     Private Tables As New System.Data.SqlClient.SqlParameter("Tables", SqlDbType.VarChar, 200)
    

    Tuesday, May 10, 2016 7:55 AM
  • User1559292362 posted

    Hi Lexi85,

    According to your description, you could use OnSelectedIndexChanged event and findcontrol method to achieve it, and I create a simple demo as below for your reference.

    Aspx File.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewWithRadiobuttonList.aspx.cs" Inherits="WingtipToys.GridView.GridViewWithRadiobuttonList" %>
    
     
    
    <!DOCTYPE html>
    
     
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head runat="server">
    
        <title></title>
    
    </head>
    
    <body>
    
        <form id="form1" runat="server">
    
        <div>
    
            <asp:GridView ID="GridView1" runat="server">
    
                <Columns>
    
                    <asp:TemplateField HeaderText="TestID">
    
                        <ItemTemplate>
    
                            <%# DataBinder.Eval(Container.DataItem,"ProductId").ToString().Equals("1") ? "Disabled":DataBinder.Eval(Container.DataItem,"ProductId")%>
    
                        </ItemTemplate>
    
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="Tables">
    
                        <ItemTemplate>  
    
                            <asp:RadiobuttonList runat="server" ID="Tables" OnSelectedIndexChanged="Tables_SelectedIndexChanged" AutoPostBack="true">
    
                            <asp:ListItem Text="Yes"></asp:ListItem>
    
                            <asp:ListItem Text="No"></asp:ListItem>
    
                            </asp:RadiobuttonList>
    
                            <asp:FileUpload runat="server" ID="FileUpload1" visible="false" />
    
                            <asp:Button ID="Button1" runat="server" visible="false" Text="upload" OnClick="Button1_Click"/>
    
                       </ItemTemplate>   
    
                    </asp:TemplateField>
    
                </Columns>
    
     
    
            </asp:GridView>
    
        </div>
    
        </form>
    
    </body>
    
    </html>
    

    Code Behind

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Web;
    
    using System.Web.UI;
    
    using System.Web.UI.WebControls;
    
    using WingtipToys.Models;
    
     
    
    namespace WingtipToys.GridView
    
    {
    
        public partial class GridViewWithRadiobuttonList : System.Web.UI.Page
    
        {
    
            protected void Page_Load(object sender, EventArgs e)
    
            {
    
                if (!IsPostBack)
    
                {
    
                    BindData();
    
                }
    
            }
    
     
    
            private void BindData()
    
            {
    
                var _db = new WingtipToys.Models.ProductContext();
    
                IQueryable<Product> query = _db.Products;
    
                GridView1.DataSource = query.ToList();
    
                GridView1.DataBind();
    
            }
    
     
    
            public IQueryable<Product> GetProducts()
    
            {
    
                var _db = new WingtipToys.Models.ProductContext();
    
                IQueryable<Product> query = _db.Products;
    
                //if (categoryId.HasValue && categoryId > 0)
    
                //{
    
                //    query = query.Where(p => p.CategoryID == categoryId);
    
                //}
    
                return query;
    
            }
    
     
    
            protected void ChckBoxAuthorized_CheckedChanged(object sender, EventArgs e)
    
            {
    
               
    
                CheckBox chk = (CheckBox)sender;
    
                GridViewRow gr = (GridViewRow)chk.Parent.Parent;
    
                Response.Write(gr.Cells[0].Text + " --" + gr.Cells[1].Text + " --" + gr.Cells[2].Text + " --" + gr.Cells[3].Text);
    
     
    
            }
    
     
    
            protected void Tables_SelectedIndexChanged(object sender, EventArgs e)
    
            {
    
                RadioButtonList rbl = (RadioButtonList)sender;
    
                GridViewRow gr = (GridViewRow)rbl.Parent.Parent;
    
                FileUpload fu = gr.FindControl("FileUpload1") as FileUpload;
    
                Button btn = gr.FindControl("Button1") as Button;
    
                if (rbl.SelectedValue == "Yes")
    
                {
    
                    fu.Visible = true;
    
                    btn.Visible = true;
    
                }
    
                else
    
                {
    
                    fu.Visible = false;
    
                    btn.Visible = false;
    
                }
    
            }
    
     
    
            protected void Button1_Click(object sender, EventArgs e)
    
            {
    
                Button btn = (Button)sender;
    
                GridViewRow gr = (GridViewRow)btn.Parent.Parent;
    
                FileUpload fu = gr.FindControl("FileUpload1") as FileUpload;
    
                if (fu.HasFile)
    
                {
    
                    //handle the pic
    
                }
    
            }
    
        }
    
    }
    

    Best regards,

    Cole Wu

    Wednesday, May 11, 2016 9:43 AM
  • User186897 posted

    it gives error near "as" and says end of statement expected???

       Dim rbl As RadioButtonList = CType(sender, RadioButtonList)
    
            Dim gr As GridViewRow = CType(rbl.Parent.Parent, GridViewRow)
    
            Dim fu As FileUpload = gr.FindControl("FileUpload1") As FileUpload
    
            Dim btn As Button = gr.FindControl("Button1") As Button 
     
                If rbl.SelectedValue = "Yes" Then
    
                fu.Visible = True
    
                btn.Visible = True
    
            Else
    
                fu.Visible = False
    
                btn.Visible = False
    
            End If
    

    Thursday, May 12, 2016 8:21 AM
  • User1559292362 posted

    Hi Lexi85,

    Lexi85

    it gives error near "as" and says end of statement expected???

    Please modify your code as below by using DirectCast method.

     Dim rbl As RadioButtonList = DirectCast(sender, RadioButtonList)
            Dim gr As GridViewRow = DirectCast(rbl.Parent.Parent, GridViewRow)
            Dim fu As FileUpload = TryCast(gr.FindControl("FileUpload1"), FileUpload)
            Dim btn As Button = TryCast(gr.FindControl("Button1"), Button)
            If rbl.SelectedValue = "Yes" Then
                fu.Visible = True
                btn.Visible = True
            Else
                fu.Visible = False
                btn.Visible = False
            End If

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 23, 2016 8:57 AM