hello sir.....

suppose i want to display list of item name at specific portion on webpage(.aspx page)

but coding portion is done in file with use of for loop i get Item name into variable from database.

now i want to use that varibale to dispaly Item name at specific portion in aspx page
so how i can able to do this thing in aspx page(source view..)?
plz help me ...its emergency....
i hope U help me as my last two post reply...(right solution)

Dani AI

Generated

Avoid Response.Write if you need the output at a specific spot. In Web Forms, place a server control in your .aspx where you want the list, then data-bind to it in code-behind. A lightweight option is a Repeater with a command to remove files.

Example .aspx:

<asp:Repeater ID="rptFiles" runat="server" OnItemCommand="rptFiles_ItemCommand">
  <ItemTemplate>
    <%# Container.ItemIndex + 1 %>. <%# Eval("Name") %>
    <asp:LinkButton runat="server" CommandName="Remove"
        CommandArgument='<%# Eval("FullName") %>' Text="Remove" />
    <br />
  </ItemTemplate>
</asp:Repeater>

Example code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) BindFiles();
    if (FileUpload1.HasFile)
    {
        FileUpload1.SaveAs(Server.MapPath("upload\\" + Path.GetFileName(FileUpload1.FileName)));
        BindFiles();
    }
}

private void BindFiles()
{
    var dir = new DirectoryInfo(Server.MapPath("upload"));
    rptFiles.DataSource = dir.Exists ? dir.GetFiles() : Array.Empty<FileInfo>();
    rptFiles.DataBind();
}

protected void rptFiles_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "Remove")
    {
        var uploadRoot = Path.GetFullPath(Server.MapPath("upload"));
        var full = Path.GetFullPath(e.CommandArgument.ToString());
        if (full.StartsWith(uploadRoot, StringComparison.OrdinalIgnoreCase) && File.Exists(full))
            File.Delete(full);
        BindFiles();
    }
}

This keeps rendering logic in the markup and file operations in code-behind, and avoids mixing raw HTML with Response.Write. If filenames can contain special characters, HTML-encode them in the template (see HttpUtility.HtmlEncode). More on Repeater data binding is in the official docs: Repeater Class.

Recommended Answers

All 4 Replies

Just declare those variables at the top of the page_load method or just below the partial class of your file. Then you will use those variables into the aspx page.

Just declare those variables at the top of the page_load method or just below the partial class of your file. Then you will use those variables into the aspx page.

Sorry Sir,
U have not get my question..
so i will try by my code...
In my file code is.....

public partial class copyfile : System.Web.UI.Page
{
    DirectoryInfo dInfo;
    FileInfo[] FilesList;
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Request["filename"]) != null)
        {
            dInfo = new DirectoryInfo(Server.MapPath("upload"));
            FilesList = dInfo.GetFiles();
            foreach (FileInfo fi in FilesList)
            {
                if (fi.Name.Equals(Request["filename"]))
                {
                    fi.Delete();
                    getfiles();
                    break;
                }
            }
        }
        if (FileUpload1.HasFile)
        {
            getfiles();
        }
    }
    protected void getfiles()
    {
        this.FileUpload1.SaveAs(Server.MapPath("upload\\" + FileUpload1.FileName));
        this.Label1.Text = "File Uploaded Sucessfull!!!!";
        dInfo = new DirectoryInfo(Server.MapPath("upload"));
        FilesList = dInfo.GetFiles();
        int i = 1;
        foreach (FileInfo fi in FilesList)
        {
            Label l = new Label();
            l.Text = fi.Name.ToString();
            Response.Write(i + "." + fi.Name);
            Response.Write("<a href='" + Request.CurrentExecutionFilePath + "?filename=" + fi.Name + "'>Remove</a>");
            Response.Write("");
            Response.Write("</br>");
            i++;
        }
    }
}

My aspx (Desing) page contain...

<html>
      <head id="Head1" runat="server"> 
      <title>Sample Page</title>
      
<script type="text/javascript">
function DoSubmit()
{ 
  var form=document.getElementById("form1");
  form.submit();
}
</script>
      <style type="text/css">
          #form1
          {
              height: 120px;
          }
      </style>
      </head>
      <body>&nbsp;<form id="form1" runat="server">
          <asp:FileUpload ID="FileUpload1" runat="server" onChange="DoSubmit();"
              style="z-index: 1; left: 51px; top: 88px; position: absolute" />
          <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
           <%  %>     
      </form>
      </body>
     </html>

The problem is regarding to forloop bcz U can see in for loop have file name in fi.Name. and now i want to display at particular place in design page
so how i use that in desing view in aspx file?
i hope u will help me?

>suppose i want to display list of item name at specific portion on webpage.

Use ASP.NET Table or GridView or DataList or Repeater control.

Expression Tag: Another way to show value of variables.

aspx markup

<%=Value %>
    <br />
    Name : <%=Name[0] %>
    <br />
    Name : <%=Name[1] %>
    <br />

Code Behind

public int Value = 100;
    public string[] Name = { "AA", "BB" };
    protected void Page_Load(object sender, EventArgs e)
    { }

You may also use simple DataBind using <%# Expression %>
(DataBind() method evaluates those expressions).

aspx markup

<%# Value %>
  <%# Name[0] %>
 <%# Name[1] %>

code-behind

public int Value = 100;
    public string[] Name = { "AA", "BB" };
    protected void Page_Load(object sender, EventArgs e)
    {
        DataBind();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.